简体   繁体   中英

Set JPanel Size to No Larger Than Element it Contains

I'm trying to get a JPanel to be no taller than the text in the single JLabel it contains. Here's some simple example code stripped down to just the essential issue:

import java.awt.*;
import javax.swing.*;

public class MinimumSpacing {

    private static void createAndShowGUI() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 250);
        JPanel pane0 = new JPanel(new GridLayout(0,1,0,0));
        JPanel pane1 = new JPanel(new GridLayout(0,1,0,0));
        JPanel pane2 = new JPanel(new GridLayout(0,1,0,0));

        pane1.add(new JLabel("This is line #1.", JLabel.CENTER));
        pane2.add(new JLabel("This is line #2.", JLabel.CENTER));
        pane2.add(new JLabel("This is line #3.", JLabel.CENTER));
        pane2.add(new JLabel("This is line #4.", JLabel.CENTER));
        pane2.add(new JLabel("This is line #5.", JLabel.CENTER));

        pane0.add(pane1);
        pane0.add(pane2);
        frame.add(pane0);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(
                "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
            );
        } catch (Exception exc) {
            //
        }
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }  
}

This is the result I get:

在此处输入图片说明

What I want is for “line #1” to be at the top of the frame with the other 4 lines immediately under it. What's the best way to shrink the size of pane1 to make this happen?

Any help would be appreciated.

Don't use a GridLayout. A GridLayout will always resize each component to be of equal size.

Maybe you should use a vertical BoxLayout .

Read the section from the Swing tutorial on Layout Managers for more information and working examples.

As already indicated, you should consider to use a BoxLayout. Here is how to do this:

  private static void createAndShowGUI() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 250);

        JPanel pane0 = new JPanel();
        pane0.setLayout(new BoxLayout(pane0, BoxLayout.Y_AXIS));

        pane0.add(new JLabel("This is line #1.", JLabel.CENTER));
        pane0.add(new JLabel("This is line #2.", JLabel.CENTER));
        pane0.add(new JLabel("This is line #3.", JLabel.CENTER));
        pane0.add(new JLabel("This is line #4.", JLabel.CENTER));
        pane0.add(new JLabel("This is line #5.", JLabel.CENTER));

        frame.add(pane0);

        frame.setVisible(true);
    }

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