java CheckBox用法

public class TestCheckBox extends JFrame {


public TestCheckBox() {

setLayout(new BorderLayout());

//声明JTextArea内部占3行10列
final JTextArea jt = new JTextArea(3, 10);
JScrollPane jsp = new JScrollPane(jt);
// jp1.add(jsp);
getContentPane().add(jsp, BorderLayout.NORTH);

JPanel jp1 = new JPanel();
final JCheckBox jc1 = new JCheckBox("1");
final JCheckBox jc2 = new JCheckBox("2");
final JCheckBox jc3 = new JCheckBox("3");

jp1.add(jc1);
jp1.add(jc2);
jp1.add(jc3);

jc1.addItemListener(new ItemListener() {

@Override
public void itemStateChanged(ItemEvent e) {
if (jc1.isSelected()) {
jt.append("JCheckBox 1 is selected\n");
}
}
});

jc2.addItemListener(new ItemListener() {

@Override
public void itemStateChanged(ItemEvent e) {
if (jc2.isSelected()) {
jt.append("JCheckBox 2 is selected\n");
}
}
});

jc3.addItemListener(new ItemListener() {

@Override
public void itemStateChanged(ItemEvent e) {


if (jc3.isSelected()) {
jt.append("JCheckBox 3 is selected\n");
}
}
});

getContentPane().add(jp1, BorderLayout.SOUTH);



setSize(300, 200);
setVisible(true);
}

public static void main(String[] args) {
new TestCheckBox();
}


}

你可能感兴趣的:(java)