java 即使未选中复选框,JCheckbox isSelected() 也会返回 true
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34500043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JCheckbox isSelected() returns true even if the checkbox is not checked
提问by Benoit Goderre
I am having trouble with JCheckbox
es, although I have used them often before.
我在使用JCheckbox
es时遇到了麻烦,尽管我以前经常使用它们。
Basically, I create a very simple window with check boxes, then check if they are selected. When doing that check, it displays the JCheckbox
as selected, even if it is not. Here is my code. To reproduce my issue, run the project, then click Start. Even if the createStatisticsFilesCheckBox
is set as not selected, from the constructor, checking if it is selected in the ActionListener
method returns true
. Thanks in advance!
基本上,我创建了一个非常简单的带有复选框的窗口,然后检查它们是否被选中。进行该检查时,它会显示JCheckbox
为已选择,即使不是。这是我的代码。要重现我的问题,请运行该项目,然后单击“开始”。即使createStatisticsFilesCheckBox
被设置为未选择,从构造函数中,检查它是否在ActionListener
方法中被选择返回true
。提前致谢!
package Queries;
包查询;
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);
}
}
回答by camickr
if (createStatisticsFilesCheckBox.isSelected() == true);
You have a trailing ";" on all your if statements which ends the if statement.
你有一个尾随的“;” 在所有结束 if 语句的 if 语句上。
Therefore every statement after the if statement is executed unconditionally.
因此,if 语句之后的每个语句都无条件地执行。
Get rid of the ";".
去掉“;”。
回答by Shady Watson
The Code is working fine it returns false if checkbox is not selected try copy pasting it when i copied yours it had 3 closing brackets missing, Let me know if it is working for you.
代码工作正常,如果未选中复选框,则返回 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);
}
}