3

我想知道我将如何在一个 JPanel 上拥有两个 JPanel,全部使用 GridBagLayout。

基本上,您拥有 Top Most JPanel,然后有 2 个 JPanel,其要求如下:

--------------------------------------
|                          |         |
|                          |         |
|                          |         |
|     JPanel 1             | JPanel  |
|                          |    2    |
|                          |         |
|                          |         |
--------------------------------------

基本上我需要 JPanel 1 比 JPanel 2 大。但 JPanel 2 也不能调整大小,JPanel 1 只有在主面板调整大小时才应该调整大小。

有任何想法吗?

4

1 回答 1

3

看看这个代码示例,看看这是否是你想要的:

import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;

public class GridBagExample {
    private JPanel leftPanel;
    private JPanel rightPanel;

    private GridBagConstraints gbc;
    private Random random;

    public GridBagExample() {
        gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.FIRST_LINE_START;
        gbc.insets = new Insets(5, 5, 5, 5);

        random = new Random();
    }

    private void displayGUI() {
        JFrame frame = new JFrame("Swing Worker Example");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = getPanel();
        contentPane.setLayout(new GridBagLayout());
        leftPanel = getPanel();
        rightPanel = getPanel();

        addComp(contentPane, leftPanel, 0, 0, 1, 1,
                    GridBagConstraints.BOTH, 0.7, 1.0);
        addComp(contentPane, rightPanel, 1, 0, 1, 1,
                    GridBagConstraints.BOTH, 0.3, 1.0);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private void addComp(JPanel panel, JComponent comp, int gridx,
                            int gridy, int gridwidth, int gridheight,
                                int fill, double weightx, double weighty) {
        gbc.gridx = gridx;
        gbc.gridy = gridy;
        gbc.gridwidth = gridwidth;
        gbc.gridheight = gridheight;
        gbc.fill = fill;
        gbc.weightx = weightx;
        gbc.weighty = weighty;

        panel.add(comp, gbc);
    }

    private JPanel getPanel() {
        JPanel panel = new JPanel();
        panel.setOpaque(true);
        panel.setBackground(new Color(random.nextInt(256),
                            random.nextInt(256), random.nextInt(256)));

        return panel;
    }

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                new GridBagExample().displayGUI();
            }
        };
        EventQueue.invokeLater(runnable);
    }
}

虽然如果你希望你的权利JPanel不改变大小,你可以传递GridBagConstraints.NONE给函数,而不是GridBagConstraints.BOTH像我做的那样。由于没有已知的实际内容JPanel将成为它的一部分,因此很难呈现其真实大小。

输出 :

GridBagLayout 示例

编辑 :

更新我的代码,以更好地解释我在说什么,尽管我也从评论中使用了 user2699405的(OP)想法。

import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;

public class GridBagExample {
    private JPanel leftPanel;
    private JPanel rightPanel;

    private GridBagConstraints gbc;
    private Random random;

    public GridBagExample() {
        gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.FIRST_LINE_START;
        gbc.insets = new Insets(5, 5, 5, 5);

        random = new Random();
    }

    private void displayGUI() {
        JFrame frame = new JFrame("Swing Worker Example");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = getPanel();
        contentPane.setLayout(new GridBagLayout());
        leftPanel = getPanel();
        rightPanel = new JPanel() {
            @Override
            public Dimension getPreferredSize() {
                return (new Dimension(100, 100));
            }
        };

        addComp(contentPane, leftPanel, 0, 0, 1, 1,
                    GridBagConstraints.BOTH, 1.0, 1.0);
        addComp(contentPane, rightPanel, 1, 0, 1, 1,
                    GridBagConstraints.NONE, 0.0, 1.0);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private void addComp(JPanel panel, JComponent comp, int gridx,
                            int gridy, int gridwidth, int gridheight,
                                int fill, double weightx, double weighty) {
        gbc.gridx = gridx;
        gbc.gridy = gridy;
        gbc.gridwidth = gridwidth;
        gbc.gridheight = gridheight;
        gbc.fill = fill;
        gbc.weightx = weightx;
        gbc.weighty = weighty;

        panel.add(comp, gbc);
    }

    private JPanel getPanel() {
        JPanel panel = new JPanel();
        panel.setOpaque(true);
        panel.setBackground(new Color(random.nextInt(256),
                            random.nextInt(256), random.nextInt(256)));

        return panel;
    }

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                new GridBagExample().displayGUI();
            }
        };
        EventQueue.invokeLater(runnable);
    }
}
于 2013-08-20T10:35:25.710 回答