简体   繁体   中英

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. I want the top panel to remain its preferred size and the tabs to take up the remaining space so I used a 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). 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.

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.

The first thing I would do, is seriously reconsider using setPreferredSize .

Because of the way the API works, JScrollPane will use the preferredSize of the component to make determinations about it's own size. 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

You see Scrollable demonstrated here and here and lots of other places if you do some searching

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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