【问题标题】:Java Swing BoxLayoutJava Swing BoxLayout
【发布时间】:2014-01-19 14:11:47
【问题描述】:

我创建了一个带有 Y.AXIS 有序框的表单。每个盒子都包含一个 JLabel 和一个 JList。这个盒子的内容是 X.Axis 排序的。 (图片)

  1. 如何在框之间添加垂直间隔,使其更具可读性。
  2. 如何拆分标签和列表,让标签更多在左侧,列表更多在右侧
  3. 我也愿意接受其他使此表单更具可读性的想法。

这是我正在使用的代码。 createCustomJList 正在返回一个 JList

String ean = (String) table.getModel().getValueAt(selection[0], 0);

JPanel addinfo= new JPanel();

String[] operations=new String[{"ROHTABAK","HERSTELLER","WARENGRUPPE","MARKENLOGO"};
Box moreInfo[] = new Box[4];

for(int i=0;i<operations.length;i++){   
    moreInfo[i] = Box.createHorizontalBox();
    moreInfo[i].add(new JLabel(operations[i]));
    moreInfo[i].add(createCustomJList(database.customgetter(operations[i],ean)));
    addinfo.add(moreInfo[i]);
}

BoxLayout layout = new BoxLayout(addinfo, BoxLayout.Y_AXIS);
addinfo.setLayout(layout);

JOptionPane.showMessageDialog(null,
    addinfo, "Naehere Infos",
    JOptionPane.OK_CANCEL_OPTION);

编辑: 我尝试了使用 gridlayout 的解决方案,但改用了 JLists。

有没有办法在 jlist 周围设置黑色边框?

已解决编辑: list.setBorder(new LineBorder(Color.darkGray, 1));

最终结果:

【问题讨论】:

标签: java forms swing layout-manager boxlayout


【解决方案1】:

我自己,我会使用 GridBagLayout 来做这样的事情。例如,虽然这不是您问题的完美再现,我在您使用 JLists 的地方使用 JTextFields,但您明白了:

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

import javax.swing.*;

@SuppressWarnings("serial")
public class InputForm extends JPanel {
   private static final int COLUMNS = 10;
   private static final int GAP = 3;
   private static final Insets LABEL_INSETS = new Insets(GAP, GAP, GAP, 15);
   private static final Insets TEXTFIELD_INSETS = new Insets(GAP, GAP, GAP, GAP);
   private String[] labelTexts;
   private Map<String, JTextField> fieldMap = new HashMap<String, JTextField>();

   public InputForm(String[] labelTexts) {
      this.labelTexts = labelTexts;
      setLayout(new GridBagLayout());
      for (int i = 0; i < labelTexts.length; i++) {
         String text = labelTexts[i];
         JTextField field = new JTextField(COLUMNS);
         fieldMap.put(text, field);

         addLabel(text, i);
         addTextField(field, i);
      }
   }

   public String[] getLabelTexts() {
      return labelTexts;
   }

   private void addTextField(JTextField field, int row) {
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridwidth = 1;
      gbc.gridheight = 1;
      gbc.gridx = 1;
      gbc.gridy = row;
      gbc.anchor = GridBagConstraints.EAST;
      gbc.fill = GridBagConstraints.HORIZONTAL;
      gbc.insets = TEXTFIELD_INSETS;
      gbc.weightx = 1.0;
      gbc.weighty = 1.0;
      add(field, gbc);
   }

   private void addLabel(String text, int row) {
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridwidth = 1;
      gbc.gridheight = 1;
      gbc.gridx = 0;
      gbc.gridy = row;
      gbc.anchor = GridBagConstraints.WEST;
      gbc.fill = GridBagConstraints.BOTH;
      gbc.insets = LABEL_INSETS;
      gbc.weightx = 1.0;
      gbc.weighty = 1.0;
      add(new JLabel(text), gbc);
   }

   public String getFieldText(String key) {
      String text = "";
      JTextField field = fieldMap.get(key);
      if (field != null) {
         text = field.getText();
      }
      return text;
   }

   private static void createAndShowGui() {
      String[] labelTexts = new String[] { "ROHTABAK", "HERSTELLER",
            "WARENGRUPPE", "MARKENLOGO" };
      InputForm inputForm = new InputForm(labelTexts);

      int result = JOptionPane.showConfirmDialog(null, inputForm, "Naehere Infos",
            JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
      if (result == JOptionPane.OK_OPTION) {
         for (String text : labelTexts) {
            System.out.printf("%20s %s%n", text, inputForm.getFieldText(text));
         }
      }
   }

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

显示时显示:

【讨论】:

  • 希望留在 jlist 上,因为可以有更多条目并且更通用:-/
  • @StefanSpreger:我没有告诉你不要使用 JList。我只是使用 JTextFields 作为占位符,因为我没有您的任何列表数据,并希望您可以从我的回答中推断,使用我的 ideas 并确定,继续使用您的 JLists .
  • 那么没有办法更紧密地指定BoxLayout?
  • @StefanSpreger:这取决于您要达到的目标。如果您的布局外观与我发布的图像相似,那么不,不要为此使用 BoxLayout,而是使用 GridBagLayout 或 MIGLayout。如果它更接近你的图片,那么一定要调整 BoxLayout。
  • 我现在用你的方法实现了。但我现在有一个小问题。我编辑了我的问题。也许你可以看看:)
猜你喜欢
  • 2013-11-22
  • 1970-01-01
  • 2018-09-14
  • 1970-01-01
  • 2021-11-20
  • 2012-03-12
  • 1970-01-01
  • 2011-02-12
  • 2012-07-28
相关资源
最近更新 更多