How to make components normal sized in GroupLayout in Java
I am trying to make a layout for my applet, but I cannot handle one problem - s开发者_运维问答ome of the elements (e.g. JComboBox) are as big as they can be - take all place in applet. setSize function do not work. What can I do to make them normal sized? (some elements e.g. JButtons and JLabels have correct sizes).
My code:
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);
Try using the setPreferredSize()
method.
精彩评论