Java 如何知道是否在GUI中检查了Jcheckbox
您能详细告诉我如何知道jcheckbox是否已检查?方法isSelected对我不起作用,它在运行时给了我一个异常Java 如何知道是否在GUI中检查了Jcheckbox,java,user-interface,checkbox,jgrasp,Java,User Interface,Checkbox,Jgrasp,您能详细告诉我如何知道jcheckbox是否已检查?方法isSelected对我不起作用,它在运行时给了我一个异常 { Sandwich = new JButton("Tall"); contentPane.add(Tall); Sandwitch.setBounds(350, 110, 90,40); //in main Sandwitch.addActionListener(this); } ..... public void
{
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()){//
....................
}
它在运行时会出现很长的异常
{
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()){//
....................
}
你能帮我解决这个问题吗?不要用
==
来比较字符串
if (clickedButton.getText()=="Sandwitch"){}
使用equals
或equalsIgnoreCase()
异常是什么?异常是怎么说的…空指针???非常长的异常是什么意思?如果不知道您得到的异常,没有人可以帮助您。可能是-1的副本,这不会删除OP的异常。这无助于我清除异常。@user3049389您从未发布过异常。我们怎么知道例外情况是什么?考虑发布一个更多的帮助。你在空指针上发布了同样的问题,可能是因为一些对象(可能是复选框本身)还没有被启动…