日期:2014-05-20  浏览次数:20618 次

怎样才能显示出来啊?
写了个Frame却什么也显示不出来,这是为什么?
Java code

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);
    }
}


------解决方案--------------------
LS正解,不明白LZ为什么要取消p1和p2的布局???。。。。
------解决方案--------------------
要是这样的话,就可以不用任何布局管理器(setLayout(null));而对所有组件逐个进行setBounds()。
------解决方案--------------------
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的
Java code

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);
    }
}