嵌套布局-boxlayout中的flowlayout

7vux5j2d  于 2021-06-30  发布在  Java
关注(0)|答案(3)|浏览(295)

我有一个 controlPanel ( BoxLayout ):

controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.Y_AXIS));

现在我造了两个 FlowLayout 并将它们添加到 contolPanel 面板:

JPanel fromDatePanel = new JPanel(new FlowLayout());
JPanel untilDatePanel = new JPanel(new FlowLayout());

fromDatePanel.add(new JLabel("From - "));
fromDatePanel.add(new JButton("..."));
untilDatePanel.add(new JLabel("Until - "));
untilDatePanel.add(new JButton("..."));

controlPanel.add(fromDatePanel);
controlPanel.add(untilDatePanel);

我明白了:

为什么会在布局之间产生间隙?如果我插入 JButton 例如,它工作正常(插入时没有间隙)。
我怎样才能消除两者之间的差距 FlowLayout ? (所以就像蓝色的缺口)

b0zn9rqh

b0zn9rqh1#

可以使用以下方法实现垂直布局(垂直居中) GridBagLayout (并使用 GridBagConstraint 用一个 gridwidth=REMAINDER ):

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestGridBagLayout {

    protected void initUI() {
        JFrame frame = new JFrame();
        JPanel controlPanel = (JPanel) frame.getContentPane();
        controlPanel.setLayout(new GridBagLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;

        JPanel fromDatePanel = new JPanel(new FlowLayout());
        JPanel untilDatePanel = new JPanel(new FlowLayout());

        fromDatePanel.add(new JLabel("From - "));
        fromDatePanel.add(new JButton("..."));
        untilDatePanel.add(new JLabel("Until - "));
        untilDatePanel.add(new JButton("..."));

        controlPanel.add(fromDatePanel, gbc);
        controlPanel.add(untilDatePanel, gbc);
        frame.setSize(600, 600);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                TestGridBagLayout testMultiplePanels = new TestGridBagLayout();
                testMultiplePanels.initUI();

            }
        });
    }

}

关于 JButton 以及 JPanel 当添加到 BoxLayout ,这是由于执行的不同 getMaximumSize() 这是由 BoxLayout . JButton 返回首选大小 JPanel 会回来的 nullBoxLayout 作为无限的维度。
如果你想保留你的 BoxLayout ,您可以重写 JPanel.getMaximumSize() 然后回来 getPreferredSize() :

import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestGridBagLayout {

    protected void initUI() {
        JFrame frame = new JFrame();
        JPanel controlPanel = (JPanel) frame.getContentPane();
        controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.Y_AXIS));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel fromDatePanel = new JPanel(new FlowLayout()) {
            @Override
            public Dimension getMaximumSize() {
                return getPreferredSize();
            }
        };
        JPanel untilDatePanel = new JPanel(new FlowLayout()) {
            @Override
            public Dimension getMaximumSize() {
                return super.getMaximumSize();
            }
        };

        fromDatePanel.add(new JLabel("From - "));
        fromDatePanel.add(new JButton("..."));
        untilDatePanel.add(new JLabel("Until - "));
        untilDatePanel.add(new JButton("..."));

        controlPanel.add(fromDatePanel);
        controlPanel.add(untilDatePanel);
        frame.setSize(600, 600);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                TestGridBagLayout testMultiplePanels = new TestGridBagLayout();
                testMultiplePanels.initUI();

            }
        });
    }

}
oewdyzsn

oewdyzsn2#

有关详细信息,请参见使用不可见组件作为填充 Box.createVerticalGlue() .

zphenhs4

zphenhs43#


我曾经 BorderLayout 而且成功了。

JPanel controlPanel = new JPanel();
        //  controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.Y_AXIS));
        controlPanel.setLayout(new BorderLayout());

        JPanel fromDatePanel = new JPanel(new FlowLayout());
        JPanel untilDatePanel = new JPanel(new FlowLayout());

        fromDatePanel.add(new JLabel("From - "));
        fromDatePanel.add(new JButton("..."));
        untilDatePanel.add(new JLabel("Until - "));
        untilDatePanel.add(new JButton("..."));

        controlPanel.add(fromDatePanel, BorderLayout.NORTH);
        controlPanel.add(untilDatePanel, BorderLayout.CENTER);

编辑-1:
可以在每个面板中添加嵌套面板。

JPanel controlPanel = new JPanel();
        controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.Y_AXIS));
        // controlPanel.setLayout(new BorderLayout());

        JPanel fromDatePanel = new JPanel(new FlowLayout());
        JPanel untilDatePanel = new JPanel(new FlowLayout());
        JPanel thirdPnl = new JPanel(new FlowLayout());
        JPanel fourthPnl = new JPanel(new FlowLayout());

        JPanel thrdForthPnl = new JPanel(new BorderLayout()); 

        fourthPnl.add(new JLabel("Fourth"));
        fourthPnl.add(new JButton("4.1"));

        thirdPnl.add(new JLabel("Third"));
        thirdPnl.add(new JButton("3.1"));

        thrdForthPnl.add(thirdPnl, BorderLayout.NORTH);
        thrdForthPnl.add(fourthPnl, BorderLayout.CENTER);

        fromDatePanel.add(new JLabel("From - "));
        fromDatePanel.add(new JButton("..."));
        untilDatePanel.add(new JLabel("Until - "));
        untilDatePanel.add(new JButton("..."));

        controlPanel.add(fromDatePanel, BorderLayout.NORTH);
        controlPanel.add(untilDatePanel, BorderLayout.CENTER);
        controlPanel.add(thrdForthPnl, BorderLayout.SOUTH);

相关问题