这几天看Javacore 运行上面的画图的例子 出来了一个窗口 但是没有得到想要的图像 面板上什么也没有 大家帮忙看下啊 
import javax.swing.JFrame;@SuppressWarnings("serial")
public class DrawFrame extends JFrame { public static final int DEFAULT_WIDTH = 400;
public static final int DEFAULT_HEIGHT = 400;
public DrawFrame(){
setTitle("DrawTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
DrawComponent component = new DrawComponent();
add(component);
}
}import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;import javax.swing.JComponent;public class DrawComponent extends JComponent{
public DrawComponent(){
super.doLayout();
}
public void painComponent(Graphics 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.setPaint(Color.RED);
g2.drawString("Warning!", 100, 100);
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);
}
}import java.awt.*;
import javax.swing.*;
public class DrawTest  { public static void main(String[] args) {
EventQueue.invokeLater(new Runnable(){
public void run(){
DrawFrame frame = new DrawFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}); }
}

解决方案 »

  1.   

    在DrawComponent的painComponent()函数设个断点就发现根本没有进来。重载DrawComponent类的paint(),手动调用painComponent(g)图像就出来了。我也不知道为什么
      

  2.   

    你的问题出在你的painComponent(Graphics g)方法中,他没有在JComponent中画出来所以加入JFrame中自然看不见 ,你把这个方法名改下,改为public void paint(Graphics g)就可以了   你是不是画了一个圆里面有个矩形,矩形里面由椭圆还有条斜线,和warning?  我调试了下可以运行代码在下面
    import javax.swing.JFrame; 
    import javax.swing.JLabel;//@SuppressWarnings("serial") 
    public class DrawFrame extends JFrame { 
    DrawComponent component = new DrawComponent(); 
    public static final int DEFAULT_WIDTH = 400; 
    public static final int DEFAULT_HEIGHT = 400; 
    JLabel btn=new JLabel("anniu");
    public DrawFrame(){ 
    setTitle("DrawTest"); 
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); getContentPane().add(component); 

    } import java.awt.Color; 
    import java.awt.Graphics; 
    import java.awt.Graphics2D; 
    import java.awt.geom.Ellipse2D; 
    import java.awt.geom.Line2D; 
    import java.awt.geom.Rectangle2D; import javax.swing.JComponent; public class DrawComponent extends JComponent{ 
    //public DrawComponent(){ 
    //super.doLayout(); 
    //} 
    public void paint(Graphics 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); 
    //g2.drawLine(100,100,150,150);
    Ellipse2D ellipse = new Ellipse2D.Double(); 
    ellipse.setFrame(rect); 
    g2.draw(ellipse); 
    g2.setPaint(Color.RED); 
    g2.drawString("Warning!", 100, 100); 
    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); 

    } import java.awt.*; 
    import javax.swing.*; 
    public class DrawTest  { public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable(){ 
    public void run(){ 
    DrawFrame frame = new DrawFrame(); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setVisible(true); 

    }); } 
    }
    运行下试试!要是不行把那个getContentPane().add()改为add()我们版本不同
      

  3.   

    其实……我想轻轻的提示一下,楼主你的重载方法名打错了,“painComponent”,应该是paintComponent,paint事件发生时自然不会调用painComponent啦。呵呵,千万不能那么粗心啊,写程序这个是最致命的。