Question

to get to the point, i can't seam to write to a static double vareable, or JTextField from an external ActionListener class. 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. 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. 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:

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. HELP! thanks

Était-ce utile?

La solution

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.

Also it seems you have a collection of Button, you may consider grouping into collection.

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

}

Autres conseils

The problem is your if checks: e.getActionCommand().equals(GUI.num0)

getActionCommand() returns a String and num0 is a JButton. So equals will always return false since they're not the same class type.

One way to solve this is to check the label of the button: getActionCommand()

So the if statement should be:

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top