落叶无神
发贴: 2
积分: 0
|
于 2010-10-28 10:09
假设在我设了十个JCheckBox,我该怎样才能这些checkbox的index public CheckBoxJFrame() { super("Check Box"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(600, 500); JPanel selectarea = new JPanel(); selectarea.setMaximumSize(new Dimension(500, 300)); selectarea.setPreferredSize(new Dimension(500, 300)); //selectarea.setBorder(BorderFactory.createLoweredBevelBorder()); selectarea.setLayout(new GridLayout(7, 7, 5, 5));
num = new JCheckBox[50]; for (index = 1; index < num.length; index++) { num[index] = new JCheckBox("" + index); selectarea.add(num[index]); num[index].addItemListener((ItemListener) this); } //button area JPanel butarea = new JPanel(); butarea.setLayout(new FlowLayout()); butarea.setMaximumSize(new Dimension(500, 100)); butarea.setPreferredSize(new Dimension(500, 100));
butsubmit = new JButton("Submit"); butcheck = new JButton("Check"); txtwinnum = new JTextField(10); butarea.add(butsubmit); butarea.add(butcheck); butarea.add(txtwinnum);
JPanel container = new JPanel(); container.setLayout(new FlowLayout()); container.add(selectarea); container.add(butarea); add(container); setVisible(true);
}
public void itemStateChanged(ItemEvent e) { int count = 0; int n,Index = 1; for (n = 1; n < num.length; n++) { if (e.getStateChange() == ItemEvent.SELECTED) { } if(count > 5) break; } txtwinnum.setText(txtwinnum.getText() + "," + ); // num[n].stSelected(true); }
在上面的code中,如果checkbox的isselected的数量超过六,那最后的checkbox 必须被deselected,但是我没法做到。请诸位指教!
|