有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

GUI中的java ActionListener。功能不起作用

我有一个关于我的程序的问题,这个程序包含一个类Calculator,它应该实现一个计算器,能够使用+*类型double进行操作

我也为这个计算器编写了一个GUI,它已经很好地工作了,但是按钮不起作用,尽管我已经实现了这个功能

public void actionPerformed(ActionEvent e)

我想这个错误一定在这个函数的某个地方。我只是不知道为什么按钮的功能不起作用。这是密码

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

共 (3) 个答案

  1. # 1 楼答案

    看起来您有一个复制粘贴错误:

    这:

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

    应该是:

        JButton multiplizieren = new JButton("*");
        multiplizieren.addActionListener(this);
        JButton addieren = new JButton("+");
        addieren.addActionListener(this);
        JButton clear = new JButton("clear");
        clear.addActionListener(this);
    
  2. # 2 楼答案

    问题是@kuporific说了些什么,但不是在顶级容器中执行implements ActionListener,您可以:

    1. 创建私有类,或
    2. 使用匿名类

    使用Swing Action(匿名类)的示例

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

    因此,您可以为每个按钮隔离相关的操作,而不是在任何地方使用ìf-else

    此外,您可以在文本字段中添加仅接受数字的documentFilter,或使用JFormattedTextField

  3. # 3 楼答案

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