我在JPanel中setSize没有作用 为什么呀?import javax.swing.*;public class SimpleFrame extends JFrame{
    
    
    public SimpleFrame() {        
        super("Frame Title");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel p = new JPanel();
        p.setSize(300, 300);  //这个没有用呀,不能显示300*300的亚
        setContentPane(p);
        pack();
        setVisible(true);
        show();
    }
    
    public static void main(String[] args) {
        SimpleFrame sf = new SimpleFrame();
    }
    
}

解决方案 »

  1.   

    import javax.swing.*;public class SimpleFrame extends JFrame {    public SimpleFrame() {
            super("Frame Title");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel p = new JPanel();
            setSize(300, 300); // 这个没有用呀,不能显示300*300的亚
            setContentPane(p);
            //pack();
            setVisible(true);
            show();
        }    public static void main(String[] args) {
            SimpleFrame sf = new SimpleFrame();
        }}
      

  2.   

    public void pack()
        Causes this Window to be sized to fit the preferred size and layouts of its subcomponents. If the window and/or its owner are not yet displayable, both are made displayable before calculating the preferred size. The Window will be validated after the preferredSize is calculated. 你里面没有component,所以最小话显示了
      

  3.   

    这样啊
    import javax.swing.*;public class sddd extends JFrame{
        
        
    public sddd() {        
    super("Frame Title");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(300,300);
    setVisible(true);
    show();
    }
        
    public static void main(String[] args) {
    sddd sf = new sddd();
    }
        
    }