1 package ; 2 3 import ; 4 import ; 5 import ; 6 import ; 7 import ; 8 9 10 public class MyFrame extends Frame {11 12 //加载窗口13 public void launchFrame() {14 setSize(, ); //设置窗口大小15 setLocation(100, 100); //设置左上角坐标,开始位置, 也就是窗口开始位置16 setVisible(true); //设置为可见(默认为不可见)17 18 //启动重画线程19 new PaintThread().start();20 21 //匿名内部类---用来关闭窗口22 addWindowListener(new WindowAdapter() {23 @Override24 public void windowClosing(WindowEvent e) {25 (0);26 }27 });28 29 }30 31 //双缓冲技术解决屏幕闪烁32 private Image offScreenImage = null; //利用双缓冲技术消除闪烁33 public void update(Graphics g) {34 if (offScreenImage == null)35 offScreenImage = (, );36 37 Graphics gOff = ();38 39 paint(gOff);40 (offScreenImage, 0, 0, null);41 }42 43 /**44 * 定义一个重画窗口的线程类45 * 是一个内部类(方便访问外部类属性)46 */47 class PaintThread extends Thread {48 public void run() {49 while (true) {50 repaint(); //重画51 try {52 (40); //1s = 1000ms53 } catch (InterruptedException e) {54 ();55 } 56 }57 }58 }59 60 }复制代码