如何在Java中压缩BoxLayout

时间:2018-06-14 01:57:57

标签: java swing jpanel layout-manager boxlayout

我正在使用垂直BoxLayout作为嵌套在其他面板中的面板。其他面板的大小会导致目标面板中的对象以难看的方式展开。如何在没有太多填充的情况下使面板从上到下呈现对象?

enter image description here

以下是类似的代码,虽然它会生成填充整个面板的旋转器,而不是放在相隔很远的普通旋转器。

 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);
  }
 }

1 个答案:

答案 0 :(得分:0)

有很多方法可以做到这一点,主要是让面板添加一个(约束/)布局,以满足首选尺寸。在这种情况下,我们使用带有默认约束的GridBagLayout。添加到其中的组件(leftPanel)位于可用空间的中心。

enter image description here

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

顺便说一句,我认为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);
    }
}