如何在 Java 小程序中将 GroupLayout 的内容居中
我想将 GroupLayout 的内容集中在 Java 小程序中。经过长时间的研究,我仍然不知道该怎么做。
下面的代码显示了我的小程序。所有元素都以布局为中心,但布局不以小程序为中心。
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,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(pb,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(l,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(b,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE))
);
layout.setVerticalGroup(
layout.createSequentialGroup()
.addComponent(c,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(pb,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(l,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(b,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
);
我会感谢任何帮助。
I would like to center content of a GroupLayout in Java applet. After long research I still cannot figure out how to do it.
Code below shows my applet. All elements are centered to layout but the layout is not center to applet.
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,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(pb,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(l,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(b,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE))
);
layout.setVerticalGroup(
layout.createSequentialGroup()
.addComponent(c,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(pb,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(l,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(b,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
);
I will appriciate any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
重要的行是:
Screenshot
SSCCE
“完整”79 行代码是:
The important lines are:
Screenshot
SSCCE
The 'full' 79 line code is:
您可以从 Box 工厂创建垂直和水平 Glue 元素,并将它们添加到布局的两侧。当布局布置好后,胶水会膨胀并迫使布局的其余部分进入中心。
You can create vertical and horizontal Glue elements from the Box factory and add these to the sides of your layout. When the layout is set out, the glue expands and forces the rest of the layout into the center.