GridBagLayout 2 JPanels - 宽度比其他宽度少一个

时间:2013-08-20 10:03:26

标签: java swing layout jpanel gridbaglayout

我想知道我将如何在一个JPanel上使用两个JPanel,所有这些都使用GridBagLayout。

基本上你有最多的JPanel,然后有2个JPanels,如下所示:

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

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

有什么想法吗?

1 个答案:

答案 0 :(得分: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所知的实际内容将成为其中的一部分,因此难以呈现其真实大小。

输出:

GridBagLayoutExample

编辑:

更新我的代码,以便更好地解释我所说的内容,尽管我也从评论中使用了 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);
    }
}
相关问题