为什么JFrame的大小超过其ContentPane的大小,即使它具有已定义的大小? [英] Why does the size of the JFrame exceed the size of its ContentPane, even though it has a defined size?

查看:142
本文介绍了为什么JFrame的大小超过其ContentPane的大小,即使它具有已定义的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到摇摆中的容器之间存在奇怪的行为。



为了举例说明测试,我创建了一个JFrame和一个JPanel,并将面板设置为contentPane。我将首选和最大JPanel大小定义为400,300。所有这些都可以在下面的例子中看到:

  import java.awt.Color; 
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;

公共类ExcessiveSpacePanelTest {
JFrame框架;
JPanel面板;

public void initGUI(){

frame = new JFrame();

panel = new JPanel();
panel.setBorder(BorderFactory.createLineBorder(Color.RED,1));
panel.setPreferredSize(new Dimension(400,300));
panel.setMaximumSize(new Dimension(400,300));


frame.setContentPane(panel);
frame.pack();

System.out.println(panel size:[+ panel.getSize()。width +,+ panel.getSize()。height +]);
System.out.println(frame size:[+ frame.getSize()。width +,+ frame.getSize()。height +]);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
}

public static void main(String [] args){
EventQueue.invokeLater(() - > {
new ExcessiveSpacePanelTest()。initGUI() ;
});
}
}

结果是:





令我惊讶的是,终端的输出是:


面板尺寸:[400,300]

框架尺寸:[416,338]




我不明白为什么框架增加了这个额外的空间,即使组件中没有任何东西强制框架调整大小。



我试图通过在<$ c之前添加行 panel.setBorder(BorderFactory.createEmptyBorder(0,0,0,0)); 来在面板上设置归零边缘$ c> pack(); 但结果仍然相同。



这个问题是java给了我一个错误的信息,由于面板按框架尺寸缩放,从上面的结果来看,我们已经看到两者的尺寸不一样。 / p>

这两项措施是否正确报告?为什么会发生这种情况?

解决方案

您可能会发现在不同平台上比较类似程序的结果会很有帮助。特别是,




  • 红色边框里面小组;您的使用符合API

      import java.awt.Color; 
    import java.awt.Dimension;
    import java.awt.EventQueue;

    import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JPanel;

    公共类ExcessiveSpacePanelTest {
    JFrame框架;
    JPanel面板;

    public void initGUI(){
    frame = new JFrame();
    panel = new JPanel(){
    @Override
    public Dimension getPreferredSize(){
    return new Dimension(400,300);
    }
    };
    panel.setBorder(BorderFactory.createLineBorder(Color.RED,1));
    frame.add(面板);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    frame.setVisible(true);

    System.out.println(panel size:[+ panel.getSize()。width +,+ panel.getSize()。height +]);
    System.out.println(frame size:[+ frame.getSize()。width +,+ frame.getSize()。height +]);
    }

    public static void main(String [] args){
    EventQueue.invokeLater(() - > {
    new ExcessiveSpacePanelTest()。initGUI() ;
    });
    }
    }


    I noticed a strange behavior between containers in the swing.

    To exemplify the test, I created a JFrame and a JPanel, and set the panel as contentPane. I defined the preferred and maximum JPanel size to 400,300. All this can be seen in the example below:

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    
    import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class ExcessiveSpacePanelTest {
        JFrame frame;
        JPanel panel;
    
        public void initGUI(){
    
            frame =  new JFrame();
    
            panel = new JPanel();
            panel.setBorder(BorderFactory.createLineBorder(Color.RED, 1));
            panel.setPreferredSize(new Dimension(400, 300));
            panel.setMaximumSize(new Dimension(400, 300));
    
    
            frame.setContentPane(panel);
            frame.pack();
    
            System.out.println("panel size: [" + panel.getSize().width + "," + panel.getSize().height +"]");
            System.out.println("frame size: [" + frame.getSize().width + "," + frame.getSize().height+"]");
    
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
            frame.setResizable(false);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(() ->{
                new ExcessiveSpacePanelTest().initGUI();
            });
        }
    }
    

    The result is:

    To my surprise, the output at the terminal is:

    panel size: [400,300]
    frame size: [416,338]

    I did not understand why the frame added this extra space, even having nothing in the component that forces the frame to resize.

    I tried to set zeroed edges on the panel by adding the row panel.setBorder (BorderFactory.createEmptyBorder(0, 0, 0, 0)); before pack(); but the result is still the same.

    The problem with this is that java is giving me a false info, since the panel is scaling in the size of the frame, and from the above result, we have seen that the two are not the same size.

    Are the two measures being reported correctly? Why does this occur?

    解决方案

    You may find it helpful to compare the result from a similar program on a different platform. In particular,

    • The red Border is inside the panel; your usage conforms to the API recommendation, "we recommend that you put the component in a JPanel and set the border on the JPanel."

    • The frame width matches the panel width on Mac OS X because the heavyweight peer used for the top-level container has no vertical border on Mac OS X; the Windows peer has its own border.

    • The frame height includes the drag bar and frame border: 38 pixels on Widows, 22 on Mac OS X.

    • Don't use setPreferredSize() when you really mean to override getPreferredSize().

    • As an aside, the frame's add() method forwards to the content pane.

    panel size: [400,300]
    frame size: [400,322]
    

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    
    import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class ExcessiveSpacePanelTest {
        JFrame frame;
        JPanel panel;
    
        public void initGUI(){
            frame = new JFrame();
            panel = new JPanel() {
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(400, 300);
                }
            };
            panel.setBorder(BorderFactory.createLineBorder(Color.RED, 1));
            frame.add(panel);
            frame.pack();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
            frame.setResizable(false);
            frame.setVisible(true);
    
            System.out.println("panel size: [" + panel.getSize().width + "," + panel.getSize().height +"]");
            System.out.println("frame size: [" + frame.getSize().width + "," + frame.getSize().height+"]");
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(() ->{
                new ExcessiveSpacePanelTest().initGUI();
            });
        }
    }
    

    这篇关于为什么JFrame的大小超过其ContentPane的大小,即使它具有已定义的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆