你能详细告诉我如何知道是否检查了jcheckbox?方法isSelected对我不起作用,它在运行时给我一个例外
{
Sandwich = new JButton("Tall");
contentPane.add(Tall);
Sandwitch.setBounds(350, 110, 90,40); //in main
Sandwitch.addActionListener(this);
}
.....
public void actionPerformed(ActionEvent event) {
JButton clickedButton = (JButton) event.getSource();
String buttonText = clickedButton.getText();
..........
if(clickedButton.getText()=="Sandwitch"){
if(Ketchup.getState()&&!Garlic.getState()){//
itm=new Item(""+m+clickedButton.getText(),3.0);
xyz.addItem(itm);
textArea.append(" "+clickedButton.getText()+",");
textArea.append(" "+itm.getPrice()+"\n");}
else if(!Ketchup.isSelected()&&Garlic.isSelected()){//
....................
}
运行
时会出现很长的异常 你可以帮我解决这个问题吗?答案 0 :(得分:2)
请勿使用==
来比较字符串!
if (clickedButton.getText()=="Sandwitch"){}
使用equals
或equalsIgnoreCase()
if ("Sandwich".equalsIgnoreCase(clickedButton.getText()){
// do something
}