BoxLayout误解了支柱

时间:2012-05-24 20:28:37

标签: java swing boxlayout

我在Swing中编写了一个简单的输入图。我使用boxLayout创建一个简单的用户输入GUI。问题是在所有标签的JPanel和JTextFields的JPanel之间创建一个水平支柱会导致整个面板向下移动(奇怪)这是整个面板:

private JPanel secondCard() {

    //main panel. set the boxlayout
    secondCard = new JPanel();
    secondCard.setLayout(new BoxLayout(secondCard,BoxLayout.Y_AXIS));

    // create vertical strut for looks
    secondCard.add(Box.createVerticalStrut(20));

    // create title. center it.
    JLabel title = new JLabel("Configure main network parameters "); 
    title.setAlignmentX(CENTER_ALIGNMENT);
    secondCard.add(title);

    // create vertical strut for looks
    secondCard.add(Box.createVerticalStrut(20));

    // create panel for the description labels
    JPanel labelPanel = new JPanel();
    labelPanel.setLayout(new BoxLayout(labelPanel,BoxLayout.Y_AXIS));
    labelPanel.setAlignmentX(LEFT_ALIGNMENT);

    JLabel inPut =new JLabel("number of inputs");
    inPut.setAlignmentX(LEFT_ALIGNMENT);
    labelPanel.add(inPut);

    inPut =new JLabel("number of outputs");
    inPut.setAlignmentX(LEFT_ALIGNMENT);
    labelPanel.add(inPut);

    inPut =new JLabel("number of layers");
    inPut.setAlignmentX(LEFT_ALIGNMENT);
    labelPanel.add(inPut);

    JPanel textFieldPanel = new JPanel();
    textFieldPanel.setLayout(new BoxLayout(textFieldPanel,BoxLayout.Y_AXIS));
    textFieldPanel.setAlignmentX(LEFT_ALIGNMENT);

    JTextField inputTextField = new JTextField();
    inputTextField.setAlignmentX(LEFT_ALIGNMENT);
    textFieldPanel.add(inputTextField);
    inputTextField.setMinimumSize(new Dimension(0,0));

    inputTextField = new JTextField();
    inputTextField.setAlignmentX(LEFT_ALIGNMENT);
    textFieldPanel.add(inputTextField);
    inputTextField.setMinimumSize(new Dimension(0,0));

    inputTextField = new JTextField();
    inputTextField.setAlignmentX(LEFT_ALIGNMENT);
    textFieldPanel.add(inputTextField);
    inputTextField.setMinimumSize(new Dimension(0,0));

    textFieldPanel.setMaximumSize(new Dimension(50, labelPanel.getMaximumSize().height));

    JPanel inputPanel = new JPanel();
    inputPanel.setLayout(new BoxLayout(inputPanel,BoxLayout.X_AXIS));
    inputPanel.setAlignmentX(CENTER_ALIGNMENT);

    inputPanel.add(labelPanel);

    //this is the problem strut!! it causes inputPanel to shift downwards 
    inputPanel.add(Box.createHorizontalStrut(20));

    inputPanel.add(textFieldPanel);

    secondCard.add(inputPanel);

    return secondCard;
}

没有支柱它看起来像: enter image description here

用strut看起来(我知道我很喜欢图片编辑):

enter image description here

3 个答案:

答案 0 :(得分:4)

您正在向Box添加BoxLayout支柱。

正如javadoc所述,createHorizontalStrut(int width)

  

创建一个不可见的固定宽度组件。在一个水平的盒子里,你   通常使用此方法强制之间的一定量的空间   两个组件。在垂直框中,您可以使用此方法强制使用   该框至少是指定的宽度。隐形组件   除非有足够的空间,否则没有高度,在这种情况下需要   它的可用空间份额,就像任何其他组件一样   没有最大高度。

因此,它填充了标题JLabelJPanel底部之间的高度。

您可能需要考虑使用Box.createRigidArea(new Dimension(20, height)),其中可以指定高度或将其设置为labelPanel的高度。

或者,您可以重新考虑JPanel的布局 - 看看visual guide

为了将来参考,如果您无法理解Swing布局,请尝试向您不确定的LineBorder添加彩色JComponent。在这种情况下,Box struts不是JComponent而是Component s,因此您必须将它们放入JPanel,但这至少会显示出来您在顶级JPanel中占用的每个组件的空间。

答案 1 :(得分:3)

答案 2 :(得分:2)

嵌套布局的示例,一个使用BorderLayout,FlowLayout(JPanel的默认值)和GridBagLayout:

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.HashMap;
import java.util.Map;

import javax.swing.*;

public class LayoutFoo {
   private static final String TITLE = "Configure Main Foobar Parameters";
   private static final String[] LABEL_TEXTS = {
      "Number of Spams", "Number of Frapzats", "Number of Zignuts"
   };
   private static final int TEXTFIELD_SIZE = 10;
   private static final Insets WEST_INSETS = new Insets(5, 5, 5, 10);
   private static final Insets EAST_INSETS = new Insets(5, 10, 5, 5);
   private static final int EB_GAP = 5;
   private Map<String, JTextField> textFieldMap = new HashMap<String, JTextField>();

   public JPanel getConfigFooPanel() {
      JPanel textFieldPanel = new JPanel(new GridBagLayout());
      for (int i = 0; i < LABEL_TEXTS.length; i++) {
         addTextAndField(textFieldPanel, LABEL_TEXTS[i], i);
      }

      int blVertGap = 20;
      JPanel borderLayoutPanel = new JPanel(new BorderLayout(0, blVertGap));
      borderLayoutPanel.setBorder(BorderFactory.createEmptyBorder(EB_GAP, EB_GAP,
            EB_GAP, EB_GAP));
      JLabel titleLabel = new JLabel(TITLE, JLabel.CENTER);
      borderLayoutPanel.add(titleLabel, BorderLayout.PAGE_START);
      borderLayoutPanel.add(textFieldPanel, BorderLayout.CENTER);

      JPanel outerWrapperFlowPanel = new JPanel();
      outerWrapperFlowPanel.add(borderLayoutPanel);

      return outerWrapperFlowPanel;      
   }

   public String getFieldText(String labelText) {
      JTextField field = textFieldMap.get(labelText);
      if (field == null) {
         return ""; // ?? throw exception
      } else {
         return field.getText();
      }
   }

   private void addTextAndField(JPanel panel, String text, int i) {
      JLabel label = new JLabel(text, JLabel.LEFT);
      JTextField textField = new JTextField(TEXTFIELD_SIZE);
      textFieldMap.put(text, textField);
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridx = 0;
      gbc.gridy = i;
      gbc.gridwidth = 1;
      gbc.gridheight = 1;
      gbc.weightx = 1.0;
      gbc.weighty = 1.0;
      gbc.fill = GridBagConstraints.HORIZONTAL;
      gbc.anchor = GridBagConstraints.WEST;
      gbc.insets = WEST_INSETS;
      panel.add(label, gbc);

      gbc.gridx = 1;
      gbc.anchor = GridBagConstraints.EAST;
      gbc.insets = EAST_INSETS;
      panel.add(textField, gbc);
   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("LayoutFoo");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new LayoutFoo().getConfigFooPanel());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}