JFrame使用paint方法的问题

程序如下,当运行时,背景不会因为线程而刷新,这个你们运行一下就知道问题所在了,请问问题出在哪,为何我把JFrame换成Frame却正常.publicclassTestex... 程序如下,当运行时,背景不会因为线程而刷新,这个你们运行一下就知道问题所在了,请问问题出在哪,为何我把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();
}
}
}
}
}
展开
 我来答
daay1986
2009-08-19 · TA获得超过6019个赞
知道大有可为答主
回答量:2208
采纳率:0%
帮助的人:1507万
展开全部
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.setBackground(Color.GREEN);
这是里错了,另外把你的线程改成定时器了,比你用个死循环来画图要好。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式