ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

面板无法使用Java中的GridBagLayout正确调整大小

2019-11-27 15:04:44  阅读:421  来源: 互联网

标签:gridbaglayout swing jpanel java user-interface


因此,我在我的GUI中使用了GridBagLayout,但是我在调​​整大小时遇到​​了一些问题.在任何人建议其他布局之前,我很确定我想使用GridBagLayout.我将拥有大量组件,并且想要正确地布局它们,当前的代码/图像仅显示到目前为止的内容.

无论如何,问题是它无法正确调整自身大小.我希望它在调整大小时保持当前具有的长宽比,但事实并非如此.某些面板在调整大小时具有某种优先权,因此当我调整大小时,它会破坏宽高比.本质上,我的某个面板比另一个面板大,但是在调整大小后,它变得比另一个面板小,如下所示:

http://i.imgur.com/LWhHm.png

现在,我真正想要的是让他们保持已经提出的比例.如您所见,我希望GUI的聊天部分小于最初的两个左面板.

下面是我现在产生此代码的代码:

jpPack = new JPanel();
    jpCards = new JPanel();
    jpInfo = new JPanel();
    jpChat = new JPanel();


    jpPack.setBackground(Color.red);
    jpCards.setBackground(Color.blue);
    jpInfo.setBackground(Color.green);

    //create the Constraints and set some that will apply to each panel
    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.BOTH;

    //set the number of columns/rows to use
    c.gridwidth = 1;
    c.gridheight = 1;
    //set the x and y position
    c.gridx = 0;
    c.gridy = 0;
    //set the weight properties
    c.weightx = 1.0;
    c.weighty = 1.0;

    getContentPane().add(jpCards, c);

    //set the number of columns/rows to use
    c.gridwidth = 1;
    c.gridheight = 1;
    //set the x and y position
    c.gridx = 0;
    c.gridy = 1;
    //set the weight properties
    c.weightx = 1.0;
    c.weighty = 1.0;

    getContentPane().add(jpPack, c);

    //set the number of columns/rows to use
    c.gridwidth = 1;
    c.gridheight = 1;
    //set the x and y position
    c.gridx = 1;
    c.gridy = 0;
    //set the weight properties
    c.weightx = 0.2;
    c.weighty = 0.2;

    getContentPane().add(jpInfo, c);

    //set the number of columns/rows to use
    c.gridwidth = 1;
    c.gridheight = 1;
    //set the x and y position
    c.gridx = 1;
    c.gridy = 1;
    //set the weight properties
    c.weightx = 0.2;
    c.weighty = 0.2;

    getContentPane().add(jpChat, c);
    jpChat.setLayout(new GridLayout());
    jpChat.add(client.gui.getContentPane(), c);

    setVisible(true);

在讨论的同时,我还可以问另一个问题.我想为整个GUI和聊天面板设置最小大小限制.首先,我希望它不允许用户将其调整为小于某个x和某个y的大小.对于后者,我希望它保持我声明的纵横比,然后在达到两个右面板的最小值后,仅调整左面板的大小.也就是说,它将调整每个面板的大小,直到达到某个大小,然后继续调整整个GUI的大小,但只是调整左侧面板的大小,使右侧面板保持最小尺寸.

谢谢你帮我这真的让我很烦,我花了几个小时尝试这样做,弄乱了GridBagLayoutConstraints却一无所获.这是我所能做到的最好的.

解决方法:

我尝试解决您的问题,但有一个简单的原因,可能会引起您的担忧.在ChatPanel中使用的JTextField初始化时,必须为其指定列,这可能会导致这种效果.自从我编写上述代码的方式以来,这件事在任何意义上都没有给我带来任何奇怪的行为.尽管关于第二个问题,@ kleopatra提供了一些见解.这是我使用的代码:

import java.awt.*;
import javax.swing.*;

public class ClientGrid
{
    private void displayGUI()
    {
        JFrame frame = new JFrame("Client Grid");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

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

        JPanel bluePanel = new JPanel();
        bluePanel.setOpaque(true);
        bluePanel.setBackground(Color.BLUE);

        JPanel greenPanel = new JPanel();
        greenPanel.setOpaque(true);
        greenPanel.setBackground(Color.GREEN);

        JPanel redPanel = new JPanel();
        redPanel.setOpaque(true);
        redPanel.setBackground(Color.RED);

        JPanel chatPanel = new JPanel();
        JTextField chatField = new JTextField();
        chatPanel.setLayout(new BorderLayout(5, 5));
        chatPanel.add(chatField, BorderLayout.PAGE_START);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.FIRST_LINE_START;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 0.7;
        gbc.weighty = 0.3;

        contentPane.add(bluePanel, gbc);

        gbc.gridx = 1;
        gbc.weightx = 0.3;

        contentPane.add(greenPanel, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.weightx = 0.7;
        gbc.weighty = 0.7;

        contentPane.add(redPanel, gbc);

        gbc.gridx = 1;
        gbc.weightx = 0.3;

        contentPane.add(chatPanel, gbc);

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

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new ClientGrid().displayGUI();
            }
        });
    }
}

标签:gridbaglayout,swing,jpanel,java,user-interface
来源: https://codeday.me/bug/20191127/2075030.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有