我的小程序遇到问题。

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


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

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/

10-11 22:28