java新手问题:为啥在JPanel中的控件显示不出来?代码如下:
求人指点import java.awt.*;
import javax.swing.*;public class Test extends JFrame{
public Test(){
JFrame jf = new JFrame ();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);

jf.setSize(200, 200);

ImagePanel ip = new ImagePanel();
Container contentPane = getContentPane();
contentPane.add(ip);

}
public static void main(String args[]){
Test fd = new Test();
}


public class MakeLabel extends JLabel{
public MakeLabel(String str,int x,int y){
setText(str);
setLocation(x,y);
setVisible(true);

}
}
public class ImagePanel extends JPanel{
public ImagePanel(){

setLayout(null);
MakeLabel ml = new MakeLabel("dgfgfdg",50,50);

add(ml);

setVisible(true);
JButton jb = new JButton("ddf");
jb.setLocation(35, 35);
add(jb);

}
}}

解决方案 »

  1.   

    首先,你既然是在里面用成员jf来作为面板容器,那么就应该是用它的ContentPane,其次,既然是null布局,指定控件的位置,那么你要指定控件的大小滴。把控件的大小指定好了,自然就能显示出来。
      

  2.   

    2楼的大哥,指定了你说的地方还是不行,如下:(注意标志行)
    import java.awt.*;
    import javax.swing.*;public class Test extends JFrame{
    public Test(){
    JFrame jf = new JFrame ();
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.setVisible(true);

    jf.setSize(200, 200);

    ImagePanel ip = new ImagePanel();
    Container contentPane = jf.getContentPane();//就是这行
    contentPane.add(ip);

    }
    public static void main(String args[]){
    Test fd = new Test();
    }


    public class MakeLabel extends JLabel{
    public MakeLabel(String str,int x,int y){
    setText(str);
    setLocation(x,y);
    setVisible(true);

    }
    }
    public class ImagePanel extends JPanel{
    public ImagePanel(){

    setLayout(null);
    MakeLabel ml = new MakeLabel("dgfgfdg",50,50);

    add(ml);

    setVisible(true);
    setSize(30,30);   //还有这行!!
    JButton jb = new JButton("ddf");
    jb.setLocation(35, 35);

    add(jb);

    }
    }}
      

  3.   

    哦,我知道了,要设置JLabel和JButton的大小,谢谢你!
      

  4.   

    说点题外话啊,你的代码我咋一看怪怪的。仔细看才注意到是问题所在。
    import javax.swing.*;
    public class TestFrame extends JFrame {
    public static void main(String[] args) {
    new TestFrame();
    }
    public TestFrame(){
    this.setSize(200,200);
    this.setVisible(true);
    }
    }
    和这个
    import javax.swing.*;
    public class TestFrame {
    public static void main(String[] args) {
    JFrame jf = new JFrame("hehe!");
    jf.setSize(200,200);
    jf.setVisible(true);
    }
    }
    这是两种最常见的编写GUI程序的方法。
    第一种是:自己写的类继承JFrame(),这个时候自己写的类就已经有了父类JFrame()所有的属性(包括动态属性和静态属性)所以没必要在new JFrame()了,直接在构造方法中用就行了。
    第二种是:既然已经引入了javax.swing.*;这个包了,直接new JFrame()用就好了。以上都是题外话,和楼主的问题没有关系。望楼主见谅!!
      

  5.   

    我觉得你这段代码
    JFrame jf = new JFrame (); 
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    jf.setVisible(true); jf.setSize(200, 200); ImagePanel ip = new ImagePanel(); 
    Container contentPane = jf.getContentPane();//就是这行 
    contentPane.add(ip); 
    移到main函数里面就对了
      

  6.   

    你既然继承了JFrame,就不用JFrame jf=new JFrame();用super("")设置标题 
      

  7.   


    JFrame jf = new JFrame (); 
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    jf.setVisible(true); jf.setSize(200, 200); ImagePanel ip = new ImagePanel(); 
    Container contentPane = jf.getContentPane();//就是这行 
    contentPane.add(ip); 
    窗口应该在实例化的时候 就要执行
    不然是不会显示的
    上面的代码放入 构造函数中
    试试看
    ^_*