我正在尝试为我的applet 进行布局,但我无法处理一个问题- 一些元素(例如JComboBox)尽可能大- 在applet 中全部放置。setSize 函数不起作用。我该怎么做才能使它们正常大小?(某些元素,例如 JButtons 和 JLabels 具有正确的尺寸)。
我的代码:
JPanel cp=new JPanel();
String[] s = new String[2];
s[0] = "Price";
s[1] = "Name";
JComboBox c = new JComboBox(s);
JProgressBar pb=new JProgressBar(17, 23);
pb.setValue(20);
pb.setStringPainted(true);
JLabel l=new JLabel("Name of product");
JButton b=new JButton("Send a message");
b.setEnabled(true);
cp.add(c);
cp.add(pb);
cp.add(l);
cp.add(b);
GroupLayout layout = new GroupLayout(cp);
cp.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
.addComponent(c)
.addComponent(pb)
.addComponent(l)
.addComponent(b))
);
layout.setVerticalGroup(
layout.createSequentialGroup()
.addComponent(c)
.addComponent(pb)
.addComponent(l)
.addComponent(b)
);
add(cp);