3

我在 Java 中根据需要设置 GroupLayout 时遇到问题。

我的代码如下。组件的所需放置是:

++++++++++++++++++++++++++++++++++++++++++++++++++++
+LABEL_A       caseStudyComboBox           LABEL_B +
+LABEL_C       TextfieldE                  BtnD    +
++++++++++++++++++++++++++++++++++++++++++++++++++++

而不是上面的,输出是:

 +++++++++++++++++++++++++++++++++++++++++++++++++++
 +LABEL_A                                          +
 +                caseStudyCombBox                 +
 +                                  LABEL_B        +
 +       LABEL_C IS LONGER THAN A:                 +
 +                                TextfieldE       +
 +                                           BtnD  +
 +++++++++++++++++++++++++++++++++++++++++++++++++++

请提出一些补救措施。谢谢。

import java.awt.Component;
import java.awt.Dimension;

import javax.swing.*;
import static javax.swing.GroupLayout.Alignment.*;

public class EXP1 extends JFrame {
String [] caseStudyList = {
        "",
        "Case A",
        "Case B"
};
//
public EXP1() {
    JLabel Label_A = new JLabel("LABEL A ");
    JComboBox caseStudyComboBox = new JComboBox(caseStudyList);
    JLabel Label_B = new JLabel("LABEL B");
    JLabel Label_C = new JLabel("LABEL C IS LONGER THAN A: ");
    JButton BtnD = new JButton("BUTTON D");
    JTextField TextFieldE = new JTextField();

    GroupLayout layout = new GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setAutoCreateGaps(true);
    layout.setAutoCreateContainerGaps(true);

    layout.setHorizontalGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(TRAILING))
                .addComponent(Label_A, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(Label_C, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(LEADING))
                .addComponent(caseStudyComboBox, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(TextFieldE, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(LEADING))
                .addComponent(Label_B, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(BtnD, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );

    layout.linkSize(SwingConstants.HORIZONTAL, Label_B, BtnD);

    layout.setVerticalGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(BASELINE))
                .addComponent(Label_A)
                .addComponent(caseStudyComboBox)
                .addComponent(Label_B)
            .addGroup(layout.createParallelGroup(LEADING))
                .addComponent(Label_C)
                .addComponent(TextFieldE)
                .addComponent(BtnD)
    );

    setTitle("EXPERIMENT");
    this.pack();
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
    // TODO Auto-generated method stub
    //BPAOntoEIAUI mainUI = new BPAOntoEIAUI();
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                UIManager.setLookAndFeel(
                              "javax.swing.plaf.metal.MetalLookAndFeel");
                            //  "com.sun.java.swing.plaf.motif.MotifLookAndFeel");
                            //UIManager.getCrossPlatformLookAndFeelClassName());
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            new EXP1().setVisible(true);
        }
    });
}
}
4

1 回答 1

3

当您设置水平和垂直组时,括号看起来有点混乱。控件不属于您创建的预期组。

代替

layout.setHorizontalGroup(layout.createSequentialGroup()
 .addGroup(layout.createParallelGroup())
 .addComponent(Label_A)
 .addComponent(Label_C)

它实际上应该是:

layout.setHorizontalGroup(layout.createSequentialGroup()
 .addGroup(layout.createParallelGroup()
 .addComponent(Label_A)
 .addComponent(Label_C))

在第一种情况下,您将标签添加到顺序组而不是像第二种情况那样的并行组。

这是使用以下稍微修改的代码的结果:

在此处输入图像描述

layout.setHorizontalGroup(layout.createSequentialGroup()
        .addGroup(layout.createParallelGroup(TRAILING)
            .addComponent(Label_A, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(Label_C, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        .addGroup(layout.createParallelGroup(LEADING)
            .addComponent(caseStudyComboBox, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(TextFieldE, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        .addGroup(layout.createParallelGroup(LEADING)
            .addComponent(Label_B, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(BtnD, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

layout.linkSize(SwingConstants.HORIZONTAL, Label_B, BtnD);

layout.setVerticalGroup(layout.createSequentialGroup()
        .addGroup(layout.createParallelGroup(BASELINE)
            .addComponent(Label_A)
            .addComponent(caseStudyComboBox)
            .addComponent(Label_B))
        .addGroup(layout.createParallelGroup(LEADING)
            .addComponent(Label_C)
            .addComponent(TextFieldE)
            .addComponent(BtnD))
);
于 2012-10-23T01:28:50.443 回答