【问题标题】:setPreferredSize does not worksetPreferredSize 不起作用
【发布时间】:2012-08-13 17:09:58
【问题描述】:

代码:

import java.awt.Dimension;

import javax.swing.*;

public class Game extends JFrame {
    private static final long serialVersionUID = -7919358146481096788L;
    JPanel a = new JPanel();
    public static void main(String[] args) {
        new Game();
    }
    private Game() {
        setTitle("Insert name of game here");
        setLocationRelativeTo(null);
        setLayout(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        a.setPreferredSize(new Dimension(600, 600));
        add(a);
        pack();
        setVisible(true);
    }
}

所以我将JPanel 的首选大小设置为 600 x 600 并打包框架,但框架的大小仍然是 0 x 0。

为什么会这样,我该如何解决?

【问题讨论】:

标签: java swing jframe awt jpanel


【解决方案1】:

正如你所说,pack() 将尝试安排窗口,以便将每个组件的大小调整为它的首选大小。

问题在于布局管理器似乎是试图排列组件及其各自的preferredSizes 的人。但是,当您将布局管理器设置为空时,没有人负责。

尝试评论setLayout(null) 行,你会看到结果。当然,对于一个完整的窗口,您将不得不选择并设置一个有意义的LayoutManager

这对我来说很好用:

import java.awt.Dimension;

import javax.swing.*;

public class Game extends JFrame {
    private static final long serialVersionUID = -7919358146481096788L;
    JPanel a = new JPanel();
    public static void main(String[] args) {
        new Game();
    }
    private Game() {
        setTitle("Insert name of game here");
        setLocationRelativeTo(null);
        //setLayout(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        a.setPreferredSize(new Dimension(600, 600));
        add(a);
        pack();
        setVisible(true);
    }
}

【讨论】:

    【解决方案2】:

    pack() 查询父容器的首选大小而不是子容器的首选大小,因此您必须使用:

    setPreferredSize(new Dimension(600, 600));
    

    另一个注意事项是调用

    setLocationRelativeTo(null);
    

    在调用pack() 计算中心坐标之后:)

    好的,刚刚发现了空布局,为什么不用JFrame默认的BorderLayout呢?

    【讨论】:

    • 好的,谢谢,但现在我添加到 JPanel 的任何内容都不会出现!
    • +1 要求更改调用顺序。先将组件添加到JFrame,使其大小可以实现,然后引导它在屏幕上相对于它的大小的位置,然后将其设置为可见。好方法:-)
    【解决方案3】:

    你的问题是setLayout(null),因为文档说pack()

    调整此窗口的大小以适合其子组件的首选大小和布局

    因此没有布局,它不能正确执行。

    这对我来说似乎很好用:

    import java.awt.Dimension;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class Game extends JFrame {
    
        JPanel panel = new JPanel();
    
        private void createAndShowGUI() {
            setTitle("FrameDemo");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            panel.setPreferredSize(new Dimension(600, 600));
            add(panel);
    
            //setLayout(null); //wont work with this call as pack() resizes according to layout manager
            pack();
            setLocationRelativeTo(null);
            setVisible(true);
        }
    
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new Game().createAndShowGUI();
                }
            });
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-05
      • 2019-04-09
      • 2013-09-05
      • 1970-01-01
      • 2017-03-20
      • 2017-08-05
      相关资源
      最近更新 更多