- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将计算器 GUI 包装在我构建的类上,以便以不同的方式(不是正常方式,采用逆波兰表示法)计算值。
我只是想知道两件事:
1) 如何编写我的 actionPerformed 方法,以便当您单击按钮或操作时,它会显示在显示屏中
2)我如何开始使用带有计算方法的GUI(例如如何使当您按下GUI上的所有数字或运算符时,它会在我的计算类中注册)。基本上如何将 GUI 包裹在它上面?
**编辑,我编辑了代码,但是编译时出现空指针异常,这是怎么回事?
到目前为止我的 GUI 类:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class GUI extends JFrame implements ActionListener
{
JPanel buttonPanel, topPanel, operationPanel;
JTextField display;
doMath math = new doMath();
JButton Num1;
JButton Num2;
JButton Num3;
JButton Num4;
JButton Num5;
JButton Num6;
JButton Num7;
JButton Num8;
JButton Num9;
JButton Num0;
JButton Add;
JButton Sub;
JButton Mult;
JButton Div;
JButton Eq;
JButton Clr;
public GUI()
{
super("Calculator");
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout (2,1));
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4));
buttonPanel.add(Num1 = new JButton("1"));
buttonPanel.add(Num2 = new JButton("2"));
buttonPanel.add(Num3 = new JButton("3"));
buttonPanel.add(Num4 = new JButton("4"));
buttonPanel.add(Num5 = new JButton("5"));
buttonPanel.add(Num6 = new JButton("6"));
buttonPanel.add(Num7 = new JButton("7"));
buttonPanel.add(Num8 = new JButton("8"));
buttonPanel.add(Num9 = new JButton("9"));
buttonPanel.add(Num0 = new JButton("0"));
buttonPanel.add(Clr = new JButton("C"));
buttonPanel.add(Eq = new JButton("="));
buttonPanel.add(Add = new JButton("+"));
buttonPanel.add(Sub = new JButton("-"));
buttonPanel.add(Mult = new JButton("*"));
buttonPanel.add(Div = new JButton("/"));
Num1.addActionListener(this);
Num2.addActionListener(this);
Num3.addActionListener(this);
Num4.addActionListener(this);
Num5.addActionListener(this);
Num6.addActionListener(this);
Num7.addActionListener(this);
Num8.addActionListener(this);
Num9.addActionListener(this);
Num0.addActionListener(this);
Clr.addActionListener(this);
Eq.addActionListener(this);
Add.addActionListener(this);
Sub.addActionListener(this);
Mult.addActionListener(this);
Div.addActionListener(this);
topPanel = new JPanel();
topPanel.setLayout(new FlowLayout());
topPanel.add(new JTextField(20));
//jtfResult.setHorizontalAlignment(JTextField.RIGHT);
// jtfResult.setEditable(false);
add(mainPanel);
mainPanel.add(topPanel, BorderLayout.NORTH);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == Num1)
{
s += "1";
display.setText(s);
}
if(e.getSource() == Num2)
{
s += "2";
display.setText(s);
}
if(e.getSource() == Num3)
{
s += "3";
display.setText(s);
}
if(e.getSource() == Num4)
{
s += "4";
display.setText(s);
}
if(e.getSource() == Num5)
{
s += "5";
display.setText(s);
}
if(e.getSource() == Num6)
{
s += "6";
display.setText(s);
}
if(e.getSource() == Num7)
{
s += "7";
display.setText(s);
}
if(e.getSource() == Num8)
{
s += "8";
display.setText(s);
}
if(e.getSource() == Num9)
{
s += "9";
display.setText(s);
}
if(e.getSource() == Num0)
{
s += "0";
display.setText(s);
}
if(e.getSource() == Add)
{
s += "+";
display.setText(s);
}
if(e.getSource() == Sub)
{
s += "-";
display.setText(s);
}
if(e.getSource() == Mult)
{
s += "*";
display.setText(s);
}
if(e.getSource() == Div)
{
s += "/";
display.setText(s);
}
if(e.getSource() == Eq)
{
String result = "" + math.doMath1(s);
display.setText(result);
}
}
}
我的计算类(按预期工作,只需要知道如何在其上包装我的 GUI):
public class doMath
{
Stack stack = new Stack();
String next = "";
public doMath()
{
}
public int doMath1(String expr)
{
while( expr.length() >0) //for(int i = 0; i < expr.length(); i++)
{
if(expr.length() == 0)
return 0;
if(expr.indexOf(" ")>0)
{
next = expr.substring(0,expr.indexOf(" "));
if(next.equals("+"))
stack.push(stack.pop() + stack.pop());
else if(next.equals("-"))
{
int x = stack.pop();
stack.push(stack.pop()- x);
}
else if(next.equals("*"))
stack.push(stack.pop() * stack.pop());
else if(next.equals("/"))
{
int x = stack.pop();
stack.push(stack.pop()/ stack.pop());
}
else
stack.push(new Integer(Integer.parseInt(next)));
expr = expr.substring(expr.indexOf(" ")+1);
}
}
return stack.pop();
}
}
错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at GUI.actionPerformed(GUI.java:126)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.
mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:5517)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3129)
at java.awt.Component.processEvent(Component.java:5282)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3984)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3819)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1791)
at java.awt.Component.dispatchEvent(Component.java:3819)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy
(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy
(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
最佳答案
从您的代码来看,您似乎有一个可行的模型。
基本上,您需要将一个 ActionListener
附加到每个按钮。在您的情况下,我将使用同一监听器的单个实例...
ActionListener calculatorActionHandler = new CalculatorActionHandler();
Num1.addActionListener(calculatorActionHandler);
在此处理程序中,我将获取按钮的文本并将它们连接在一起
public class CalculatorActionHandler implements ActionListener {
private StringBuilder expression = new StringBuilder(32);
private Gui gui;
public CalculatorActionHandler(Gui gui) {
this.gui = gui;
}
public void actionPerformed(ActionEvent e) {
JButton source = (JButton)e.getSource();
String text = source.getText();
if (text.equals("=")) {
doMath math = new doMath();
int result = math.doMath1(expression.toString());
expession = new StringBuilder(32);
// Update the UI
} else {
expession.append(text);
// Update this value to the screen, maybe using a JLabel
}
}
}
关于java - 计算器 GUI ActionPerformed 方法,并包装另一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13281971/
假设我有一个带有 SelectPieceListener 的“选择”JButton。我想检查是否再次执行该操作(单击选择按钮)。但是,actionPerformed 是 void,所以我不能将它用作
我有一个关于 Swing Timer 的问题,特别是如何多次调用 actionPerformed 并等待最后一个 actionPerformed 完成后再执行它。我知道其他人以前也问过这个问题,但我发
所以我读了this了解事件源、事件对象和事件处理程序及其实现,如下所示: aButton.addActionListener(new ActionAdapter() { public void
我不明白为什么 jcombobox4ActionPerformed 中的代码在我没有单击任何内容的情况下就被执行了。程序一执行,就会出现“已执行”消息。有什么问题吗? public class Mai
我创建了一个代码,每当您单击 jmenuitem New Game int x 时,其值都会为 1,但遗憾的是它不会返回任何值 package sprite; import java.
我想在与按钮关联的 ActionPerformed 方法内执行一个方法,但不起作用。我研究了很多时间,但无法检测到错误。 这是我的代码: /** * Método que crea un nuevo
我要感谢 Andrew Thompson 帮助我完成了代码。如何访问每个按钮的 actionPerformed 监听器? 代码应该根据您按下的按钮来移动屏幕上的“球”。 import javax.sw
我希望每次单击按钮“bouton”时都执行该功能 boutonPane.Panel2(h, ....) 应该显示 h 个圆圈。所以我想要 2 个,然后 3 个,然后 4 个,然后 5 个......圆
这是一个正在 build 中的平开窗。问题出在一个 Action 中。当我对“打开”按钮执行操作时,我调用一个方法 OpenFile()。 此方法似乎一直有效,直到最后抛出 UnsupportedOp
我有三个不同的类,每个类都是一个单独的 JFrame,具有自己的 GUI,并且每个类都执行不同的操作。在另一个类中,我有我的 Keypad 类,上面所有这些类都包含该类。 这是我的 Keypad 类中
这段代码有一点问题。 actionPerformed 方法不起作用。当我按下按钮时,按钮 knappStartSalg 和 knappStartKunde 没有反应。 所有应该导入的内容都已导入。 非
` public void actionPerformed(ActionEvent e) { if (e.getSource() == saleButton)
好的,我正在为我的学校项目开发 JApplet。我想要它做的是每次单击 JButton(“菜单按钮”)时,它都会删除容器的当前内容,然后向容器添加一个新的 JApplet。我已经可以正常工作了,我遇到
我正在尝试创建一个用于导入 CSV 文件的按钮,但出现此错误: actionPerformed(java.awt.event.ActionEvent) in cannot implement
我是一名 Java 初学者,现在当我开始使用接口(interface)时,我想知道到底发生了什么。我认为 ActionListener 接口(interface)就是一个很好的例子。 我对接口(int
这里是控件的初始化。 public void init(){ ... c = new JComboBox(); .... c.addActionListener(thi
我正在尝试将 RedSquare 类的对象添加到 CatchMeV2 类的 JFrame 中。有什么问题? public class CatchMeV2 implements ActionListen
我试图更好地理解 Java 中的 GUI 编程,但出现了一些奇怪的行为。该程序显示一个带有随机颜色渐变的圆圈,当您单击该按钮时,颜色会发生变化。很简单,除了我注意到当我不小心展开窗口时,触发了 act
actionPerformed 执行后会发生什么? actionPerformed 执行后,有什么方法可以回到程序的主类吗? 这是代码示例: public final class JavaGame e
我正在尝试使用按钮来控制在窗口上执行的操作。为此,我试图返回字符串以在整个程序中发出命令。到目前为止,我设法让这段代码工作: @Override public void actionPerformed
我是一名优秀的程序员,十分优秀!