
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();
}
}
}
}
} 展开
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();
}
}
}
}
} 展开
1个回答
展开全部
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);
这是里错了,另外把你的线程改成定时器了,比你用个死循环来画图要好。
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);
这是里错了,另外把你的线程改成定时器了,比你用个死循环来画图要好。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询