且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何调整JPanel的大小以适合"docknorth"中的JFrame.不会干扰其余的JPanel

更新时间:2023-12-05 18:59:22

我从没使用过MigLayout,而且我个人不知道是否可以使用默认Java LayoutManager完成此操作.

I have never used MigLayout, and personally dont see the reason if it can be done using default java LayoutManager.

好的,所以我使用了FlowLayout GridBagLayout的组合以及gc.fill=GridBagConstraints.NONEgc.anchor=GridBagConstraints.WEST用于那些我们不想填充 contentpane 宽度的面板,也根据您的注释进行了更新,以停止JPanel/当添加更多JLabel时,JFrame变得大于给定的最大宽度,这是使用JScrollPane:

Okay so I used a combination FlowLayout and GridBagLayout to achieve this, along with gc.fill=GridBagConstraints.NONE and gc.anchor=GridBagConstraints.WEST for those panels which we dont want to fill the contentpane width, also updated as per your comment to stop the JPanel/JFrame from growing larger than the given max width when more JLabels are added this was done using a JScrollPane:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

public class Test {

    public Test() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLayout(new GridBagLayout());

        final JPanel labelPanel = new JPanel();
        labelPanel.setBorder(new LineBorder(Color.black));
        for (int i = 0; i < 5; i++) {
            labelPanel.add(new JLabel("Label" + (i + 1)));
        }

        final int maxWidth = 200;
        final JScrollPane jsp = new JScrollPane(labelPanel) {
            @Override
            public Dimension getPreferredSize() {
                //we set the height by checking if we exceeed the wanted ith thus a scrollbar will appear an we must incoprate that or labels wont be shpwn nicely
                return new Dimension(maxWidth, labelPanel.getPreferredSize().width < maxWidth ? (labelPanel.getPreferredSize().height + 5) : ((labelPanel.getPreferredSize().height + getHorizontalScrollBar().getPreferredSize().height) + 5));
            }
        };

        JPanel otherPanel = new JPanel();
        otherPanel.add(new JLabel("label"));
        otherPanel.setBorder(new LineBorder(Color.black));

        JPanel otherPanel2 = new JPanel();
        otherPanel2.add(new JLabel("label 1"));
        otherPanel2.add(new JLabel("label 2"));
        otherPanel2.setBorder(new LineBorder(Color.black));

        GridBagConstraints gc = new GridBagConstraints();

        gc.fill = GridBagConstraints.BOTH;
        gc.weightx = 1.0;
        gc.weighty = 1.0;
        gc.gridx = 0;
        gc.gridy = 0;
        frame.add(jsp, gc);

        gc.fill = GridBagConstraints.NONE;
        gc.anchor = GridBagConstraints.WEST;
        gc.gridy = 1;
        frame.add(otherPanel, gc);
        gc.anchor = GridBagConstraints.WEST;
        gc.gridy = 2;
        frame.add(otherPanel2, gc);

        frame.pack();
        frame.setVisible(true);
        frame.revalidate();
        frame.repaint();
    }

    public static void main(String[] args) {
        //Create Swing components on EDT
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Test();
            }
        });
    }
}