import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class JCheckBoxTest {
JFrame f = null;
@SuppressWarnings("deprecation")
JCheckBoxTest() {
f = new JFrame("复选框示例"); // 创建一个JFrame实例对象
Container contentPane = f.getContentPane(); // 定义一个容器
contentPane.setLayout(new GridLayout(2, 1)); // 设置窗口的布局
JPanel p1 = new JPanel(); // 创建一个面板对象p1
p1.setLayout(new GridLayout(1, 3));
p1.setBorder(BorderFactory.createTitledBorder("选择你喜欢的科目?"));
JCheckBox c1 = new JCheckBox("语文");
JCheckBox c2 = new JCheckBox("数学");
JCheckBox c3 = new JCheckBox("英语");
p1.add(c1);
p1.add(c2);
p1.add(c3);
contentPane.add(p1);
f.pack();
f.show();
f.addWindowListener(new WindowAdapter() { // 创建一个窗口监听器
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String args[]) {
new JCheckBoxTest();
}
}