JFrame使用paint方法的问题

来源:百度知道 编辑:UC知道 时间:2024/05/10 05:20:31
程序如下,当运行时,背景不会因为线程而刷新,这个你们运行一下就知道问题所在了,请问问题出在哪,为何我把JFrame换成Frame却正常.
public class Test extends JFrame{
public static void main(String[] args) {
new Test();
}
public Test() {
super ("Test");
launch();
}

public void launch() {
this.setSize(GAMEWIDTH, GAMEHEIGHT);
this.setResizable(false);
new Thread(new PaintThread()).start();
this.getContentPane.setBackground(Color.GREEN);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);
}

public void paint(Graphics g) {
g.fillOval(50, 50, 50, 50);
}

public class PaintThread implements Runnable {

public void run() {
while(true) {
repaint();
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}

class Test extends JFrame {

public static void main(String[] args) {
new Test();
}

public Test() {
super("Test");
launch();
}

public void launch() {
this.setResizable(true);
this.setBounds(100, 100, 300, 300);
Timer timer = new Timer(300, new PaintThread());
this.getContentPane().setBackground(Color.GREEN);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
timer.start();
}

public void paint(Graphics g) {
super.paint(g);
g.fillOval(50, 50, 50, 50);
}

public class PaintThread extends AbstractAction {

@Override
public void actionPerformed(ActionEvent e) {
repaint();
}
}
}

this.getContentPane.setBackgrou