【发布时间】:2011-09-16 03:55:39
【问题描述】:
我正在尝试为我的小程序制作布局,但我无法处理一个问题 - 一些元素(例如 JComboBox)尽可能大 - 全部放置在小程序中。 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);
【问题讨论】:
标签: java swing layout grouplayout