为何框架窗口中没有显示图形....
// 我设计了一个CAR类,再JFRAME类来显示CAR,怎么运行时只见框架,不见车呢?
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import javax.swing.JFrame;
import javax.swing.JComponent;
class CarComponent extends JComponent
{
public void PrintCarComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
Car car1 = new Car(0,0);
Car car2 = new Car(getWidth() - 50,getHeight() - 30);
car1.draw(g2);
car2.draw(g2);
}
}
class Car
{
public Car(int x,int y)
{
X1 = x;
Y1 = y;
}
public void draw(Graphics2D g2)
{
Point2D.Double fronttop = new Point2D.Double(X1 + 20,Y1);
Point2D.Double frontlow = new Point2D.Double(X1 + 10,Y1 + 10);
Point2D.Double backtop = new Point2D.Double(X1 + 30,Y1);
Point2D.Double backlow = new Point2D.Double(X1 + 40,Y1 + 10);
Line2D.Double cartop = new Line2D.Double(fronttop,backtop);
Line2D.Double frontglass = new Line2D.Double(fronttop,frontlow);
Line2D.Double backglass = new Line2D.Double(backtop,backlow);
Rectangle carbody = new Rectangle(X1,Y1+10,50,10);
Ellipse2D.Double frontwheel = new Ellipse2D.Double(X1 + 10,Y1 + 20,10,10);
Ellipse2D.Double backwheel = new Ellipse2D.Double(X1 + 30,Y1 + 20,10,10);
g2.draw(cartop);
g2.draw(frontglass);
g2.draw(backglass);
g2.draw(carbody);
g2.draw(frontwheel);
g2.draw(backwheel);
}
private int X1;
private int Y1;
}
public class CarViewer
{
public static void main(String[] args)
{
JFrame Frame = new JFrame();
Frame.setSize(300,500);
Frame.setTitle("我的文档");
CarComponent Component = new CarComponent();
Frame.add(Component);
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Frame.setVisible(true);
}
}
搜索更多相关的解决方案:
图形 框架 窗口
----------------解决方案--------------------------------------------------------
public void PrintCarComponent(Graphics g)
把它改成
public void paintComponent(Grahpics g)
----------------解决方案--------------------------------------------------------
豁然明朗...
----------------解决方案--------------------------------------------------------