日期:2014-05-20 浏览次数:20740 次
public class Test extends JFrame implements ActionListener { public static void main(String[] args) { Test t = new Test(); t.fun(); System.out.println("End of Main"); } int x, y; Button but; public Test() { setBounds(400, 100, 350, 200); this.setLayout(null); setDefaultCloseOperation(EXIT_ON_CLOSE); but = new Button("OK"); but.setSize(40, 40); add(but); but.addActionListener(this); x = y = 50; setVisible(true); } public void paint(Graphics g) { System.out.println("call paint()"); super.paint(g); g.fillOval(x, y, 9, 9); } public void fun() { for (int i = 0; i < 5; i++) { x += 20; repaint(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } public void actionPerformed(ActionEvent e) { fun(); } }
public class Test extends JFrame implements ActionListener { public static void main(String[] args) { Test t = new Test(); t.fun(); System.out.println("End of Main"); for (int i=0; i<100; i++) { //主线程循环测试,看看主线程是否影响Swing线程 System.out.println("main" + i); try {Thread.sleep(1000);} catch (Exception e) {} } } int x, y; Button but; public Test() { setBounds(400, 100, 350, 200); this.setLayout(null); setDefaultCloseOperation(EXIT_ON_CLOSE); but = new Button("OK"); but.setSize(40, 40); add(but); but.addActionListener(this); x = y = 50; setVisible(true); } public void paint(Graphics g) { System.out.println("call paint()"); super.paint(g); g.fillOval(x, y, 9, 9); } public void fun() { for (int i = 0; i < 5; i++) { x += 20; repaint(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } public void actionPerformed(ActionEvent e) { new Thread() { //用线程来执行,看看该线程是否影响Swing线程 public void run() { fun(); } }.start(); } }