当前位置: 动力学知识库 > 问答 > 编程问答 >

forms - Java Swing BoxLayout

问题描述:

I created a Form with Y.AXIS ordered Boxes. Every box contains a JLabel and a JList. This boxes contents are X.Axis ordered. (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.

Here is the code I'm working with. createCustomJList is returning a 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.

Is there a way to have something like a black border around a jlist?

SOLVEDEDIT:

list.setBorder(new LineBorder(Color.darkGray, 1));

ENDRESULT:

网友答案:

Myself, I would use a GridBagLayout for something like this. For instance, while this is not a perfect renditioning of your problem, and I use JTextFields where you use JLists, you get the idea:

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:

分享给朋友:
您可能感兴趣的文章:
随机阅读: