我在使用 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);
}
}
最佳答案
if (createStatisticsFilesCheckBox.isSelected() == true);
你有一个尾随的“;”在结束 if 语句的所有 if 语句上。
因此if语句之后的每一条语句都是无条件执行的。
去掉“;”。
关于java - JCheckbox isSelected() 即使复选框未被选中也返回 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34500043/