简体   繁体   English

打包JFrame太高?

[英]Packed JFrame Too Tall?

I am trying to create a simple Java Swing GUI that consists of a panel on top and tabs in the center. 我正在尝试创建一个简单的Java Swing GUI,该GUI由顶部的面板和中心的标签组成。 I want the top panel to remain its preferred size and the tabs to take up the remaining space so I used a BorderLayout . 我希望顶部面板保持其首选大小,并且选项卡占用剩余空间,因此我使用BorderLayout The content of the tab can be tall so I put the tab component into a scroll pane. 选项卡的内容可能很高,因此我将选项卡组件放入滚动窗格中。

Everything seems to work the way I expect (with respect to component sizing and scroll bar behavior when I resize the frame) except that my packed frame is 12 pixels too tall (and possibly 16 pixels too wide). 一切似乎都按我期望的方式工作(关于调整框架大小时的组件大小和滚动条行为),除了打包的框架太高12像素(可能太宽16像素)。 Would someone please explain what is going on and how to resolve it. 有人请解释发生了什么事以及如何解决它。 Somehow when the pack is sizing all of the components, something is smart enough to (mostly) respect the screen size. 当包装确定所有组件的大小时,某种程度上,某种东西足够聪明,足以(主要)尊重屏幕尺寸。 I am using Java 8 on Windows 7 with a screen resolution of 1920 x 1200. 我在Windows 7上使用Java 8,屏幕分辨率为1920 x 1200。

Below is my test code and the output it produces. 以下是我的测试代码及其产生的输出。

Code: 码:

package test;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;

public final class SizeTest
{
    public static void main(final String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                createAndShowGui();
            }
        });
    }

    private static void createAndShowGui()
    {
        final JPanel topPanel = new JPanel();
        topPanel.setBorder(BorderFactory.createTitledBorder("Top"));
        topPanel.setPreferredSize(new Dimension(800, 150));

        final JPanel centerPanel = new JPanel();
        centerPanel.setBorder(BorderFactory.createTitledBorder("Center"));
        centerPanel.setPreferredSize(new Dimension(800, 1300));

        final JScrollPane scrollPane = new JScrollPane(centerPanel);

        final JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.addTab("Tab", scrollPane);

        final JPanel mainPanel = new JPanel(new BorderLayout(0, 10));
        mainPanel.add(topPanel, BorderLayout.NORTH);
        mainPanel.add(tabbedPane, BorderLayout.CENTER);

        final JFrame mainFrame = new JFrame("Size Test");
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.add(mainPanel);
        mainFrame.pack();

        System.err.println("***** Frame Size: " + mainFrame.getSize() + ", Screen Size: "
            + Toolkit.getDefaultToolkit().getScreenSize() + ", Maximum Window Bounds: "
            + GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds());

        mainFrame.setVisible(true);
    }
}

Output: 输出:

***** Frame Size: java.awt.Dimension[width=816,height=1212], Screen Size: java.awt.Dimension[width=1920,height=1200], Maximum Window Bounds: java.awt.Rectangle[x=0,y=0,width=1920,height=1156]

If you're going to stuff around with setPreferredSize , be prepared for things to go astray. 如果您打算使用setPreferredSize来解决问题,请为误入歧途做好准备。

The first thing I would do, is seriously reconsider using setPreferredSize . 我要做的第一件事是认真考虑使用setPreferredSize

Because of the way the API works, JScrollPane will use the preferredSize of the component to make determinations about it's own size. 由于API的工作方式, JScrollPane将使用组件的preferredSize来确定其自身的大小。 You can change this by implementing the Scrollable interface, which allows you to return the preferredScrollableViewportSize , which JScrollPane will use instead when determing how large it needs to be 您可以通过实现Scrollable接口来更改此设置,该接口允许您返回preferredScrollableViewportSizeJScrollPane在确定需要多大时将使用它。

You see Scrollable demonstrated here and here and lots of other places if you do some searching 如果您进行一些搜索,则会看到此处此处以及其他许多地方都展示 Scrollable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM