【问题标题】:ActionListener in GUI. Functionality doesn't workGUI 中的 ActionListener。功能不起作用
【发布时间】:2013-06-25 06:18:05
【问题描述】:

我的程序有一个问题,该程序包含一个单独的类Calculator,它应该实现一个能够使用+* 操作类型为double 的计算器。

我也为那个计算器写了一个图形用户界面,它已经很好用了,但是按钮不起作用,虽然我已经实现了这个功能

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

【问题讨论】:

  • "已经可以正常工作,但是按钮不能正常工作" - 然后就不能正常工作了:D
  • 1) 对代码块使用一致且符合逻辑的缩进。代码的缩进是为了帮助人们理解程序流程。 2) 不要扩展框架或其他顶级容器。而是创建并使用一个实例。
  • 定义“不起作用”。处理程序中的断点是否命中?会发生什么?

标签: java swing user-interface actionlistener actionevent


【解决方案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);

【讨论】:

  • 工作正常,感谢 kuporific 的帮助。我对 nachokk 的评论还有一个问题:我如何控制那个动作事件?然后我必须把 e.getActionCommand() 放在每个被覆盖的 actionPerformed 方法中,对吧?
  • @rikojir 你不再需要actionCommand,我会更新答案
【解决方案2】:

问题是@kuporific 所说的,但您可以:

  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

【讨论】:

  • if-else 构造远远好于为每个JComponent 的相关Action 创建新类的相关开销。尽管您的第一点更合适,但关于创建私有类,如引用here
  • @nIcEcOw 是的,您可以使用所有 if-else 而不是以面向对象的方式编程来提高性能..
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多