javaGUI解决闪屏问题
javaGUI解决闪屏问题可以通过继承JPanel来实现,特别简单,因为JPanel有已经实现的解决闪屏的方法
JFrame jfm=new JFrame();
this.setDoubleBuffered(true);
1 package demo.wl; 2 3 import java.awt.Color; 4 import java.awt.Graphics; 5 6 import javax.swing.JFrame; 7 import javax.swing.JPanel; 8 9 public class DemoGUI extends JPanel{ 10 public void init(){ 11 JFrame jfm=new JFrame(); 12 //this.setDoubleBuffered(true);//这个就是JPnel自带的双缓冲技术 13 jfm.add(this);//把JPanel组件加到窗口上来 14 jfm.setTitle("GUI"); 15 jfm.setVisible(true); 16 jfm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 17 jfm.setBounds(0, 8, 820, 620); 18 jfm.setLocationRelativeTo(null); 19 jfm.setResizable(false); 20 new Thread(new MyThread()).start(); 21 } 22 public static void main(String[] args) { 23 new DemoGUI().init(); 24 } 25 @Override 26 public void paint(Graphics g) { 27 super.paint(g); 28 g.setColor(Color.GRAY); 29 g.drawRect(20, 40, 750, 525); 30 g.fillRect(200, 110, 50,50); 31 for(int i=1;i<=50;i++){ 32 for(int j=1;j<=35;j++){ 33 g.drawLine(20,15*j+40,770, 15*j+40); 34 } 35 g.drawLine(20+15*i, 40, 20+15*i, 565); 36 } 37 } 38 public class MyThread implements Runnable{ 39 @Override 40 public void run() { 41 while(true){ 42 repaint(); 43 try { 44 Thread.sleep(1); 45 } catch (InterruptedException e) { 46 // TODO Auto-generated catch block 47 e.printStackTrace(); 48 } 49 } 50 } 51 } 52 }