【问题标题】:Strange JFrame size奇怪的JFrame大小
【发布时间】:2014-07-05 05:30:54
【问题描述】:

所以我设置了 setSize(500,500).. 添加一些面板,面板 Y 的总和是 500,就像 JFrame 但执行它显示计数 Y 为 525 我错过了什么吗?

JPanel panel = new JPanel();
    panel.setLayout(null);
    getContentPane().add(panel);
//--------------------

    JPanel top_panel = new JPanel();
    top_panel.setLayout(null);
    top_panel.setBackground(Color.blue);
    top_panel.setBounds(0, 0, 500, 40);
    panel.add(top_panel);

    //------------------------------

    JPanel middle_panel = new JPanel();
    middle_panel.setLayout(null);
    middle_panel.setBackground(Color.yellow);
    middle_panel.setBounds(0, 40, 500, 385);
    panel.add(middle_panel);

    //-----------------------------

    JPanel bottom_panel = new JPanel();
    bottom_panel.setLayout(null);
    bottom_panel.setBackground(Color.black);
    bottom_panel.setBounds(0, 425, 500, 75);
    panel.add(bottom_panel);

    setSize(500,500);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setResizable(false);
    setLocationRelativeTo(null);

40+385+75 = 500 但要显示我必须显示的所有面板

setSize(500,525);

那么它适合

这是一张图片:

【问题讨论】:

    标签: java swing jframe size


    【解决方案1】:

    框架大小是包括标题栏在内的浅蓝色矩形。您的面板出现在框架大小小于框架边框和框架标题栏的内部边界中。您是否看到底部标记的空间与标题栏的高度相同?

    在将面板/组件添加到框架之后,在调用 frame.setVisible(true) 之前,调用 frame.pack()。

    如果您采用布局管理器(例如 FlowLayout)并在必要时调用 setPreferredSize 并让布局管理器进行布局,这也是更可取的。通常人们会调用 setPreferredSize 而不是 setBound、setSize、setMininumSize、setMaximumSize。

    import javax.swing.*;
    import java.awt.*;
    
    public class FrameSize {
    
        private JFrame frame;
    
        FrameSize create() {
    
            frame = createFrame();
            frame.getContentPane().add(createContent());
    
            return this;
        }
    
        private JFrame createFrame() {
            JFrame frame = new JFrame(getClass().getName());
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    
            return frame;
        }
    
        void show() {
            //   frame.setSize(500, 500);
            frame.setResizable(false);
            frame.setLocationRelativeTo(null);
            frame.pack();
            frame.setVisible(true);
        }
    
        private Component createContent() {
            JPanel panel = new JPanel(null);
    
            JPanel topPanel = new JPanel(null);
            topPanel.setBackground(Color.blue);
            topPanel.setBounds(0, 0, 500, 40);
            panel.add(topPanel);
    
            JPanel middlePanel = new JPanel(null);
            middlePanel.setBackground(Color.yellow);
            middlePanel.setBounds(0, 40, 500, 385);
            panel.add(middlePanel);
    
            JPanel bottomPanel = new JPanel(null);
            bottomPanel.setBackground(Color.black);
            bottomPanel.setBounds(0, 425, 500, 75);
            panel.add(bottomPanel);
    
            panel.setPreferredSize(new Dimension(500, topPanel.getBounds().height + middlePanel.getBounds().height + bottomPanel.getBounds().height));
            return panel;
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new FrameSize().create().show();
                }
            });
        }
    }
    

    【讨论】:

    • 我调用了 pack() 但现在它出现了一个奇怪的框架,像 100x25 ...你确定框架大小计入标题栏吗?
    • 是的。 frame.setSize 设置整个帧大小,而不是内部内容窗格大小。请参阅上面的修改代码。布局管理器和 setPreferredSize 是要走的路。同样的建议也适用于 JavaFX。
    【解决方案2】:

    您不应该设置大小或调用setSize(...)setBounds(...),因为这会让您在未来遇到类似问题,或者当您尝试在不同平台上显示您的 GUI 时出现更严重的问题。相反,让您的组件的 preferredSizes 和布局管理器为您完成这项工作。如果您绝对必须设置组件的大小,则覆盖 getPreferredSize() 并返回一个为您计算的 Dimension。是的,根据 javajon,您应该在显示之前在 JFrame 上调用 pack()

    有关空布局的更多讨论,请阅读本网站上最好的 Swing 专家之一 MadProgrammer 在他的回答 here 中所说的话。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-09
      • 1970-01-01
      • 1970-01-01
      • 2011-07-15
      • 2018-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多