动作听众麻烦

时间:2014-04-14 23:54:32

标签: java swing applet actionlistener

我的小applet出了问题。

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

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

1 个答案:

答案 0 :(得分:2)

我认为你必须让你的子类内部类,就像他们可以直接与你的主要classe交谈,并相互改变属性。

所以试试这个并看看:

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

告诉我们它是否有效;)