java - Action 监听器的麻烦

标签 java swing applet actionlistener

我的小程序遇到问题。

public CdProgram()
   {
             //Create the four labels
      amountL = new JLabel("Enter the amount deposited: ",
                                  SwingConstants.RIGHT);
      yearsL = new JLabel("Enter the years: ",
                                  SwingConstants.RIGHT);
      interestL = new JLabel("Enter the interest rate: ", SwingConstants.RIGHT);
      certificateL = new JLabel("Certificate: ",
                                  SwingConstants.RIGHT);

             //Create the four text fields
      amountTF = new JTextField(10);
      yearsTF = new JTextField(10);
      interestTF = new JTextField(10);
      certificateTF = new JTextField(10);

             //Create Calculate Button
      calculateB = new JButton("Calculate");
      cbHandler = new CalculateButtonHandler();
      calculateB.addActionListener(cbHandler);

             //Create Exit Button
      exitB = new JButton("Exit");
      ebHandler = new ExitButtonHandler();
      exitB.addActionListener(ebHandler);

             //Set the title of the window
      setTitle("Certificate of amount on maturity");

             //Get the container
      Container pane = getContentPane();

             //Set the layout
      pane.setLayout(new GridLayout(5, 2));

             //Place the components in the pane
      pane.add(amountL);
      pane.add(amountTF);
      pane.add(yearsL);
      pane.add(yearsTF);
      pane.add(interestL);
      pane.add(interestTF);
      pane.add(certificateL);
      pane.add(certificateTF);
      pane.add(calculateB);
      pane.add(exitB);

             //Set the size of the window and display it
      setSize(WIDTH, HEIGHT);
      setVisible(true);
      setDefaultCloseOperation(EXIT_ON_CLOSE);
   }



   public static void main(String[] args)
   {
       CdProgram CertObjt = new CdProgram();
   }
}


private class CalculateButtonHandler implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
         double amount, years, interst, certificate;

         amount = Double.parseDouble(amountTF.getText());
         years = Double.parseDouble(yearsTF.getText());
         interest = Double.parseDouble(interestTF.getText());
         certificate = amount * Math.pow(1 + rate/100, years);

         certificateTF.setText("" + certificate);
      }
   }

private class ExitButtonHandler implements ActionListener
   {
       public void actionPerformed(ActionEvent e)
       {
           System.exit(0);
       }
   }

当我实现 Action 监听器功能时,我的子类无法识别,因此我不断收到如下错误:

CalculateButtonHandler.java:1: error: cannot find symbol
private class CalculateButtonHandler implements ActionListener

CalculateButtonHandler.java:3: error: cannot find symbol
      public void actionPerformed(ActionEvent e)

CdProgram.java:39: error: method addActionListener in class AbstractButton cannot be applied to given types;
      calculateB.addActionListener(cbHandler);
                ^
  required: ActionListener
  found: CalculateButtonHandler

我不太确定我在这里做错了什么。谢谢您的帮助。

最佳答案

我认为你必须让你的子类成为内部类,这样它们就可以直接与你的主类对话,并相互更改属性。

所以试试这个并看看:

alculateB.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e)
      {
         double amount, years, interst, certificate;

         amount = Double.parseDouble(amountTF.getText());
         years = Double.parseDouble(yearsTF.getText());
         interest = Double.parseDouble(interestTF.getText());
         certificate = amount * Math.pow(1 + rate/100, years);

         certificateTF.setText("" + certificate);
      }
   });

exitB.addActionListener(new ActionListener(){
       public void actionPerformed(ActionEvent e)
       {
           System.exit(0);
       }
   });

让我们知道它是否有效;)

关于java - Action 监听器的麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23072371/

相关文章:

java - 我的 Java 程序没有生成 CSV 输出

java - 使用任务调度程序运行 java jar(来自批处理文件)

java - Intellij IDEA 内置检查代码 vs checkstyle, PMD & findbugs

java - 如何隐藏或删除 JLabel

java - MigLayout:尝试全屏时仅显示窗口

java - 使用 UCanAccess 而不是 JDBC-ODBC Bridge 来处理 DBF 文件

java - JTree - 如何将背景设置为节点

java - 签名的小程序不显示发布者信息

java - 使用 mysql-connector 将小程序连接到数据库并向小程序添加外部 jar

java - 所有的 Java 小程序都去哪儿了?