我在JPANEL上画了点东西,再将这个JPANEL放入到JFRAME中时,并没有看到图,怎么回事。下面是我的代码。
package testprogram;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;public class DrawPanel extends JFrame {

JPanel jp = null;

public DrawPanel(JPanel jp) {
this.jp = jp;
}

public void init() {
this.getContentPane().add(jp);
this.setSize(400,300);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setDefaultCloseOperation(1);
this.setVisible(true);
} public static void main(String[] args) {
MyJPanel jp = new MyJPanel();
//jp.setBackground(Color.PINK);
DrawPanel dp = new DrawPanel(jp);
dp.init();
}
}class MyJPanel extends JPanel { public void paintComponents(Graphics g) {
//super.paintComponents(g);
g.setColor(Color.RED);
g.fillOval(50,50, 60, 60);
}
}

解决方案 »

  1.   

    哎 , 查到了,重写了PAINT()方法后出来了。
    但的问题又来了,我在外面设置JP的背景色后运行没有反应。
    难道只能在 PAINT()中用画长方形的方法把背景给画了不成?
      

  2.   


    class MyJPanel extends JPanel {
    Color color = Color.red;
    public void setColor(Color color){
    this.color = color;
    }
        @Override
        public void paint(Graphics g){
         g.setColor(color);
            g.fillOval(50,50, 60, 60);
        }
    }调用setColor()改变背景色,这样就能在外面设置背景色了
      

  3.   

    我还是搞不出来,我用的是直接在里面画了个大大的方形
    g.setColor(Color.PINK);
    g.fillRect(0, 0, 400, 300);
    帮我改改,谢谢!package testprogram;
    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JFrame;
    import javax.swing.JPanel;public class DrawPanel extends JFrame {

    JPanel jp = null;

    public DrawPanel(JPanel jp) {
    this.jp = jp;
    }

    public void init() {
    this.getContentPane().add(jp);
    this.setSize(400,300);
    this.setLocationRelativeTo(null);
    this.setResizable(false);
    this.setDefaultCloseOperation(1);
    this.setVisible(true);
    } public static void main(String[] args) {
    MyJPanel jp = new MyJPanel();
    jp.startInnerThread();
    //jp.setBackground(Color.PINK);
    DrawPanel dp = new DrawPanel(jp);
    dp.init();
    }
    }class MyJPanel extends JPanel {

    int x = 160;
    int y = 90;
    int r = 5; public void paint(Graphics g) {
    Color c = g.getColor();
    g.setColor(Color.PINK);
    g.fillRect(0, 0, 400, 300);
    g.setColor(Color.RED);
    g.drawOval(x, y, 2*r, 2*r);
    g.setColor(c);
    r += 2;
    x -= 2;
    y -= 2;
    }

    public void startInnerThread () {
    new Thread(new InnerThread()).start();
    }

    private class InnerThread implements Runnable {

    public void run() {
    while(true) {
    repaint();
    try {
    Thread.sleep(50);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
        }
    }
      

  4.   

    看看效果吧paint()
    方法 改成 paintComponent()可以自动实现画图
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;import javax.swing.JFrame;
    import javax.swing.JPanel;public class DrawPanel extends JFrame
    {
      
    private static final long serialVersionUID = 1L;
    JPanel jp = null;
        
        public DrawPanel(JPanel jp) {
            this.jp = jp;
        }
        
        public void init() {
            this.getContentPane().add(jp);            
            this.setSize(400,300);        
            this.setLocationRelativeTo(null);
            this.setResizable(false);
            this.setDefaultCloseOperation(1);
            this.setVisible(true);
        }    public static void main(String[] args) 
        {
            MyJPanel jp = new MyJPanel();
            jp.startInnerThread();
            //jp.setBackground(Color.PINK);
            DrawPanel dp = new DrawPanel(jp);
            dp.init();
        }
    }class MyJPanel extends JPanel 
    {
        
        /**
     * 
     */
    private static final long serialVersionUID = 1L;
    int x = 160;
        int y = 90;
        int r = 5;    public void paintComponent(Graphics g) 
        {
         //Graphics g;
         Graphics2D g2=(Graphics2D)(g);
            Color c = g.getColor();
            g2.setColor(Color.PINK);
            g2.fillRect(0, 0, 400, 300);
            g2.setColor(Color.RED);
            g2.drawOval(x, y, 2*r, 2*r);        
            g2.setColor(c);
            r += 10;
            x -= 10;
            y -= 10;                
        }
        
        public void startInnerThread ()
        {
            new Thread(new InnerThread()).start();
        }
        
        private class InnerThread implements Runnable
        {
            Graphics g;
            public void run() 
            {
                while(true) 
                {
                
                 repaint();
                    try 
                    {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
      

  5.   

    自动实现画图是什么意思 ?
    好像就是把g换成了g2了。还有个问题就是,发现如果把paint中的代码放到一个while语句中就看不到任何画的东西了,怎么回事。
      

  6.   

    g2  与g  没什么关系
    你没注意到主要的不同
    里面的方法paint()
    变为了 paintComponent()
    这才是最要的
      

  7.   

    这个我是知道,我是说,paint 和paintComponent 方法体基本一样(就是一个是g一个是g2),这两个方法区别在哪。
    我本意是问,能不能不用画长方形的方法设置panel的背景色,而是用形如setBackground的方法来设置。