gpt4 book ai didi

Java:Joptionpane 消息对话框未打开

转载 作者:行者123 更新时间:2023-11-29 03:45:59 26 4
gpt4 key购买 nike

我决定制作一个简洁的小计算器程序,但现在,当我按下计算并运行计算脚本时,它不会打开一个说明答案的框。我找不到问题所在。谁能告诉我一下?

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: Unresolved 编译问题: 类型 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)

最佳答案

您正在尝试运行不可编译的代码——不要这样做!

而是在编译时修复所有编译问题,然后仅在代码编译良好时运行。

首先:您需要正确地将 actionPerformed(...) 大写。

下一步:您的 JRadioButton 上不应该有 任何 监听器。而是检查仅在主要计算 JButton 的 ActionListener 中按下了哪个按钮。原因是您不关心用户是否选择了一个 JRadioButton 然后又改变了主意并选择了另一个,而是您只关心在按下计算 JButton 时选择了哪个 JRadioButton。

关于Java:Joptionpane 消息对话框未打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10864815/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com