以下是我的窗口事件处理程序:
1 package WindowBuilder; 2 import java.awt.*; 3 import java.awt.event.*; 4 import javax.swing.*; 5 public class EventHandling extends JFrame 6 { 7 public EventHandling() 8 { 9 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);10 addWindowListener //设置窗口大小为屏幕的0.8倍11 (12 new WindowAdapter() //窗口适配器,可以只重写自己所需的方法13 {14 public void windowOpened(WindowEvent e)15 {16 Toolkit toolkit=getToolkit();17 Dimension screenSize=toolkit.getScreenSize();18 int width=(int)(screenSize.width*0.8);19 int height=(int)(screenSize.height*0.8);20 setSize(width,height); 21 }22 }23 );24 setLocation(100,100); 25 setVisible(true);26 JPanel panel=new JPanel();27 setContentPane(panel);28 panel.setLayout(null);29 }30 public static void main(String[] args) 31 {32 new EventHandling();33 }34 }
运行结果如下: