gpt4 book ai didi

java - 为什么我无法将 getActionCommand() 与匹配的字符串值进行比较?

转载 作者:太空宇宙 更新时间:2023-11-04 08:19:03 26 4
gpt4 key购买 nike

我正在制作一个计算器作为 Java 小程序。我已成功创建布局,并将 actionListener 注册到所有 JButton。

因此,在实现 ActionListener 的类中,我为所有数字按钮提供了以下代码:

String buttonClicked = e.getActionCommand();
// for number buttons
for(int i = 0; i <= 9; i++)
{
if(i == Integer.parseInt(buttonClicked) )
answer.setText(answer.getText() + e.getActionCommand() );
}

...工作正常。那么,为什么这段代码不起作用:

if(buttonClicked.equals("/") )
{
answer.setText(answer.getText() + e.getActionCommand() );
}

...我们是否无法将字符串与 getActionCommand() 字符串内容进行比较?或者我可能没有导入我应该导入的东西?

编辑 - 这是我的完整代码:

` 
import java.awt.*;
import javax.swing.*;
import java.applet.*;
import java.awt.event.*;

public class Part_C extends Applet
{
// answer field here (no panel; JTextField is 100% width without panel)
private JTextField answer = new JTextField();
//answer.setHorizontalAlignment(JTextField.RIGHT); // align input to the right

// JButton array
private JButton[] buttons =
{
// (panel 1) backspace, clear entry, and clear buttons here
new JButton("backspace"),
new JButton("CE"),
new JButton("C"),

// (panel 2, row 1) numbers 7 - 9, divide, and sqare-root buttons here
new JButton("7"),
new JButton("8"),
new JButton("9"),
new JButton("/"),
new JButton("sqrt"),

// (panel 2, row 2) numbers 4 - 6, multiply, and percent buttons here
new JButton("4"),
new JButton("5"),
new JButton("6"),
new JButton("X"),
new JButton("%"),

// (panel 2, row 3) numbers 1 - 3, subtract, and inverse buttons here
new JButton("1"),
new JButton("2"),
new JButton("3"),
new JButton("-"),
new JButton("1/x"),

// (panel 2, row 4) number 0, positive/negative, decimal, addition, and equals buttons here
new JButton("0"),
new JButton("+/-"),
new JButton("."),
new JButton("+"),
new JButton("="),

}; // end of JButton array

// constructor for class Part_C
public Part_C()
{
// first panel for backspace, clear entry, and clear buttons
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(1, 3) );
for(int i = 0; i < 3; i++)
panel1.add(buttons[i] );

// second panel for operators and operands
JPanel panel2 = new JPanel();
panel2.setLayout(new GridLayout(4, 5) );
for(int i = 3; i < buttons.length; i++)
panel2.add(buttons[i] );

// third panel for BorderLayout of answer JTextField, and first 2 panels
JPanel panel3 = new JPanel(new BorderLayout() );
panel3.add(answer, BorderLayout.NORTH);
panel3.add(panel1, BorderLayout.CENTER);
panel3.add(panel2, BorderLayout.SOUTH);
add(panel3); // add panel3 to the JFrame

// action listener created here
ButtonListener listener = new ButtonListener();

// action listener is added to all JButtons here
for (int i = 0; i < buttons.length; i++)
buttons[i].addActionListener(listener);
}

/* overriding the init() method of Applet;
* ...in an Applet, init() is used instead
* of the "main" method */
public void init()
{
// create JFrame
Part_C frame = new Part_C();

// the applet stops when the window / tab is closed
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

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

// for number buttons
for(int i = 0; i <= 9; i++)
{
if(i == Integer.parseInt(buttonClicked) )
answer.setText(answer.getText() + e.getActionCommand() );
}

// for operators
if(buttonClicked.equals("/") )
{
answer.setText(answer.getText() + e.getActionCommand() );
}
else if(buttonClicked.equals("sqrt") )
{
answer.setText(answer.getText() + e.getActionCommand() );
}
else if(buttonClicked.equals("X") )
{
answer.setText(answer.getText() + e.getActionCommand() );
}
else if(buttonClicked.equals("%") )
{
answer.setText(e.getActionCommand() );
}
else if(buttonClicked.equals("-") )
{
answer.setText(answer.getText() + e.getActionCommand() );
}
else if(buttonClicked.equals("1/x") )
{
answer.setText(e.getActionCommand() );
}
else if(buttonClicked.equals("+/-") )
{
answer.setText(e.getActionCommand() );
}
else if(buttonClicked.equals(".") )
{
answer.setText(answer.getText() + e.getActionCommand() );
}
else if(buttonClicked.equals("+") )
{
answer.setText(e.getActionCommand() );
}

// for data-clearing operations
if(buttonClicked.equals("backspace") )
{
answer.setText(answer.getText().substring(0, answer.getText().length() - 1) );
}
else if(buttonClicked.equals("CE") )
{

}
else if(buttonClicked.equals("C") )
{
answer.setText("");
}

// for the equals button
if(buttonClicked.equals("=") )
{

}
}
} // end of inner class buttonListener
} // end of Part_C

最佳答案

您正在尝试对“/”执行 ParseInt。这会引发 NumberFormatException。如果字符串无法转换为整数,则您的代码永远不会超出 for 循环。

关于java - 为什么我无法将 getActionCommand() 与匹配的字符串值进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9911983/

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