4

我正在用 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;
}

没有支柱它看起来像: 在此处输入图像描述

使用支柱它看起来像(我知道我在图片编辑方面很烂):

在此处输入图像描述

4

3 回答 3

5

您正在BoxBoxLayout.

正如javadoc所说,createHorizontalStrut(int width)

创建一个不可见的、固定宽度的组件。在水平框中,您通常使用此方法在两个组件之间强制留出一定的空间。在垂直框中,您可以使用此方法强制框至少为指定宽度。除非有多余的空间可用,否则不可见组件没有高度,在这种情况下,它会占用可用空间的份额,就像任何其他没有最大高度的组件一样。

因此,它填充了您的标题JLabelJPanel.

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

或者,您可以重新考虑布局JPanel- 看看视觉指南​​。

为了将来参考,如果您无法理解 Swing 布局,请尝试在您不确定的 sLineBorder中添加颜色。JComponent在这种情况下,Boxstruts 不是JComponents 而是Components,因此您必须将它们放入 aJPanel中,但这至少可以向您展示每个组件在顶层 . 中占用的空间JPanel

于 2012-05-24T21:35:11.043 回答
3
于 2012-05-24T21:18:23.397 回答
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();
         }
      });
   }
}
于 2012-05-24T21:35:37.353 回答