简体   繁体   中英

ActionListener in GUI. Functionality doesn't work

I've got a question on my program, the program contains one single class Calculator , which should implement a calculator who is able to operate with + and * with the type double .

I have written a GUI for that calculator, too, which already works fine, but the buttons don't work, although I've implemented the function

public void actionPerformed(ActionEvent e)

The mistake has to be somewhere in this function I guess. I just don't know why the functionality of the buttons doesn't work. Here's the code.

public class Calculator extends JFrame implements ActionListener {

    Calculator () {}

    JTextField parameter1;
    JTextField parameter2;
    JTextField ergebnis;
    JFrame window;
    Container cont;
    /* this function works fine */
    public void calculator_GUI() {     
       builds the GUI of the calculator,     
       this.window = new JFrame("Calculator");
       window.setSize(300,300);
       this.parameter1 = new JTextField("Parameter1...", 10);
       parameter1.addActionListener(this);
       this.parameter2 = new JTextField("Parameter1...", 10);
       parameter2.addActionListener(this);
       this.ergebnis = new JTextField("Ergebnis...", 5);
       ergebnis.addActionListener(this);
       JButton multiplizieren = new JButton("*");
       parameter1.addActionListener(this);
       JButton addieren = new JButton("+");
       parameter1.addActionListener(this);
       JButton clear = new JButton("clear");
       parameter1.addActionListener(this);
       this.cont = window.getContentPane();

       FlowLayout flowLayout = new FlowLayout(FlowLayout.RIGHT);
       cont.setLayout(flowLayout);

       cont.add(parameter1);
       cont.add(parameter2);
       cont.add(ergebnis);
       cont.add(multiplizieren);
       cont.add(addieren);
       cont.add(clear);
       window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       window.pack();
       window.setVisible(true);;
   }

   public void actionPerformed(ActionEvent e) {  
       String label = e.getActionCommand();
       if (label.equals("*")) {
           String a = parameter1.getText();
           String b = parameter2.getText();
           double zahl1=Double.parseDouble(a);
           double zahl2=Double.parseDouble(b);
           double result = zahl1*zahl2;
           String c = String.valueOf(result);
           ergebnis.setText(c);
       }
       else if (label.equals("+")) {
           String a = parameter1.getText();
           String b = parameter2.getText();
           double zahl1=Double.parseDouble(a);
           double zahl2=Double.parseDouble(b);
           double result = zahl1+zahl2;
           String c = String.valueOf(result);
           ergebnis.setText(c);
      }

      else if (label.equals("clear")) {
           String z = "";
           ergebnis.setText(z);
      }
      else { 
           window.dispose(); 
      }
}


  public static void main (String[] args) {   
      Calculator MyCalculator = new Calculator();
      MyCalculator.calculator_GUI();
  }
}

Looks like you have a copy-paste error:

This:

    JButton multiplizieren = new JButton("*");
    parameter1.addActionListener(this);
    JButton addieren = new JButton("+");
    parameter1.addActionListener(this);
    JButton clear = new JButton("clear");
    parameter1.addActionListener(this);

Should be:

    JButton multiplizieren = new JButton("*");
    multiplizieren.addActionListener(this);
    JButton addieren = new JButton("+");
    addieren.addActionListener(this);
    JButton clear = new JButton("clear");
    clear.addActionListener(this);

The problem is what @kuporific says but instead of doing implements ActionListener in your top level container you can:

  1. create private classes, or
  2. use anonymous classes

Example using Swing Action (anonymous classes)

JButton multiplizieren = new JButton("*");
multiplizieren.addActionListener(new ActionListener(){
         @Override
         public void actionPerformed(ActionEvent evt){
                String a = parameter1.getText();
                String b = parameter2.getText();
                double zahl1=Double.parseDouble(a); // this can cause NumberFormatException
                double zahl2=Double.parseDouble(b); // this can cause NumberFormatException
                double result = zahl1*zahl2;
                String c = String.valueOf(result);
                ergebnis.setText(c);
         }
});
JButton addieren = new JButton("+");
addieren.addActionListener((new ActionListener(){
         @Override
         public void actionPerformed(ActionEvent evt){
            String a = parameter1.getText();
            String b = parameter2.getText();
            double zahl1=Double.parseDouble(a);
            double zahl2=Double.parseDouble(b);
            double result = zahl1+zahl2;
            String c = String.valueOf(result);
            ergebnis.setText(c);
         }
});

So instead of using ìf-else everywhere you can isolate associated actions for each button.

Besides you can add in the textfield a documentFilter that only accept numbers, or use a JFormattedTextField

将actionListener添加到按钮而不是文本字段

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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