4

3 つのパネルを正しく整列させることができません。私は基本的に1つの大きなパネルと2つの小さな(必ずしも同一ではない)パネルを持っています. 大きい方が左側に、小さい方が右側に 2 つ、上下に 1 つずつあります。コンポーネントは、動的にサイズ変更可能なままにする必要があります。

私が欲しいのはこれです(9は大きいもの、1は小さいものの1つ、2はもう1つの小さいものです):

999999 111
999999 111
999999 111
999999 222
999999 222

私が得るものはこれです:

999999 111
999999 111
999999 111
999999 
999999 
       222
       222

私のコードは次のとおりで、graphsimscrollpane が大きく、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);
}
4

2 に答える 2

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 と b2 で構成されるグループに平行な b1」です。

明らかに、ここではサイズ変更のプロパティが欠落しています。コンポーネントのサイズに関してコンポーネントをどのように動作させたいかについて、詳細を指定する必要があります。

1 つの可能性は、2 つの小さなコンポーネントに垂直方向のスペース全体を与え、優先される水平方向のスペースのみを与えることです。そしてもちろん、大きなコンポーネントに残りのスペースを与えます。

    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-14T15:15:42.810 に答える
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();
            }
        });
    }
}
于 2013-02-14T15:13:42.277 に答える