CheckBoxes 在我设置启用(false)后没有被禁用
JCheckBoxes not being disabled after I setEnable(false) them
如果有 30 个复选框,我不想选中其中的 6 个以上。我有一个 int 跟踪检查了多少。每个盒子都有一个项目侦听器,它调用一个方法来查看 int 是什么,如果它达到 6,该方法将在每个盒子上调用 setEnabled(false)。
然而,即使在 int number_of_boxes_checked 达到 6 之后,框也不会被禁用。
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JCheckBox;
import java.awt.GridLayout;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JLabel;
public class JLottery2 extends JFrame {
private JPanel contentPane;
int number_of_boxes_checked = 1;
JCheckBox[] checkBox = new JCheckBox[30];
JLabel label = new JLabel("0");
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JLottery2 frame = new JLottery2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public JLottery2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 586, 360);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(10, 11, 321, 299);
contentPane.add(panel);
panel.setLayout(new GridLayout(5, 6, 0, 0));
JPanel panel_1 = new JPanel();
panel_1.setBounds(330, 11, 230, 299);
contentPane.add(panel_1);
panel_1.add(label);
for (int i = 0; i < 30; ++i) {
checkBox[i] = new JCheckBox(Integer.toString(i + 1));
panel.add(checkBox[i]);
}
for (int i = 0; i < 30; ++i) {
checkBox[i].addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
limit_checkBoxes();
}
});
}
}
public void limit_checkBoxes() {
label.setText(Integer.toString(number_of_boxes_checked));
if (number_of_boxes_checked > 6) {
for (int i = 0; i > 30; ++i) {
checkBox[i].setEnabled(false);
}
} else {
++number_of_boxes_checked;
}
}
}
在您的 limit_checkBoxes()
方法中,您希望在循环中使用 i < 30
,而不是 i > 30
。请参阅我的嵌入式评论。
label.setText(Integer.toString(number_of_boxes_checked));
if (number_of_boxes_checked > 6) {
for (int i = 0; i < 30; ++i) { //<- your bug was here, I fixed it
checkBox[i].setEnabled(false);
}
} else {
++number_of_boxes_checked;
}
将行:for (int i = 0; i > 30; ++i) {
更改为 for (int i = 0; i < 30; i++) {
如果有 30 个复选框,我不想选中其中的 6 个以上。我有一个 int 跟踪检查了多少。每个盒子都有一个项目侦听器,它调用一个方法来查看 int 是什么,如果它达到 6,该方法将在每个盒子上调用 setEnabled(false)。 然而,即使在 int number_of_boxes_checked 达到 6 之后,框也不会被禁用。
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JCheckBox;
import java.awt.GridLayout;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JLabel;
public class JLottery2 extends JFrame {
private JPanel contentPane;
int number_of_boxes_checked = 1;
JCheckBox[] checkBox = new JCheckBox[30];
JLabel label = new JLabel("0");
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JLottery2 frame = new JLottery2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public JLottery2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 586, 360);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(10, 11, 321, 299);
contentPane.add(panel);
panel.setLayout(new GridLayout(5, 6, 0, 0));
JPanel panel_1 = new JPanel();
panel_1.setBounds(330, 11, 230, 299);
contentPane.add(panel_1);
panel_1.add(label);
for (int i = 0; i < 30; ++i) {
checkBox[i] = new JCheckBox(Integer.toString(i + 1));
panel.add(checkBox[i]);
}
for (int i = 0; i < 30; ++i) {
checkBox[i].addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
limit_checkBoxes();
}
});
}
}
public void limit_checkBoxes() {
label.setText(Integer.toString(number_of_boxes_checked));
if (number_of_boxes_checked > 6) {
for (int i = 0; i > 30; ++i) {
checkBox[i].setEnabled(false);
}
} else {
++number_of_boxes_checked;
}
}
}
在您的 limit_checkBoxes()
方法中,您希望在循环中使用 i < 30
,而不是 i > 30
。请参阅我的嵌入式评论。
label.setText(Integer.toString(number_of_boxes_checked));
if (number_of_boxes_checked > 6) {
for (int i = 0; i < 30; ++i) { //<- your bug was here, I fixed it
checkBox[i].setEnabled(false);
}
} else {
++number_of_boxes_checked;
}
将行:for (int i = 0; i > 30; ++i) {
更改为 for (int i = 0; i < 30; i++) {