JCheckbox isSelected() returns 即使未选中复选框也为真

JCheckbox isSelected() returns true even if the checkbox is not checked

我在使用 JCheckboxes 时遇到了问题,尽管我以前经常使用它们。

基本上,我创建了一个非常简单的 window 复选框,然后检查它们是否被选中。进行该检查时,即使未选中,它也会显示 JCheckbox。这是我的代码。要重现我的问题,运行 项目,然后单击开始。即使 createStatisticsFilesCheckBox 设置为未选中,从构造函数中检查它是否在 ActionListener 方法 returns 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);
    }

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

你有一个尾随的“;”在结束 if 语句的所有 if 语句上。

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

去掉“;”。

代码工作正常 returns 如果未选中复选框,则为 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);

}
}