How do i know if a Jcheckbox is checked?in GUI [closed]

偶尔善良 提交于 2019-12-13 08:17:27

问题


can you tell me in details how do i know if a jcheckbox is checked or not? method isSelected didn't work with me it gives me an exception while running

{
 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()){//

....................
}

it gives a very long exception while running

can you please help me with this problem?


回答1:


Don't use == to compare Strings!

if (clickedButton.getText()=="Sandwitch"){}

Use equals or equalsIgnoreCase()

if ("Sandwich".equalsIgnoreCase(clickedButton.getText()){
    // do something
}


来源:https://stackoverflow.com/questions/20286460/how-do-i-know-if-a-jcheckbox-is-checkedin-gui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!