简体   繁体   English

Java ActionListener类不写入静态变量

[英]Java ActionListener class not writing to static variable

to get to the point, i can't seam to write to a static double vareable, or JTextField from an external ActionListener class. 为了达到目的,我无法通过外部ActionListener类写入静态双vareable或JTextField。 i am making an advanced calculator, and to make things easier, i am trying to create the GUI, imlementing its buttons, and other features, and i am trying to put the ActionListeners in anouther class. 我正在制作一个高级计算器,为了使事情变得更容易,我正在尝试创建GUI,模仿其按钮和其他功能,我正在尝试将ActionListeners放在一个类中。 in eclipse, it says i need to have the vareables for the calculator static, but making them static, i can no longer write to them, and display answers. 在eclipse中,它说我需要让计算器的静态可变,但是让它们静态,我不能再写入它们,并显示答案。 this is the code i have: 这是我的代码:

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);
}

and in the GUINumListener class: 并在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));
        }
    }

clicking a number in the program does not output it in the ansField field. 单击程序中的数字不会在ansField字段中输出它。 HELP! 救命! thanks 谢谢

The problem is your if checks: e.getActionCommand().equals(GUI.num0) 问题是你的if检查: e.getActionCommand().equals(GUI.num0)

getActionCommand() returns a String and num0 is a JButton . getActionCommand()返回一个Stringnum0是一个JButton So equals will always return false since they're not the same class type. 因此, equals将始终返回false因为它们不是同一类类型。

One way to solve this is to check the label of the button: getActionCommand() 解决此问题的一种方法是检查按钮的标签: getActionCommand()

So the if statement should be: 所以if语句应该是:

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

Don't use static fields, and also encapsulate them. 不要使用静态字段,也要封装它们。

For ActionListener i always use inner private classes if the only scope is the outside class or Anoymous Classes . 对于ActionListener如果唯一的范围是外部类或Anoymous Classes我总是使用内部私有Anoymous Classes

Also it seems you have a collection of Button, you may consider grouping into collection. 此外,您似乎有一个Button集合,您可以考虑分组到集合中。

Example (I comment in code): 示例(我在代码中发表评论):

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));
        }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM