我如何知道是否在GUI中选中了Jcheckbox? [英] How do i know if a Jcheckbox is checked?in GUI

查看:141
本文介绍了我如何知道是否在GUI中选中了Jcheckbox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能告诉我详细信息我怎么知道一个jcheckbox是否被选中?方法isSelected不能与我一起工作,它在运行时给我一个异常

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

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

在运行

时会出现很长的异常,你能帮我解决这个问题吗? / p>

can you please help me with this problem?

推荐答案

不要使用 == 来比较字符串! >

Don't use == to compare Strings!

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

使用等于 equalsIgnoreCase()

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

这篇关于我如何知道是否在GUI中选中了Jcheckbox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆