简体   繁体   English

Java Swing BoxLayout

[英]Java Swing BoxLayout

I created a Form with Y.AXIS ordered Boxes. 我用Y.AXIS订购盒创建了一个表格。 Every box contains a JLabel and a JList. 每个框都包含一个JLabel和一个JList。 This boxes contents are X.Axis ordered. 此框的内容是X.Axis排序的。 (picture) (图片)

  1. How can i add a vertical spacer between the boxes, to make it more readable. 我如何在两个框之间添加垂直间隔条,以使其更具可读性。
  2. How can i split the label and the list for having the label more on the left and the list more on the right 我如何拆分标签和列表,以使标签左侧更多,列表更多右侧
  3. I'm also open for other ideas making this form more readable. 我也欢迎其他想法使此表格更具可读性。

BoxLayout的

Here is the code I'm working with. 这是我正在使用的代码。 createCustomJList is returning a JList 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);

EDIT: I tried the solution with the gridlayout, but used JLists instead. 编辑:我尝试使用gridlayout解决方案,但改用JLists。

Is there a way to have something like a black border around a jlist? 有没有办法让jlist周围有黑色边框?

BlackBorderJlist

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

ENDRESULT: 最终结果:

结果

Myself, I would use a GridBagLayout for something like this. 我自己,我会使用GridBagLayout这样的东西。 For instance, while this is not a perfect renditioning of your problem, and I use JTextFields where you use JLists, you get the idea: 例如,虽然这不是对问题的完美诠释,但我在使用JList的地方使用了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();
         }
      });
   }
}

which when displayed shows: 显示时显示:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM