package testCheckbox;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class testCheckbox extends JFrame {
String number[]={“1-HL1000052”,”2-HL1000054”,”3-HL1000059”,”4-HL1000059”};
Checkbox machineCb;//复选框
Checkbox chooseCb;//选的值
String[] temp;//临时分割
String machineName=”继电器”;
CheckboxGroup cg=new CheckboxGroup();
CbItemListener cbi=new CbItemListener();
public testCheckbox(){
//add
// FlowLayout flw=new FlowLayout();
// setLayout(flw);
//end add
setLayout(new GridLayout(0,18,8,8)); //设置为网格布局,未指定行数
for(int i=0;i<number.length;i++){
temp=number[i].split("-");
machineCb=new Checkbox(machineName+temp[0]);
getContentPane().add(machineCb);
getContentPane().add(new JLabel("------------"));
for(int k=0;k<16;k++){
chooseCb=new Checkbox(""+k);
getContentPane().add(chooseCb);
}
machineCb.addItemListener(cbi);
}//for
getContentPane().add(new JButton("确定连接并发送"));
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
});
}//testCheckbox
/**
* 实现checkbox的操作
*/
class CbItemListener implements ItemListener{
public void itemStateChanged(ItemEvent e){
//cb1.setLabel("fddffd");
Checkbox cb=(Checkbox)e.getItemSelectable();
if(cb.getLabel().equals("你好吗?")){
if(cb.getState()==true){
System.out.println("选中“你好吗?”");
}else{
System.out.println("未选中“你好吗?”");
}
}else {
Checkbox cbx=cg.getSelectedCheckbox();
if(cbx!=null){
System.out.println(cbx.getLabel());
}//if
}//if
}//itemStateChanged
}//CbItemListener
public static void main(String[] args) {
// TODO Auto-generated method stub
testCheckbox dw=new testCheckbox();
dw.setSize(400, 300);
dw.setTitle("test");
dw.pack();
dw.setVisible(true);
}//main
}//testCheckbox