java - 如何使用 GridBagLayout 防止组件晃动?

标签 java swing layout layout-manager gridbaglayout

我有一个 GUI,其中有四个组件通过 GridBagLayout 彼此相邻排列。当我向右调整窗口大小时,最左边的组件会晃动,当我向左调整大小时,最右边的组件会晃动。

这是一个例子:

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

public class GBLShakeTest extends JPanel {
    public static void main(String[] args) {
        JFrame frame = new JFrame("GBLShakeTest");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(500, 500));
        frame.add(new GBLShakeTest());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public GBLShakeTest() {
        super(new GridBagLayout());
        add(new JTextArea("component 1"), new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
        add(new JTextArea("component 2"), new GridBagConstraints(1, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
        add(new JTextArea("component 3"), new GridBagConstraints(2, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
        add(new JTextArea("component 4"), new GridBagConstraints(3, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
    }
}

它甚至发生在 GridBagLayout demo from oracle 上它仅使用 JButton。向右调整大小没问题,但是向左放大窗口时,右侧按钮会抖动。 有谁知道如何解决这个问题吗?

最佳答案

这绝对是 GridBagLayout 中的一个错误。我找不到解决方法,至少没有使用 GridBagLayout。

对于某些容器大小,似乎发生的是 GridBagLayout 将第一个组件的 X 坐标从 0 更改为 1。如果只有第一个组件具有非零weightx,则不会出现问题(这让我怀疑这是浮点weightx值不准确的问题,但我还没有尝试深入研究GridBagLayout源来确认这一点) .

一种替代方法是使用 SpringLayout 。 SpringLayout 很难使用,但它可以做 GridBagLayout 可以做的许多事情,只需做一些工作。 (编写过使用 Motif 的 XmForm 小部件的代码的人会认识到对其他组件的附件的使用,尽管 SpringLayout 的工作方式并不完全相同。)

SpringLayout layout = new SpringLayout();
setLayout(layout);

add(new JTextArea("component 1"));
add(new JTextArea("component 2"));
add(new JTextArea("component 3"));
add(new JTextArea("component 4"));

Component previous = null;
for (Component c : getComponents()) {
    // Attach component to top and bottom of container.
    layout.putConstraint(
        SpringLayout.NORTH, c, 0, SpringLayout.NORTH, this);
    layout.putConstraint(
        SpringLayout.SOUTH, c, 0, SpringLayout.SOUTH, this);

    if (previous != null) {
        // Attach component's left (west) edge to previous component's
        // right(east) edge.
        layout.putConstraint(
            SpringLayout.WEST, c, 0, SpringLayout.EAST, previous);
    }

    previous = c;
}

// Bind this container's right (east) edge to the right (east) edge
// of the rightmost child component.
Component lastComponent = getComponent(getComponentCount() - 1);
layout.putConstraint(
    SpringLayout.EAST, this, 0, SpringLayout.EAST, lastComponent);

关于java - 如何使用 GridBagLayout 防止组件晃动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68335814/

相关文章:

java - 如何将字符串数组设置为等于另一个字符串(Java)(NB : I am using I/O)?

android - 在不同密度下使用 dp 和 sp 时遇到问题

internet-explorer - IE PrintTemplate 混合上下文警告

java - 我可以以某种方式在静态上下文中使用非静态变量吗?

java - Spring Boot 中 @oneToMany 双向映射的问题

Java 的准备语句与 Oracle Merge

java - 除了金属之外,jbutton 工具提示中的快捷方式不显示在其他主题中

Java Spring Boot - 如何像在 PHP Laravel 中一样为数据库播种

java - JDialog 关闭后滞后

android - 如何制作 ImageView "match_parent"?