有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

表单Java Swing BoxLayout

我创建了一个带有Y轴排序框的表单。每个框包含一个JLabel和一个JList。此框的内容是X轴订购的。(图片)

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

BOXLAYOUT

这是我正在使用的代码。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周围加上黑色边框

BlackBorderJlist

解决它: 列表设置顺序(新的线边框(Color.darkGray,1))

最终结果:

RESULT


共 (1) 个答案

  1. # 1 楼答案

    我自己,我会用GridBagLayout来做这样的事情。例如,虽然这并不是对问题的完美表述,我使用JTextFields,而您使用JLists,但您会想到:

    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();
             }
          });
       }
    }
    

    显示时会显示:

    enter image description here