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

我想弄清楚如何完全使用GridBagLayout在一个JPanel上拥有两个JPanel.

 

基本上,您拥有最高的JPanel,然后按以下要求拥有2个JPanel:

 

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

基本上,我需要JPanel 1大于JPanel2.但是JPanel 2也不能调整大小,JPanel 1仅在调整主面板大小时才可以调整大小.

有任何想法吗?

最佳答案

看一下此代码示例,看看这是否是您想要的:

 

 

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已知的实际内容(它将成为它的一部分),因此很难显示其真实大小.

输出:

编辑:

更新我的代码以更好地解释我的意思,尽管我也从注释中使用了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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值