简体   繁体   English

为什么在添加JLabel时GridBagLayout中的面板之一会扩展?

[英]Why is one of the panel in GridBagLayout expanding when i add a JLabel?

I am trying to create a GUI with a gridbaglayout. 我正在尝试使用gridbaglayout创建一个GUI。

Below is the GUI i created with a gridbaglayout which is perfectly sized. 下面是我使用大小合适的gridbaglayout创建的GUI。

http://postimg.org/image/azjsw5xvd/ http://postimg.org/image/azjsw5xvd/

However, when i try to add a JLabel in the "Functions" panel, my "Functions" panel automatically expands and changes my GUI. 但是,当我尝试在“功能”面板中添加JLabel时,我的“功能”面板会自动展开并更改我的GUI。

See below with automatically expanded Panel. 自动扩展面板请参见下文。

http://postimg.org/image/xu1h21lk1/ http://postimg.org/image/xu1h21lk1/

How do i lock all panels from changing their size and keeping it in this panel ratios? 如何锁定所有面板,以免改变其尺寸并将其保持在此面板比例之内?

Below is the source code i used to to align my GUI using gridbaglayout. 下面是我用来使用gridbaglayout对齐GUI的源代码。 I wish to make sure all panels maintain the same panel ratios when i resize my frame or add components into my sub-panels. 我希望在调整框架大小或将组件添加到子面板时,确保所有面板保持相同的面板比率。

    frame = new JFrame("Cocoon");
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

    background = new JPanel();
    background.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    frame.getContentPane().add(background, BorderLayout.CENTER);

    filePanel = new FilesPanel();
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.gridwidth = 2;
    gbc.gridheight = 10;
    gbc.weightx = 0.2;
    gbc.weighty = 1.0;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.insets = new Insets(10, 10, 10, 10);
    background.add(filePanel, gbc);

    beforePanel = new BeforePanel();
    afterPanel = new AfterPanel();

    gbc.gridx = 2;
    gbc.gridy = 0;
    gbc.gridwidth = 7;
    gbc.gridheight = 8;
    gbc.weightx = 0.5;
    gbc.weighty = 0.8;
    codes = new JSplitPane(JSplitPane.VERTICAL_SPLIT, beforePanel, afterPanel);
    codes.setOneTouchExpandable(true);
    codes.setResizeWeight(0.5);
    background.add(codes, gbc);

    feedBackPanel = new FeedbackPanel();
    gbc.gridx = 2;
    gbc.gridy = 8;
    gbc.gridwidth = 5;
    gbc.gridheight = 2;
    gbc.weightx = 0.5;
    gbc.weighty = 0.2;
    background.add(feedBackPanel, gbc);

    functionsPanel = new FunctionsPanel();
    gbc.gridx = 7;
    gbc.gridy = 0;
    gbc.gridwidth = 3;
    gbc.gridheight = 10;
    gbc.weightx = 0.3;
    gbc.weighty = 1.0;
    background.add(functionsPanel, gbc);

    frame.add(background);
    frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
    frame.validate();
    frame.pack();
    frame.setVisible(true);

Thank you. 谢谢。

Check out this answer . 看看这个答案 Basically there is no easy setting to get GridBagLayout from resizing the layout based on the content. 基本上,没有简单的设置可以使GridBagLayout根据内容调整布局大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM