下面这几行代码完全按书上打出来的 为何只显示个光秃秃的frame出来 是panel没add进去 还是没draw出来?import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
public class SimpleFrameTest {
public static void main(String... args){
DrawFrame frame=new DrawFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class DrawFrame extends JFrame{
public DrawFrame(){
setTitle("DrawTest");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
DrawPanel panel=new DrawPanel();
add(panel);
}
public static final int DEFAULT_WIDTH=400;
public static final int DEFAULT_HEIGHT=400;
}
class DrawPanel extends JPanel{
public void painComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2=(Graphics2D)g;
double leftX=100;
double topY=100;
double width=200;
double height=150;

Rectangle2D rect=new Rectangle2D.Double(leftX,topY,width,height);
g2.draw(rect);

Ellipse2D ellipse=new Ellipse2D.Double();
ellipse.setFrame(rect);
g2.draw(ellipse);

g2.draw(new Line2D.Double(leftX,topY,leftX+width,topY+height));

double centerX=rect.getCenterX();
double centerY=rect.getCenterY();
double radius=150;

Ellipse2D circle=new Ellipse2D.Double();
circle.setFrameFromCenter(centerX, centerY, centerX+radius, centerY+radius);
g2.draw(circle);
}
}

解决方案 »

  1.   

    这里没有什么问题呀,你的程序本来就没有加载什么图形
       DrawPanel panel=new DrawPanel();
       add(panel);
    这里就是加了一个空的DrawPanel进去,里面什么都没有的,你的painComponent方法没有被调用,应该是有另一个类继承实现了Graphics2D,然后才能调用painComponent方法
      

  2.   

    找到问题了painComponent少写了个t
      

  3.   

    ……
    class DrawFrame extends JFrame{
        public DrawFrame(){
            setTitle("DrawTest");
            setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
            DrawPanel panel=new DrawPanel();
            this.getContentPane().add(panel);//低版本的jdk应该这样写
        }
        public static final int DEFAULT_WIDTH=400;
        public static final int DEFAULT_HEIGHT=400;
    }……试试
      

  4.   

    public void painComponent(Graphics g){小了一个t导致你的这个方法不会被调用
    应该是paintComponent