Java使用grouplayout对齐3个面板

时间:2013-02-14 13:41:40

标签: java swing layout-manager grouplayout

我不能让我的3个面板正确对齐。 我基本上有1个大面板和2个较小(不一定相同)的面板。 大的一个在左边,两个小的在右边,一个在另一个之上。组件应保持动态可调整大小。

我想要的是这个(9是大的,1是小的,2是另一个小):

999999 111
999999 111
999999 111
999999 222
999999 222

我得到的是:

999999 111
999999 111
999999 111
999999 
999999 
       222
       222

我的代码如下,graphimscrollpane大一个,workpanel和informationpanel小一些:

    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);
}

2 个答案:

答案 0 :(得分:4)

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)));

请注意,结构与第一个代码相同,只添加了尺寸参数。

答案 1 :(得分:3)

我真的不知道,如何使用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();
            }
        });
    }
}