import java.awt.*;
public class TestPaint{
public static void main(String[]args){
new PaintFrame().launchFrame();
}
}
class PaintFrame extends Frame{
public void launchFrame(){
setBounds(200,200,640,640);
setVisible(true);
}
public void paint(Graphics g){
Color c = g.getColor();
g.setColor(Color.red);
g.drawOval(50,50,30,30);
g.setColor(Color.green);
g.drawRect(50,50,50,50);
g.setColor(c);
}
}
上面这段程序,调试没有错误,运行时之显示一个窗口,里面本意要显示的一个椭圆和一个方形却没有显示。各位,谁能帮我看一下,哪里出了问题?谢谢!
------解决方案--------------------
试了一下可以显示,如果不行试试下面这个代码:
- Java code
import java.awt.Color;import java.awt.EventQueue;import java.awt.Frame;import java.awt.Graphics;public class TestPaint { public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new PaintFrame().launchFrame(); } }); }}class PaintFrame extends Frame { public void launchFrame() { setBounds(200, 200, 640, 640); setVisible(true); } @Override public void paint(Graphics g) { Color c = g.getColor(); g.setColor(Color.red); g.drawOval(50, 50, 30, 30); g.setColor(Color.green); g.drawRect(50, 50, 50, 50); g.setColor(c); }}