Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何知道是否在GUI中检查了Jcheckbox_Java_User Interface_Checkbox_Jgrasp - Fatal编程技术网

Java 如何知道是否在GUI中检查了Jcheckbox

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

您能详细告诉我如何知道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()){//

....................
}
它在运行时会出现很长的异常

{
 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您从未发布过异常。我们怎么知道例外情况是什么?考虑发布一个更多的帮助。你在空指针上发布了同样的问题,可能是因为一些对象(可能是复选框本身)还没有被启动…