我不能让我的三个面板正确对齐。我基本上有一个大的面板和两个较小的(不一定相同)。大的在左边,两个小的在右边,一个在另一个的上面。组件应该保持可动态调整大小。
我想要的是这样的(9是大的,1是小的,2是另一个小的):
999999 111
999999 111
999999 111
999999 222
999999 222我得到的是这样的:
999999 111
999999 111
999999 111
999999
999999
222
222我的代码如下所示,其中图形滚动面板大,工作面板和信息面板小:
private void createLayout(GroupLayout groupLayout) {
groupLayout.setHorizontalGroup(
groupLayout.createSequentialGroup()
.addContainerGap()
.addComponent(graphSimScrollPane, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap(20, Short.MAX_VALUE)
.addGroup(groupLayout.createParallelGroup(Alignment.CENTER)
.addComponent(workPanel, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(informationPanel, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
groupLayout.setVerticalGroup(
groupLayout.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(Alignment.CENTER)
.addComponent(graphSimScrollPane, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(workPanel, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addComponent(informationPanel, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
guiFrame.getContentPane().setLayout(groupLayout);
}发布于 2013-02-14 23:15:43
您的GroupLayout的基本结构类似于下面的代码:
JPanel pnl = new JPanel();
GroupLayout l = new GroupLayout(pnl);
pnl.setLayout(l);
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
l.setHorizontalGroup(
l.createSequentialGroup()
.addComponent(b1)
.addGroup(l.createParallelGroup()
.addComponent(b2)
.addComponent(b3)));
l.setVerticalGroup(
l.createParallelGroup()
.addComponent(b1)
.addGroup(l.createSequentialGroup()
.addComponent(b2)
.addComponent(b3)));
JFrame f = new JFrame("test");
f.setContentPane(pnl);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(1024, 768);
f.setVisible(true);简单地说,横轴上的约束是"b1,然后是并行的b2和b3“。垂直轴上的约束为"b1与由b1然后b2组成的组并行“。
显然,这里的可调整大小属性是不匹配的。您需要提供更多详细信息,说明您希望组件如何根据其大小进行操作。
一种可能是为这两个小组件提供整个垂直空间,但只给它们首选的水平空间。当然,给大组件留出剩余的空间。
l.setHorizontalGroup(
l.createSequentialGroup()
.addComponent(b1, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(l.createParallelGroup()
.addComponent(b2, 0, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(b3, 0, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)));
l.setVerticalGroup(
l.createParallelGroup()
.addComponent(b1, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(l.createSequentialGroup()
.addComponent(b2, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(b3, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));请注意,其结构与第一个代码相同,只是添加了大小参数。
发布于 2013-02-14 23:13:42
我真的不知道如何使用GroupLayout,但是如果您愿意使用GridBagLayout完成此任务,那么此示例代码可能可以为您完成此任务。请务必看一看:
import javax.swing.*;
import java.awt.*;
import java.util.Random;
/**
* Created with IntelliJ IDEA.
* User: Gagandeep Bali
* Date: 2/14/13
* Time: 8:18 PM
* To change this template use File | Settings | File Templates.
*/
public class GridBagExample
{
private GridBagConstraints gbc;
private Random random;
private JPanel largePanel;
private JPanel smallPanel;
private JPanel superSmallPanel;
public GridBagExample()
{
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
random = new Random();
}
private void displayGUI()
{
JFrame frame = new JFrame("GridBagLayout Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = getPanel();
contentPane.setLayout(new GridBagLayout());
largePanel = getPanel();
contentPane.add(largePanel, getConstraints(
GridBagConstraints.BOTH, 0, 0, 1, 2, 0.7f, 1.0f));
smallPanel = getPanel();
contentPane.add(smallPanel, getConstraints(
GridBagConstraints.BOTH, 1, 0, 1, 1, 0.3f, 0.7f));
superSmallPanel = getPanel();
contentPane.add(superSmallPanel, getConstraints(
GridBagConstraints.BOTH, 1, 1, 1, 1, 0.3f, 0.3f));
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel getPanel()
{
JPanel panel = new JPanel();
panel.setOpaque(true);
panel.setBackground(getColor());
panel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(getColor(), 5),
BorderFactory.createEmptyBorder(5, 5, 5, 5)));
return panel;
}
private Color getColor()
{
return (new Color(
random.nextFloat(), random.nextFloat()
, random.nextFloat(), random.nextFloat()));
}
private GridBagConstraints getConstraints(
int filler, int x, int y, int w, int h
, float weightx, float weighty)
{
gbc.fill = filler;
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = w;
gbc.gridheight = h;
gbc.weightx = weightx;
gbc.weighty = weighty;
return gbc;
}
public static void main(String... args)
{
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
new GridBagExample().displayGUI();
}
});
}
}https://stackoverflow.com/questions/14876159
复制相似问题