public class ChatFrame extends JFrame {

String username = null;

public ChatFrame(String username) {
this.username = username;
}
public static void main(String[] args) {
ChatFrame cf = new ChatFrame("test");
cf.launchFrame();
}


public  void launchFrame() {
setLayout(null);
setIconImage(Toolkit.getDefaultToolkit().getImage("./image/main.png"));
setTitle(username + "欢迎你");
//this.setBackground(new Color(82,195,253));

JPanel jp1 = new JPanel();
jp1.setLayout(null);
JLabel jl = new JLabel("sdfsdfdsf");
jl.setBounds(5, 15, 50, 50);
//ImageIcon im = new ImageIcon("./image/main.png");
//jl.setForeground(new Color(250,9,38));
//jl.setIcon(im);
jp1.add(jl);
this.add(jp1,BorderLayout.NORTH);
setSize(213,523);
setLocation(700,150);
setVisible(true);
}
}
为什么jpanel中的label不显示呢?请高手解答下!

解决方案 »

  1.   

    setLayout(null);
     把这句去掉   就能显示了  你这都设置为null了  后面还用this.add(jp1,BorderLayout.NORTH);
     肯定不能显示了
      

  2.   


    /*
     * 以下代码在 Windows XP + JDK 5 测试通过,2008年11月15日,oracle_lover
     * 关键之处在于,如果 JPanel 的 Layout 为 null,其父容器的 Layout 也要为 null
     * 各组件需要用 setPreferredSize() 自定义大小,用 setBounds() 自定义位置
     */
    import java.awt.Dimension;import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;public class LayoutTest extends JFrame {  private static final long serialVersionUID = -8655534928921913270L;  public LayoutTest() {
        super("Java Test");    this.setSize(640, 480); // 设置窗口大小
        this.setLocationRelativeTo(null); // 窗口屏幕居中
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 窗口关闭,则程序退出
        java.awt.Container container = this.getContentPane();    // 创建一个 JPanel,且 Layout 为 null
        JPanel paneTop = new JPanel();
        paneTop.setLayout(null);    // 创建相应组件,首先各组件要自己用 setPreferredSize()定义大小,
        // 然后用 setBounds() 设定位置
        JLabel lb = new JLabel("测试文本:");
        Dimension dim1 = new Dimension(100, 20);
        lb.setPreferredSize(dim1);
        lb.setBounds(12, 12, dim1.width, dim1.height);
        JTextField tf = new JTextField();
        Dimension dim2 = new Dimension(100, 20);
        tf.setPreferredSize(dim2);
        tf.setBounds(140, 12, dim2.width, dim2.height);
        paneTop.add(lb);
        paneTop.add(tf);    // 要想让 Layout 为 null 的 JPanel 显示,其父容器的 Layout 也必须为 null
        // 同样,这个 JPanel 要自定义大小和位置
        container.setLayout(null);
        Dimension dim0 = new Dimension(300, 40);
        paneTop.setBounds(0, 0, dim0.width, dim0.height);
        container.add(paneTop);
      }  // 创建GUI
      private static void createAndShowGUI() {
        LayoutTest xframe = new LayoutTest();
        xframe.setVisible(true);
      }  public static void main(String[] args) {
        // 为事件处理线程安排一个任务:创建并显示程序的GUI
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            createAndShowGUI();
          }
        });
      }}
      

  3.   

    //setLayout(null);this.getContentPane().add(jp1);这样就可以显示了,不过效果不好,应该是布局管理器没有设置好的原因