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

时间:2015-12-28 20:10:24

标签: java swing jcheckbox

我遇到了JCheckbox es的问题,虽然我以前经常使用它们。

基本上,我创建了一个带有复选框的非常简单的窗口,然后检查它们是否被选中。执行该检查时,它会将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);
    }

}

2 个答案:

答案 0 :(得分:5)

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

你有一个尾随&#34 ;;"在所有结束if语句的if语句中。

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

摆脱";"。

答案 1 :(得分:0)

代码工作正常,如果没有选中复选框则返回false尝试复制粘贴它,当我复制它时它有3个关闭括号丢失,让我知道它是否适合你。

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();
       }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;
}

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);

}
}
相关问题