如何在 Java 中压缩 BoxLayout

How to Condense BoxLayout in Java

我正在为嵌套在其他一些面板中的面板使用垂直 BoxLayout。其他面板的大小导致我的目标面板中的对象以难看的方式展开。如何让面板从上到下显示对象而不需要太多填充?

以下是类似的代码,尽管它生成的微调器会填满整个面板,而不是相距很远的正常外观微调器。

 import java.awt.Dimension;
 import javax.swing.BoxLayout;
 import javax.swing.JFrame;
 import javax.swing.JLabel;
 import javax.swing.JPanel;
 import javax.swing.JSpinner;
 import javax.swing.SpinnerNumberModel;

 public class PanelTest {

  public static void main(String[] args){
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));

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

    JPanel spinPanel1 = new JPanel();
    spinPanel1.setLayout(new BoxLayout(spinPanel1, BoxLayout.X_AXIS));
    spinPanel1.add(new JLabel("top"));
    SpinnerNumberModel spinMod1 = new SpinnerNumberModel(0,0,10,1);
    spinPanel1.add(new JSpinner(spinMod1));
    leftPanel.add(spinPanel1);

    JPanel spinPanel2 = new JPanel();
    spinPanel2.setLayout(new BoxLayout(spinPanel2, BoxLayout.X_AXIS));
    spinPanel2.add(new JLabel("bottom"));
    SpinnerNumberModel spinMod2 = new SpinnerNumberModel(0,0,10,1);
    spinPanel2.add(new JSpinner(spinMod2));
    leftPanel.add(spinPanel2);

    JPanel rightPanel = new JPanel();
    rightPanel.setPreferredSize(new Dimension(800,600));

    mainPanel.add(leftPanel);
    mainPanel.add(rightPanel);
    frame.getContentPane().add(mainPanel);
    frame.pack();
    frame.setVisible(true);
  }
 }

有很多方法可以做到这一点,主要涉及添加具有(约束/)布局的面板,该布局将遵循首选大小。在这种情况下,我们使用具有默认约束的 GridBagLayout 。添加到其中的组件 (leftPanel) 在可用的 space.

中居中

每个面板都有彩色边框,以使其范围更加明显。这是调试“布局压缩”问题时使用的一种方便的可视化技术。

顺便说一句,我认为 leftPanel 看起来很拥挤,而且未对齐的微调器进一步影响了外观。我可能会对标签和文本字段(全部在一个面板中)使用 GridBagLayout,并使用一些填充和右对齐的标签。

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

public class PanelTest {

    public static void main(String[] args) {
        // any single component added to this will be centered & have
        // its preferred size honored.
        JPanel constrainSpinnerPanel = new JPanel(new GridBagLayout());
        constrainSpinnerPanel.setBorder(new LineBorder(Color.CYAN, 4));
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel mainPanel = new JPanel();
        mainPanel.setBorder(new LineBorder(Color.RED,4));
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));

        JPanel leftPanel = new JPanel();
        leftPanel.setBorder(new LineBorder(Color.BLUE,4));
        leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));

        JPanel spinPanel1 = new JPanel();
        spinPanel1.setLayout(new BoxLayout(spinPanel1, BoxLayout.X_AXIS));
        spinPanel1.add(new JLabel("top"));
        SpinnerNumberModel spinMod1 = new SpinnerNumberModel(0, 0, 10, 1);
        spinPanel1.add(new JSpinner(spinMod1));
        leftPanel.add(spinPanel1);

        JPanel spinPanel2 = new JPanel();
        spinPanel2.setLayout(new BoxLayout(spinPanel2, BoxLayout.X_AXIS));
        spinPanel2.add(new JLabel("bottom"));
        SpinnerNumberModel spinMod2 = new SpinnerNumberModel(0, 0, 10, 1);
        spinPanel2.add(new JSpinner(spinMod2));
        leftPanel.add(spinPanel2);

        JPanel rightPanel = new JPanel();
        rightPanel.setBorder(new LineBorder(Color.GREEN, 4));
        rightPanel.setPreferredSize(new Dimension(400, 100));

        constrainSpinnerPanel.add(leftPanel);
        mainPanel.add(constrainSpinnerPanel);
        //mainPanel.add(leftPanel);
        mainPanel.add(rightPanel);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setVisible(true);
    }
}