按钮处理程序不工作。基本Java矩形计算器

时间:2016-08-04 15:52:52

标签: java

所以我一直在关注基本Java GUI计算器的教程。无论出于何种原因,当我单击“计算”按钮时,控制台会提供大量错误。我怎样才能解决这个问题。

代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class RectangleProgram extends JFrame {

private static final int WIDTH = 400;
private static final int HEIGHT = 300;

private JLabel lengthL, widthL, areaL;
private JTextField lengthTF, widthTF, areaTF;
private JButton calculateB, exitB;

//Button handlers
private CalculateButtonHandler cbHandler;
private ExitButtonHandler ebHandler;

public RectangleProgram(){
    lengthL = new JLabel("Enter the length: ", SwingConstants.RIGHT);
    widthL = new JLabel("Enter the width: ", SwingConstants.RIGHT);
    areaL = new JLabel("Area: ", SwingConstants.RIGHT);

    lengthTF = new JTextField(10);
    widthTF = new JTextField(10);
    areaTF = new JTextField(10);


    //New buttons

    calculateB = new JButton("Calculate");
    cbHandler = new CalculateButtonHandler();
    calculateB.addActionListener(cbHandler);
    exitB = new JButton("Exit");
    ebHandler = new ExitButtonHandler();
    exitB.addActionListener(ebHandler);

    setTitle("Sample Title: Area of a rectangle");
    Container pane = getContentPane();
    pane.setLayout(new GridLayout(4, 2));

    //Adding wings and things... well just things... no wings :(

    pane.add(lengthL);
    pane.add(lengthTF);
    pane.add(widthL);
    pane.add(widthTF);
    pane.add(areaL);
    pane.add(areaTF);
    pane.add(calculateB);
    pane.add(exitB);

    setSize(WIDTH, HEIGHT);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

}


private class CalculateButtonHandler implements ActionListener{
    public void actionPerformed(Action e){
        double width, length, area;

        length = Double.parseDouble(lengthTF.getText());
        width = Double.parseDouble(widthTF.getText());
        area = length * width;

        areaTF.setText("" + area);
    }
}

public class ExitButtonHandler implements ActionListener{

    public void actionPerformed(Action e){
        System.exit(0);

    }
}

public static void main(String[] args){
    RectangleProgram rectObj = new RectangleProgram();
}

}

错误:

  

线程“AWT-EventQueue-0”中的异常java.lang.Error:未解析的编译>问题:     RectangleProgram.CalculateButtonHandler类型必须实现>继承的抽象方法ActionListener.actionPerformed(ActionEvent)

     

at> RectangleProgram $ CalculateButtonHandler.actionPerformed(RectangleProgram.java:5>9)     在javax.swing.AbstractButton.fireActionPerformed(未知来源)     at javax.swing.AbstractButton $ Handler.actionPerformed(Unknown Source)     在javax.swing.DefaultButtonModel.fireActionPerformed(未知来源)     在javax.swing.DefaultButtonModel.setPressed(未知来源)     在javax.swing.plaf.basic.BasicButtonListener.mouseReleased(未知来源)     at java.awt.Component.processMouseEvent(Unknown Source)     在javax.swing.JComponent.processMouseEvent(未知来源)     at java.awt.Component.processEvent(Unknown Source)     at java.awt.Container.processEvent(Unknown Source)     at java.awt.Component.dispatchEventImpl(Unknown Source)     at java.awt.Container.dispatchEventImpl(Unknown Source)     at java.awt.Component.dispatchEvent(Unknown Source)     at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)     at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)     at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)     at java.awt.Container.dispatchEventImpl(Unknown Source)     at java.awt.Window.dispatchEventImpl(Unknown Source)     at java.awt.Component.dispatchEvent(Unknown Source)     at java.awt.EventQueue.dispatchEventImpl(Unknown Source)     在java.awt.EventQueue.access $ 500(未知来源)     在java.awt.EventQueue $ 3.run(未知来源)     在java.awt.EventQueue $ 3.run(未知来源)     at java.security.AccessController.doPrivileged(Native Method)     at> java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(U> nknown Source)     at> java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(U> nknown Source)     在java.awt.EventQueue $ 4.run(未知来源)     在java.awt.EventQueue $ 4.run(未知来源)     at java.security.AccessController.doPrivileged(Native Method)     at> java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(U> nknown Source)     at java.awt.EventQueue.dispatchEvent(Unknown Source)     at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)     at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)     at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)     在java.awt.EventDispatchThread.run(未知来源)

1 个答案:

答案 0 :(得分:0)

在actionPerformed方法中,更改参数"操作e" to" ActionEvent e"在您的CalculateButtonHandler和ExitButtonHandle类中。

如果您查看ActionListener接口方法详细信息,您会注意到actionPerformed方法将ActionEvent作为参数。由于您在类中实现了接口,因此您需要覆盖该方法。