[英]Why can't I compare getActionCommand() with a matching String value?
I am making a calculator as a Java applet. 我正在将计算器作为Java小程序进行。 I have successfully created the layout, as well as registering the actionListener to all of the JButtons. 我已经成功创建了布局,以及将actionListener注册到所有JButton。
So, in my class that implements ActionListener I have this code for all of the number buttons: 因此,在实现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() );
}
...which works correctly. ...可以正常工作。 So, why doesn't this code work: 所以,为什么这段代码行不通:
if(buttonClicked.equals("/") )
{
answer.setText(answer.getText() + e.getActionCommand() );
}
...are we not able to compare Strings with the getActionCommand() String contents? ...我们是否无法将字符串与getActionCommand()字符串内容进行比较? Or am I maybe not importing something that I should? 还是我可能不导入我应该导入的内容?
EDIT -- here is my complete code: 编辑-这是我完整的代码:
`
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
You are attempting to do a ParseInt on a "/". 您正在尝试对“ /”进行ParseInt。 This throws a NumberFormatException. 这将引发NumberFormatException。 Your code never makes it beyond the for loop if the string cannot be converted to an integer. 如果字符串不能转换为整数,则您的代码永远不会超出for循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.