开发者

How to make components normal sized in GroupLayout in Java

开发者 https://www.devze.com 2023-03-13 14:05 出处:网络
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 functio

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消