当前位置:  开发笔记 > 编程语言 > 正文

即使未选中复选框,JCheckbox isSelected()也会返回true

如何解决《即使未选中复选框,JCheckboxisSelected()也会返回true》经验,为你挑选了1个好方法。

JCheckbox虽然我以前经常使用它们,但我遇到了问题.

基本上,我创建一个带有复选框的非常简单的窗口,然后检查它们是否被选中.执行该检查时,JCheckbox即使不是,也会显示为已选中.这是我的代码.要重现我的问题,请运行该项目,然后单击"开始".即使createStatisticsFilesCheckBox设置为未选中,也要从构造函数中检查是否在ActionListener方法中选中它true.提前致谢!

包查询;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test extends Thread implements ActionListener 
{
    private JButton cancelButton, backButton, startButton;
    private JCheckBox createQueriesCheckBox,createStatisticsFilesCheckBox, createPokerHandIDFilesCheckBox;
    private JFrame frame;

    public void actionPerformed(ActionEvent e)
    {
        if ("Start".equals(e.getActionCommand()))
        {
            if (createQueriesCheckBox.isSelected() == true);
            {
                // Code here
            }

            if (createStatisticsFilesCheckBox.isSelected() == true);
            {
                // Code here
                // Always show as selected
                System.out.println("Test");
            }

            if (createPokerHandIDFilesCheckBox.isSelected() == true);
            {
                // Code here
            }

            start();
        }
        else if ("Back".equals(e.getActionCommand()))
        {
            // Code here
        }
        else if ("Cancel".equals(e.getActionCommand()))
        {
            System.exit(0);
        }
    }

    public Test()
    {       
        JPanel mainPanel = new JPanel(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();

        gbc.insets = new Insets(4, 4, 4, 4);
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridheight = 1;
        gbc.gridwidth = 1;

        mainPanel.add(checkBoxesPanel(), gbc);

        gbc.gridy++;

        mainPanel.add(buttonsPanel(), gbc);

        frame = new JFrame("Actions");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(mainPanel);
        frame.setVisible(true);
        frame.pack();
    }

    /**
     * Panel that contains the Cancel and Continue buttons
     * 
     * @return panel
     */
    public JPanel buttonsPanel()
    {
        JPanel buttonsPanel = new JPanel(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();

        gbc.insets = new Insets(10, 10, 10, 10);
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridheight = 1;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.EAST;

        cancelButton = new JButton("Cancel");
        cancelButton.addActionListener(this);

        buttonsPanel.add(cancelButton, gbc);

        backButton = new JButton("Back");
        backButton.addActionListener(this);

        gbc.gridx++;

        buttonsPanel.add(backButton, gbc);

        startButton = new JButton("Start");
        startButton.addActionListener(this);

        gbc.gridx++;

        buttonsPanel.add(startButton, gbc);

        return buttonsPanel;
    }

    /**
     * Panel that contains the check boxes for the types of queries
     * 
     * @return panel
     */
    public JPanel checkBoxesPanel()
    {
        JPanel checkBoxesPanel = new JPanel(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();

        gbc.insets = new Insets(4, 4, 4, 4);        

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridheight = 1;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.WEST;       

        createQueriesCheckBox = new JCheckBox("Create queries", true);

        checkBoxesPanel.add(createQueriesCheckBox, gbc);

        createStatisticsFilesCheckBox = new JCheckBox("Create statistics files", false);

        gbc.gridy++;
        checkBoxesPanel.add(createStatisticsFilesCheckBox, gbc);

        createPokerHandIDFilesCheckBox = new JCheckBox("Create PokerHandID files", false);

        gbc.gridy++;
        checkBoxesPanel.add(createPokerHandIDFilesCheckBox, gbc);

        return checkBoxesPanel;
    }

    public static void main(String[] args)
    {
        new Test();
    }

    public void run()
    {       
        System.exit(0);
    }

}

camickr.. 5

if (createStatisticsFilesCheckBox.isSelected() == true);

你有一个尾随";" 在所有结束if语句的if语句中.

因此,if语句之后的每个语句都是无条件执行的.

摆脱";".



1> camickr..:
if (createStatisticsFilesCheckBox.isSelected() == true);

你有一个尾随";" 在所有结束if语句的if语句中.

因此,if语句之后的每个语句都是无条件执行的.

摆脱";".

推荐阅读
勤奋的瞌睡猪_715
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有