2013-07-13 77 views
0

为了达到这个目的,我无法通过外部ActionListener类来编写静态双精度值或JTextField。 我正在制作一个高级计算器,为了让事情变得更容易,我正在尝试创建GUI,实现其按钮和其他功能,并且我正在尝试将ActionListeners放在其他类中。在eclipse中,它表示我需要让计算器的静态变量静态,但使它们变为静态,我不能再写入它们,并显示答案。 这是代码我有:Java ActionListener类不写入静态变量

public static JButton num0, num1, num2, num3, num4, num5, num6, num7, num8, num9; 
public static double tempNum1; 
public static double tempNum2; 
public static boolean pointOn = false; 
public static int APC = 1; 

public GUI(){ 
    GUINumListener numListener = new GUINumListener(); 

    num0.addActionListener(numListener); 
    num1.addActionListener(numListener); 
    num2.addActionListener(numListener); 
    num3.addActionListener(numListener); 
    num4.addActionListener(numListener); 
    num5.addActionListener(numListener); 
    num6.addActionListener(numListener); 
    num7.addActionListener(numListener); 
    num8.addActionListener(numListener); 
    num9.addActionListener(numListener); 
} 

并在GUINumListener类:

public class GUINumListener implements ActionListener{ 
    public void actionPerformed(ActionEvent e){ 
     if (e.getActionCommand().equals(GUI.num0)){ 
      GUI.tempNum2 *= 10; 
     }else if (e.getActionCommand().equals(GUI.num1)){ 
      if (GUI.pointOn = false){ 
       GUI.tempNum2 = (GUI.tempNum2 * 10) + 1; 
      }else{ 
       GUI.tempNum2 = (GUI.tempNum2 * Math.pow(10, GUI.APC) + 1)/Math.pow(10, GUI.APC); 
       GUI.APC++; 
      } 
      GUI.ansField.setText(Double.toString(GUI.tempNum2)); 
     } 
    } 

点击程序中的数不输出它在ansField字段。 帮助! 感谢

+4

不要让他们一成不变的。 Eclipse只是抱怨,因为代码没有编译,并且提出了一个解决方案来编译它。但是编译代码并不能使它正确和清晰。可写入的静态变量是99.9%的次数,是一种设计气味。哦,田野也不应该公开。使它们成为私有的,或者至少是包私有的。 –

+2

我第二次都是由@JBNizet指出的。并且了解并使用数组或集合。如果做得好,它可以使你的代码更短,更容易调试和维护。 –

回答

1

不要使用静态字段,并封装它们。

对于ActionListener如果唯一的范围是外部类或Anoymous Classes,我总是使用内部私有类。

此外,它似乎你有一个按钮的集合,你可以考虑分组收集。

实例(我在代码注释):

private List<JButton> buttons; 
private double tempNum1; 
private double tempNum2; 
private boolean pointOn = false; 
private int APC = 1; 
//make them private why public and static? 


public GUI(){ 
    ActionListener numListener = new GUINumListener(); 

    //initiliatze buttons 

    int size=10; 
    buttons= new ArrayList<>(size); 

    for(int i=0;i<size;i++){ 
     JButton button = new JButton(); 
     button.addActionListener(numListener); 
     buttons.add(button);    
    } 

} 


private class GUINumListener implements ActionListener{ 
@Override 
public void actionPerformed(ActionEvent e){ 
     if (e.getSource() == buttons.get(0)){ // actionCommand returns string you have to use getSource() or setting an actionCommand to the button and compare num0.getActionCommand() 
      tempNum2 *= 10; 
     }else if (e.getSource() == buttons.get(1)){ 
      if (!pointOn){ // u were assigning pointOn = false 
       tempNum2 = (tempNum2 * 10) + 1; 
      }else{ 
       tempNum2 = (tempNum2 * Math.pow(10, APC) + 1)/Math.pow(10, APC); 
       APC++; 
      } 
      ansField.setText(Double.toString(tempNum2)); 
     } 

} 
+0

即使'GUINumListener'成为内部类。 – kuporific

+0

@kuporific我在编辑你的评论xD – nachokk

+0

我觉得你到了那里;)这是问题的适当解决方案。 – kuporific

1

的问题是你的if检查:e.getActionCommand().equals(GUI.num0)

getActionCommand()返回Stringnum0JButton。因此,equals将始终返回false,因为它们不是相同的类类型。要解决这个

一种方法是检查按钮的标签:getActionCommand()

所以if的说法应该是:

e.getActionCommand().equals(GUI.num0.getActionCommand())