gpt4 book ai didi

Java Action 监听器

转载 作者:行者123 更新时间:2023-11-30 05:00:22 25 4
gpt4 key购买 nike

尝试创建操作监听器来执行乘法任务。它似乎有效,但它似乎忽略了我输入的最后一个数字。我一直在想我需要第二个变量来跟踪最后一个命令,这样当按下等于按钮时,它将当前数字添加到之前的数字中。但如果按下等号按钮,我就不必执行该任务了吗?

class ButtonListener implements ActionListener {        
public void actionPerformed(ActionEvent e) {
String numbers = e.getActionCommand();
if (begin) {
textField.setText(numbers);
begin = false;
}

else if (numbers.equals("C")) {
textField.setText("0.0");
action.reset();
}

else
textField.setText(textField.getText() + numbers);
}
}

class OperatorListener implements ActionListener {

public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
String text = textField.getText();

if (begin) {
textField.setText("0.0");
action.setTotal("0.0");
}
else {
if (command.equals("+")) {
action.add(text);
begin = true;
}
else if (command.equals("=")) {
textField.setText(text);
System.out.println(action.getTotal());
}
textField.setText(action.getTotal());
}
}
}

变量的一些解释。 begin 只是检查 JTextField 的当前状态是否为空。 action 只是“添加”的方法,它还有我希望它执行的其他调用。

关于如何跟踪最后一个命令或绕过它以免忽略最后一个数字有什么建议吗?这些任务与计算器的任务类似。

<小时/>

编辑:对于那些感兴趣的人,这就是我的最终结果。

class ButtonListener implements ActionListener {        
public void actionPerformed(ActionEvent e) {
String numbers = e.getActionCommand();

// check if a number has been pressed
if (begin) {
textField.setText(numbers);
begin = false;
}

// if clear is checked
else if (numbers.equals("C")) {
action.reset();
begin = true;
operator = "=";
textField.setText("0.0");
}

// Once a number is pressed keep adding it to the text field until an operator is pressed

else
textField.setText(textField.getText() + numbers);
}
}

/**
* Action listener for Operators
*/

class OperatorListener implements ActionListener {

public void actionPerformed(ActionEvent e) {
// String command = e.getActionCommand();

// if nothing has been pressed
if (begin) {
textField.setText("0.0");
begin = false;

// else begin performing operation pressed
} else {
begin = true;
String text = textField.getText();

// right away add the value of the text field
if (operator.equals("=")) {
action.setTotal(text);
}
else if (operator.equals("+")) {
action.add(text);
}
// and so on for all the other operators.

textField.setText("" + action.getTotal());

// now capture the operator pressed.
operator = e.getActionCommand();
}
}
}

最佳答案

旁注:一般来说,为每个按钮创建一个单独的监听器比尝试共享监听器更容易。当您创建一个监听器时,您首先将多个按钮集中到一个函数,然后要求该函数将它们分开。为什么不一开始就把它们分开呢?如果有公共(public)代码,就调用公共(public)函数。

但是对于你的问题:如果所有运算符具有相同的优先级,那么只需保存运算符即可。如:

class Calculator
{
// Well, this would probably better be an enum, but whatever.
public char operator=' ';
public float lastNum;

public void calc()
{
try
{
double currNum=Double.parseDouble(numfield.getText());
if (operator==' ')
lastNum=currNum;
else if (operator=='+')
lastNum+=currNum;
else if (operator=='-')
lastNum-=currNum;
else if (operator=='*')
lastNum*=currNum;
else if (operator=='/')
lastNum/=currNum;
}
catch (NumberFormatException panic)
{
... whatever error handling ...
}
}
}
class OperatorListener implements ActionListener
{
Calculator calc;
public OpeatorListener(Calculator calc)
{
this.calc=calc;
}
public abstract void actionPerformed(ActionEvent e);
}
class PlusListener extends OperatorListener
{
public void actionPeformed(ActionEvent e)
{
calc.calc();
calc.operator='+';
}
}
class MinusListener extends OperatorListener
{
public void actionPerformed(ActionEvent e)
{
calc.calc();
calc.operator='-';
}
}
class EqualListener extends OperatorListener
{
public void actionPerformed(ActionEvent e)
{
calc.calc();
}
}

此代码为所有运算符提供相同的优先级,例如“2+3*5=”将给出 25。

如果您希望乘法和除法具有更高的优先级,例如“2+3*5=”给出 17,您需要创建一个堆栈并保存运算符和临时中间值以及其他一些复杂性。

关于Java Action 监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6951462/

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