试图创建动作侦听器来执行乘法任务。它似乎有效,但它似乎忽略了我输入的最后一个数字。我一直在想,我需要一个第二个变量来跟踪最后一个命令,这样当按下“相等”按钮时,它就会将当前的数字添加到前面的命令中。但是,如果按下相等的按钮,我就不用去执行任务了?
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();
}
}
}发布于 2011-08-05 05:04:26
附带注意:一般来说,为每个按钮创建一个单独的监听器要比尝试共享一个侦听器要容易。当您创建一个侦听器时,首先要将几个按钮放入一个函数,然后要求该函数将它们分开。为什么不把它们分开呢?如果有公共代码,请调用公共函数。
但对于您的问题:如果所有运算符都有相同的优先级,那么只需保存运算符。如:
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,则需要创建一个堆栈并保存操作符、临时中间值和一些其他复杂性。
发布于 2011-08-05 05:39:52
在JTextField中存储操作数时,可以方便地侦听property changes、document changes或focus changes,以便决定采取什么操作。
https://stackoverflow.com/questions/6951462
复制相似问题