我在 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);
}
});
}
}