JAVA窗体中画不出图形来?

来源:百度知道 编辑:UC知道 时间:2024/06/18 12:38:29
import java.awt.*;
import javax.swing.*;
class test
{
public static void main(String [] args)
{
Graphics g;
test p1 = new test();
Frame f1 = new Frame("test");
f1.setSize(200,200);
f1.setVisible(true);

g = f1.getGraphics();
g.setColor(Color.black);
g.drawLine(30,30,80,80);
}
}

为什么在frame中看不到线?

import java.awt.*;
import javax.swing.*;
class test
{
public static void main(String [] args)
{
Graphics g;
test p1 = new test();
Frame f1 = new Frame("test");
f1.setSize(200,200);
f1.setVisible(true);

g = f1.getGraphics();

g.setColor(Color.black);
try{Thread.sleep(100);}
catch(Exception e){
}
g.drawLine(30,30,80,80);
}
}

画图请到paint里面去画