【问题标题】:Why is one of the panel in GridBagLayout expanding when i add a JLabel?为什么当我添加 JLabel 时 GridBagLayout 中的面板之一会扩展?
【发布时间】:2015-06-23 21:46:39
【问题描述】:

我正在尝试使用 gridbaglayout 创建一个 GUI。

下面是我使用尺寸完美的 gridbaglayout 创建的 GUI。

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

但是,当我尝试在“功能”面板中添加 JLabel 时,我的“功能”面板会自动展开并更改我的 GUI。

自动展开面板见下文。

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

我如何锁定所有面板,使其不改变它们的大小并保持它在这个面板比率中?

以下是我用来使用 gridbaglayout 对齐我的 GUI 的源代码。当我调整框架大小或将组件添加到子面板时,我希望确保所有面板保持相同的面板比例。

    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);

谢谢。

【问题讨论】:

    标签: java user-interface jpanel gridbaglayout


    【解决方案1】:

    查看this answer。基本上没有简单的设置来让 GridBagLayout 根据内容调整布局大小。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多