简体   繁体   English

为JPanel设置灵活的大小

[英]Set a flexible size for JPanel

My ContentPane contains several JPanels that in turn contain JTabbedPanes and other things. 我的ContentPane包含几个JPanels ,而这些JPanels又包含JTabbedPanes和其他内容。 How can I make those JPanels to change their size as user shrinks or expands the window. 当用户缩小或扩展窗口时,如何使这些JPanels更改其大小。 Is there any way to set up a percentage size? 有什么方法可以设置百分比大小?

JPanel panelOne = new JPanel;
panelOne.setSize( // % of );

You can use a variety of layout methods on ContentPanes. 您可以在ContentPanes上使用各种布局方法。 For something that scales based on size in relation to the canvas, you can try using a SpringLayout and define the borders in relation to a content pane itself. 对于根据画布的大小进行缩放的对象,可以尝试使用SpringLayout并相对于内容窗格本身定义边框。 Other elements can be constrained to that anchored element or to the content pane. 可以将其他元素约束到该锚定元素或内容窗格。

You can use constraints to stretch panels based on the ContentPane or other elements. 您可以使用约束来基于ContentPane或其他元素来拉伸面板。

You can visually plan SpringLayout (and other possible layouts, such as Mig, Grid, etc.) using WindowBuilder for Eclipse. 您可以使用WindowBuilder for Eclipse直观地规划SpringLayout(以及其他可能的布局,例如Mig,Grid等)。 Depending on the size and scale of your Layout, you may find other layouts to be more practical based on your requirements or preference. 根据布局的大小和比例,根据您的要求或偏好,您可能会发现其他布局更实用。

Make use of appropriate layout managers, that's what they are there for. 利用适当的布局管理器,这就是它们的作用。 They take care of all the dirty work involved in monitoring changes to the parent container and calculating the required settings based on the changes. 他们负责监视监视父容器的更改并根据更改计算所需设置的所有脏工作。

Take a look at Laying Out Components Within a Container 看看在容器布置组件

动画的gif

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

public class TestSizableComponents {

    public static void main(String[] args) {
        new TestSizableComponents();
    }

    public TestSizableComponents() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JTabbedPane tabbedPane = new JTabbedPane();
                for (int index = 0; index < 10; index++) {
                    tabbedPane.add(Integer.toString(index), new TabPane(Integer.toString(index)));
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new HeaderPane(), BorderLayout.NORTH);
                frame.add(tabbedPane);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class HeaderPane extends JPanel {

        public HeaderPane() {

            setLayout(new GridBagLayout());
            add(new JLabel("Look ma, no hands"));
            setBorder(new CompoundBorder(new LineBorder(Color.RED), new EmptyBorder(10, 10, 10, 10)));

        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 100);
        }

    }

    public class TabPane extends JPanel {

        public TabPane(String name) {

            setLayout(new GridBagLayout());
            add(new JLabel(name));
            setBorder(new EmptyBorder(10, 10, 10, 10));

        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

}

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

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