import javax.swing.JFrame;
@SuppressWarnings("serial")
public class DisplaySurface extends JFrame
{ public DisplaySurface() 
{
super();
this.getContentPane().setLayout(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//客户区大小
this.setSize(600+4+4, 400+30+4);
//加载绘图Panel
this.add(new DrawPanel());
//不可改变大小
this.setResizable(false);
//设置标题
this.setTitle("MandelProb");
//显示
this.setVisible(true);
// TODO Auto-generated constructor stub

} /**
 * @param args
 */
public static void main(String[] args) 
{
// TODO Auto-generated method stub

DisplaySurface tempFrame = new DisplaySurface(); }}
import java.awt.Graphics;
import javax.swing.JPanel;
public class DrawPanel extends JPanel
{
public DrawPanel()
{
this.setSize(600, 400);
//this.setBackground(Color.BLACK); 
this.setVisible(true);
} /* (non-Javadoc)
 * @see javax.swing.JComponent#print(java.awt.Graphics)
 */
@Override
public void print(Graphics g) 
{
// TODO Auto-generated method stub
super.print(g);

g.fillRect(50,100,100,100); g.drawString("dfsfsdfsdf", 100, 100);
}}

解决方案 »

  1.   

    在Jpanel上绘图,写字不显示。希望快快能有解答,多谢了
      

  2.   


        @Override
        public void print(Graphics g) 
        {
            // TODO Auto-generated method stub
            super.print(g);
            g.setColor(Color.white);
            g.fillRect(50,100,100,100);
            g.setColor(Color.black);        g.drawString("dfsfsdfsdf", 100, 100);
        }
      

  3.   

    在主函数里面也要有tempFrame.setVisible(true);
      

  4.   

    Swing的容器都有个ContentPane,这里面的组件才会显示~
    this.getContentPane().add(你的panel)
      

  5.   

    同意5楼的说法,需要把东西放进ContentPane
      

  6.   

    对于Swing的JPanel组件重绘只需重写paintComponent()方法即可
    重写paint()方法是awt的重量级重绘,不是好选择!