18f=new Frame("My Frame");
19
20//对Frame进行基本设置。
21 f.setBounds(300,100,600,500);//对框架的位置和大小进行设置
22 f.setLayout(new FlowLayout(FlowLayout.CENTER,5,5));//设计布局
23
24bt=new Button("My Button");
25
26//将组件添加到Frame中
27 f.add(bt);
28
29//加载一下窗体上的事件
30myEvent();
31
32//显示窗体
33 f.setVisible(true);
34}
35
36private void myEvent()
37{
38 f.addWindowListener(new WindowAdapter()//窗口监听
39{
40public void windowClosing(WindowEvent e)
41{
42System.out.println("窗体执行关闭!");
43System.exit(0);
44}
45});
46//让按钮具备关闭窗口的功能
47bt.addActionListener(new ActionListener()
48{
49public void actionPerformed(ActionEvent e)
50{
51System.out.println("按钮执行关闭窗口的功能"); 52System.exit(0);
53}
54});