写了个Frame却什么也显示不出来,这是为什么?import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Computor {
public static void main(String[] args) {
new ComputorFrame();
}}class ComputorFrame extends JFrame{ JLabel jl1,jl2,jl3,jl4,jl5,jl6;

public ComputorFrame(){
super("计算器");

Container container=getContentPane();
jl1=new JLabel("被乘数");
jl2=new JLabel("乘数");
jl3=new JLabel("积");
JPanel p1=new JPanel();
p1.setLayout(null);
p1.add(jl1);
p1.add(jl2);
p1.add(jl3);
jl1.setSize(50, 40);
jl1.setLocation(50, 0);
jl2.setSize(50, 40);
jl2.setLocation(100, 0);
jl3.setSize(50, 40);
jl3.setLocation(150, 0); jl4=new JLabel("被除数");
jl5=new JLabel("除数");
jl6=new JLabel("商");
JPanel p2=new JPanel();
p2.setLayout(null);
p2.add(jl4);
p2.add(jl5);
p2.add(jl6);
jl4.setSize(50, 40);
jl4.setLocation(50, 50);
jl5.setSize(50, 40);
jl5.setLocation(100, 50);
jl6.setSize(50, 40);
jl6.setLocation(150, 50);
container.add(p1,BorderLayout.NORTH);
container.add(p2,BorderLayout.SOUTH);

setSize(300,300);
setVisible(true);
}
}

解决方案 »

  1.   

    setLayout(null)有问题,注释了就出来了
    import java.awt.BorderLayout;
    import java.awt.Container;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    public class Computor {
        public static void main(String[] args) {
            new ComputorFrame();
        }}class ComputorFrame extends JFrame{    JLabel jl1,jl2,jl3,jl4,jl5,jl6;
        
        public ComputorFrame(){
            super("计算器");
            
            Container container=getContentPane();
            jl1=new JLabel("被乘数");
            jl2=new JLabel("乘数");
            jl3=new JLabel("积");
            JPanel p1=new JPanel();
            //p1.setLayout(null);
            p1.add(jl1);
            p1.add(jl2);
            p1.add(jl3);
            jl1.setSize(50, 40);
            jl1.setLocation(50, 0);
            jl2.setSize(50, 40);
            jl2.setLocation(100, 0);
            jl3.setSize(50, 40);
            jl3.setLocation(150, 0);        jl4=new JLabel("被除数");
            jl5=new JLabel("除数");
            jl6=new JLabel("商");
            JPanel p2=new JPanel();
            //p2.setLayout(null);
            p2.add(jl4);
            p2.add(jl5);
            p2.add(jl6);
            jl4.setSize(50, 40);
            jl4.setLocation(50, 50);
            jl5.setSize(50, 40);
            jl5.setLocation(100, 50);
            jl6.setSize(50, 40);
            jl6.setLocation(150, 50);
            container.add(p1,BorderLayout.NORTH);
            container.add(p2,BorderLayout.SOUTH);
            
            setSize(300,300);
            setVisible(true);
        }
    }
      

  2.   

    setLayout(null)
    最好用一种布局
      

  3.   

    LS正解,不明白LZ为什么要取消p1和p2的布局???。
      

  4.   

    因为有很多个JLabel、JTextField和JButton要安插在上面,每一个组件的位置都要精确定义,不能按照默认管理器的顺序排列。
    这么说吧,有30多个组件放进去,每一个的位置都要用x、y坐标精确定义,该用什么方法?
      

  5.   

    要是这样的话,就可以不用任何布局管理器(setLayout(null));而对所有组件逐个进行setBounds()。
      

  6.   

    jl1.setSize(50, 40);
    jl1.setLocation(50, 0);
    jl2.setSize(50, 40);
    jl2.setLocation(100, 0);
    jl3.setSize(50, 40);
    jl3.setLocation(150, 0);
    你的这些坐标定位希望是相对于什么的
    如果是相对于container的import java.awt.BorderLayout;
    import java.awt.Container;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    public class Computor {
        public static void main(String[] args) {
            new ComputorFrame();
        }}class ComputorFrame extends JFrame{    JLabel jl1,jl2,jl3,jl4,jl5,jl6;
        
        public ComputorFrame(){
            super("计算器");
            
            Container container=getContentPane();
    container.setLayout(null);

            jl1=new JLabel("被乘数");
            jl2=new JLabel("乘数");
            jl3=new JLabel("积");
            container.add(jl1);
            container.add(jl2);
            container.add(jl3);
            jl1.setSize(50, 40);
            jl1.setLocation(50, 0);
            jl2.setSize(50, 40);
            jl2.setLocation(100, 0);
            jl3.setSize(50, 40);
            jl3.setLocation(150, 0);        jl4=new JLabel("被除数");
            jl5=new JLabel("除数");
            jl6=new JLabel("商");
            container.add(jl4);
            container.add(jl5);
            container.add(jl6);
            jl4.setSize(50, 40);
            jl4.setLocation(50, 50);
            jl5.setSize(50, 40);
            jl5.setLocation(100, 50);
            jl6.setSize(50, 40);
            jl6.setLocation(150, 50);
            
            setSize(300,300);
            setVisible(true);
        }
    }
    如果你是想相对于panel的import java.awt.*;
    import javax.swing.*;public class Computor
    {
        public static void main(String[] args)
    {
            new ComputorFrame();
        }
    }class ComputorFrame extends JFrame
    {    
        public ComputorFrame()
    {
            super("计算器");
            
            Container container=getContentPane();
    container.setLayout(new GridLayout(2, 1));

            MPanel p1=new MPanel();
            YPanel p2=new YPanel();
            
    container.add(p1);
            container.add(p2);

    setSize(300,300);
            setVisible(true);
        }
    }class MPanel extends JPanel
    {
    public MPanel()
    {
    setLayout(null);
    JLabel jl1=new JLabel("被乘数");
            JLabel jl2=new JLabel("乘数");
            JLabel jl3=new JLabel("积");
    jl1.setSize(50, 40);
            jl1.setLocation(50, 0);
            jl2.setSize(50, 40);
            jl2.setLocation(100, 0);
            jl3.setSize(50, 40);
            jl3.setLocation(150, 0);
    add(jl1);
            add(jl2);
            add(jl3);
    }
    }class YPanel extends JPanel
    {
    public YPanel()
    {
    setLayout(null);
    JLabel jl4=new JLabel("被除数");
            JLabel jl5=new JLabel("除数");
            JLabel jl6=new JLabel("商");
            jl4.setSize(50, 40);
            jl4.setLocation(50, 50);
            jl5.setSize(50, 40);
            jl5.setLocation(100, 50);
            jl6.setSize(50, 40);
            jl6.setLocation(150, 50);
    add(jl4);
            add(jl5);
            add(jl6);
    }
    }