import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
// 复选框
public class JCheckBoxDemo extends JFrame {
public JCheckBoxDemo() {
this.setTitle("JCheckBoxDemo");
this.setSize(300, 300);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new GridLayout(2,1));
JCheckBox jc1 = new JCheckBox("Hello");
JCheckBox jc2 = new JCheckBox("World");
JTextArea txt = new JTextArea();
JScrollPane js = new JScrollPane(txt);
this.add(js);
JPanel jp = new JPanel();
jp.add(jc1);
jp.add(jc2);
this.add(jp);
this.setVisible(true);
jc1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
txt.setText("");
if (jc1.isSelected())
txt.append(jc1.getText() + "\n");
}
});
jc2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
txt.setText("");
if (jc2.isSelected())
txt.append(jc2.getText() + "\n");
}
});
}
public static void main(String[] args) {
new JCheckBoxDemo();
}
}