3

6 つのパネルがあり、すべてグリッド レイアウトを使用しています。そして、gridbaglayoutを使用してそれらをまとめました。これが私が望んでいたデザインです

レイアウト

「2番目」のパネルは右に遠くなり、3番目のパネルは左側に大きく圧迫され、惨事でした。これがグリッドバッグレイアウトの私のコードです

    c.gridx = 0; c.gridy = 0;
    add (first,c); 

    c.gridx = 2; //so that the second panel starts from the center and is divided evenly with the first panel
    add(second,c);

    c.gridx = 0; c.gridy = 1;
    add(third,c);

    c.gridx = 1; 
    add(fourth,c);

    c.gridx = 2;
    add(fifth,c);

    c.gridx = 3;
    add(sixth,c);

どんな助けでも大歓迎です。

4

4 に答える 4

6

あなたが書いたように、これが占有するc.gridx = 0 and c.gridy = 0列の数を指定するのを忘れました。JPanelそれを指定するには、 を使用する必要があります。これは、2 列分のスペースが必要c.gridwidth = 2であることをレイアウトに伝えます。JPanel

次のような出力を取得するには:

グリッドバッグレイアウトの例

ここに小さな実用的な例があります:

import java.awt.*;
import javax.swing.*;
// http://stackoverflow.com/questions/10968853/two-jpanels-in-jframe-one-under-other
public class GridBagLayoutExample
{
    // http://stackoverflow.com/questions/10977017/grid-bag-layout-not-displaying-the-way-i-want
    private void displayGUI()
    {
        JFrame frame = new JFrame("GridBagLayout Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.FIRST_LINE_START;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 2;
        gbc.weightx = 0.5;
        gbc.weighty = 0.2;

        JPanel topLeftPanel = new JPanel();
        topLeftPanel.setOpaque(true);
        topLeftPanel.setBackground(Color.DARK_GRAY);
        contentPane.add(topLeftPanel, gbc);

        gbc.gridx = 2;

        JPanel topRightPanel = new JPanel();
        topRightPanel.setOpaque(true);  
        topRightPanel.setBackground(Color.BLUE);
        contentPane.add(topRightPanel, gbc);

        gbc.gridwidth = 1;
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.weightx = 0.25;
        gbc.weighty = 0.8;

        JPanel firstPanel = new JPanel();
        firstPanel.setOpaque(true);
        firstPanel.setBackground(Color.RED);
        contentPane.add(firstPanel, gbc);

        gbc.gridx = 1;

        JPanel secondPanel = new JPanel();
        secondPanel.setOpaque(true);
        secondPanel.setBackground(Color.GREEN.darker());
        contentPane.add(secondPanel, gbc);

        gbc.gridx = 2;

        JPanel thirdPanel = new JPanel();
        thirdPanel.setOpaque(true);
        thirdPanel.setBackground(Color.WHITE);
        contentPane.add(thirdPanel, gbc);

        gbc.gridx = 3;

        JPanel fourthPanel = new JPanel();
        fourthPanel.setOpaque(true);
        fourthPanel.setBackground(Color.MAGENTA);
        contentPane.add(fourthPanel, gbc);

        frame.setContentPane(contentPane);
        frame.setSize(200, 300);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new GridBagLayoutExample().displayGUI();
            }           
        });
    }
}
于 2012-06-11T11:58:37.570 に答える
4

(別の) 別のネストされたレイアウトを次に示します。パネル 3 から 6 は余分な高さを取得し、余分な幅は 6 つのパネルすべてに均等に分割されます。

SixPanelNested

import java.awt.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

public class SixPanelNested {

    SixPanelNested() {
        JPanel gui = new JPanel(new BorderLayout());
        gui.setBorder(new TitledBorder("BorderLayout()"));

        JPanel north = new JPanel(new GridLayout(1,0));
        north.setBorder(new TitledBorder("GridLayout(1,0)"));
        gui.add(north, BorderLayout.NORTH);
        for (int ii=1; ii<3; ii++) {
            JLabel l = new JLabel("Panel " + ii);
            l.setBorder(new LineBorder(Color.BLACK));
            north.add(l);
        }

        JPanel south = new JPanel(new GridLayout(1,0));
        south.setBorder(new TitledBorder("GridLayout(1,0)"));
        gui.add(south);
        for (int ii=3; ii<7; ii++) {
            JLabel l = new JLabel("Panel " + ii);
            l.setBorder(new LineBorder(Color.BLACK));
            south.add(l);
        }

        JOptionPane.showMessageDialog(null, gui);
    }

    public static void main(String[] args) throws Exception {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new SixPanelNested();
            }
        });
    }
}
于 2012-06-11T09:26:23.000 に答える
3

私は、GridBagLayoutこれを修正するためにコードに何を改善できるかをすばやく見つけるには十分な知識がありません。ただし、ネストされたを使用して、別のアプローチが可能になる場合がありますBorderLayout

パネル「first」、「third」、および「fourth」は、それぞれ、、およびにaを付けてパネル内に配置JPanel AできBorderLayoutます。JPanelのパネル「2番目」、「5番目」、「6番目」についても同じことができます。NORTHWESTEASTB

次に、これら2つのパネル(AおよびB)を別のパネルにBorderLayoutグループ化WESTします。EAST

于 2012-06-11T09:03:11.393 に答える
2

これを追加:

c.gridwidth = 2;

FIRST および SECOND パネルを追加する前に、他のパネルを追加する前に gridwidth を 1 に戻します。または、上記で提案したように、それらをさらに分離することもできます。行ごとに 2 つの FlowLayout を使用し、その 2 つを NORTH/SOUTH の BorderLayout を持つ別のパネルに追加できます。

于 2012-06-11T09:14:02.753 に答える