我决定制作一个简洁的小计算器程序,但是现在,当我按下计算并运行计算脚本时,它并没有打开一个说明答案的框。我找不到问题。谁能告诉我?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Gui extends JFrame{
private JTextField text;
private JTextField text2;
private JRadioButton plus;
private JRadioButton divide;
private JRadioButton multiply;
private JRadioButton subtract;
private ButtonGroup group;
private JButton button;
private int str = 1;
private String txt;
private String txt2;
private int num1;
private int num2;
private int ans;
public Gui(){
super("Calculator");
setLayout(new FlowLayout());
text = new JTextField(10);
text2 = new JTextField(10);
add(text);
add(text2);
plus = new JRadioButton("Add", true);
divide = new JRadioButton("Divide", false);
multiply = new JRadioButton("Multiply", false);
subtract = new JRadioButton("Subtract", false);
group = new ButtonGroup();
group.add(plus);
group.add(divide);
group.add(multiply);
group.add(subtract);
add(plus);
add(divide);
add(multiply);
add(divide);
add(subtract);
button = new JButton("Calculate");
add(button);
plus.addItemListener(
new ItemListener(){
public void itemStateChanged(ItemEvent event){
str = 1;
System.out.println(str);
}
}
);
divide.addItemListener(
new ItemListener(){
public void itemStateChanged(ItemEvent event){
str = 2;
System.out.println(str);
}
}
);
multiply.addItemListener(
new ItemListener(){
public void itemStateChanged(ItemEvent event){
str = 3;
System.out.println(str);
}
}
);
subtract.addItemListener(
new ItemListener(){
public void itemStateChanged(ItemEvent event){
str = 4;
System.out.println(str);
}
}
);
HandlerClass handler = new HandlerClass();
button.addActionListener(handler);
}
private class HandlerClass implements ActionListener{
public void ActionPerformed(ActionEvent event){
txt = text.getText();
txt2 = text2.getText();
num1 = Integer.parseInt(txt);
num2 = Integer.parseInt(txt2);
if(str==1){
ans=num1+num2;
}
if(str==2){
ans=num1/num2;
}
if(str==3){
ans=num1*num2;
}
if(str==4){
ans=num1-num2;
}
JOptionPane.showMessageDialog(null, String.format("Answer: %s", ans));
}
}
}
如果需要,这是运行它的脚本:
import javax.swing.JFrame;
public class apples {
public static void main(String[] args){
Gui bucky = new Gui();
bucky.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
bucky.setSize(300,150);
bucky.setVisible(true);
}
}
这是我单击“计算”时的错误代码:
线程“AWT-EventQueue-0”java.lang.Error 中的异常:未解决的编译问题:类型 Gui.HandlerClass 必须实现继承的抽象方法 ActionListener.actionPerformed(ActionEvent)
at Gui$HandlerClass.actionPerformed(Gui.java:85)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
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)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown 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)
at java.awt.EventDispatchThread.run(Unknown Source)