不正确的 GridBagLayout 行为

发布于 2024-11-18 12:17:51 字数 2088 浏览 4 评论 0原文

当我调整 JFrame 大小时,表格(或更具体地说是scrollPane)会压缩成一个小矩形(保持不变,但高度更像是 5-10 像素)。

我还注意到 JFrame 有一个特定的高度阈值,当 JFrame 的高度低于此阈值时,所有 GridBagLayout 都会正常运行(例如,所有面板的正确宽度和高度)。当高度高于此阈值时,两个右侧面板都会获得额外的宽度,并且在降低 JFrame 高度时,右侧顶部面板比右侧底部面板更快地失去其高度。

当 rightTopPanel 获得最小高度(大约 5-10 像素)时,阈值似乎是点。

JPanel panel = new JPanel(new GridBagLayout());

JPanel rightTopPanel = new JPanel(new GridBagLayout());
panel.add(rightTopPanel, Helper.createGridBagConstraints(1, 0, 1, 1, 0.5, 0.5, GridBagConstraints.BOTH));

JPanel rightBottomPanel = new JPanel(new GridLayout(1, 1));

tableModel = new DefaultTableModel(0, 2);
JTable table = new JTable(tableModel);

JScrollPane scrollPane = new JScrollPane(table);
rightBottomPanel.add(scrollPane);

panel.add(rightBottomPanel, Helper.createGridBagConstraints(1, 1, 1, 1, 0.5, 0.5, GridBagConstraints.BOTH));

JPanel leftPanel = new JPanel();
panel.add(leftPanel, Helper.createGridBagConstraints(0, 0, 1, 2, 0.5, 1.0));

add(panel);

Helper.createGridBagConstraints 只是创建 GridBagConstraints 的辅助方法,具有许多“可选”参数。

GridBagConstraints createGridBagConstraints(int gridx, int gridy)
GridBagConstraints createGridBagConstraints(int gridx, int gridy, int gridwidth, int gridheight)
GridBagConstraints createGridBagConstraints(int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty)
GridBagConstraints createGridBagConstraints(int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, int fill)

编辑:这似乎是 JScrollPane 中的问题。如果我不添加它,其余面板会正确调整大小。

编辑2:因为到目前为止没有人明白我的问题是什么,以下是屏幕截图: http://img155.imageshack.us/img155/8847/badgridbaglayout1.png

<一href="http://img17.imageshack.us/img17/8779/badgridbaglayout2.png" rel="nofollow">http://img17.imageshack.us/img17/8779/badgridbaglayout2.png

http://img28.imageshack.us/img28/9336/badgridbaglayout3.png

When I resize JFrame, the table (or more specifically the scrollPane) squishes into a little rectangle (with remains the same, but height is more like 5-10 pixels).

Also I noticed that there's a specific height threshold of JFrame, and when height of JFrame drops below this, all GridBagLayouts behave correctly (e.g. correct width & height of all panels). When the height is higher than this threshold, both right panels gain additional width and the rightTopPanel loses its height much faster than the rightBottomPanel when decreasing height of JFrame.

The threshold seems to be point when rightTopPanel gets minimum height (about 5-10 pixels).

JPanel panel = new JPanel(new GridBagLayout());

JPanel rightTopPanel = new JPanel(new GridBagLayout());
panel.add(rightTopPanel, Helper.createGridBagConstraints(1, 0, 1, 1, 0.5, 0.5, GridBagConstraints.BOTH));

JPanel rightBottomPanel = new JPanel(new GridLayout(1, 1));

tableModel = new DefaultTableModel(0, 2);
JTable table = new JTable(tableModel);

JScrollPane scrollPane = new JScrollPane(table);
rightBottomPanel.add(scrollPane);

panel.add(rightBottomPanel, Helper.createGridBagConstraints(1, 1, 1, 1, 0.5, 0.5, GridBagConstraints.BOTH));

JPanel leftPanel = new JPanel();
panel.add(leftPanel, Helper.createGridBagConstraints(0, 0, 1, 2, 0.5, 1.0));

add(panel);

Helper.createGridBagConstraints is just a helper method to create GridBagConstraints, with numerous "optional" parameters.

GridBagConstraints createGridBagConstraints(int gridx, int gridy)
GridBagConstraints createGridBagConstraints(int gridx, int gridy, int gridwidth, int gridheight)
GridBagConstraints createGridBagConstraints(int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty)
GridBagConstraints createGridBagConstraints(int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, int fill)

Edit: It seems that this is a problem within JScrollPane. If I don't add it, remaining panels resize correctly.

Edit 2: Since, so far no one understands what's my problem, here are screenshots:
http://img155.imageshack.us/img155/8847/badgridbaglayout1.png

http://img17.imageshack.us/img17/8779/badgridbaglayout2.png

http://img28.imageshack.us/img28/9336/badgridbaglayout3.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

人间☆小暴躁 2024-11-25 12:17:51

表格(或更具体地说是scrollPane)被挤压成一个小矩形(保持不变,但高度更像是5-10像素)。

GridBagLayout 将尝试按照组件的首选大小绘制组件。当框架收缩时,组件将以“最小尺寸”绘制。因此有时组件的最小尺寸为 0,它只会显示为一个小矩形。

您可以尝试将滚动窗格的最小大小设置为等于首选大小,或者您可以使用您使用的约束来控制此行为。

阅读 Swing 教程中关于如何使用 GridBagLayout 的部分更多信息。

如果您仍然遇到问题,请发布您的 SSCCE 来说明问题。

the table (or more specifically the scrollPane) squishes into a little rectangle (with remains the same, but height is more like 5-10 pixels).

A GridBagLayout will attempt to paint components at their preferred size. When the frame shrinks, the component will be be painted at is "minimum size". So sometimes is a component has a 0 minimum size it will only display as a small rectangle.

You can try setting the minimum size of the scroll pane equal to the preferred size, or you can use the constraints you use you can control this behaviour.

Read the section from the Swing tutorial on How to Use GridBagLayout for more information.

If you still have problems then post your SSCCE that demonstrates the problem.

落日海湾 2024-11-25 12:17:51

尝试为 GridBagConstraints 指定 ipadx 和 ipady 值。

TRy to specify ipadx and ipady values for the GridBagConstraints.

秋心╮凉 2024-11-25 12:17:51

我创建了这个程序 -

package pkg;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class TestFrame extends JFrame {
    public TestFrame() {
        JPanel panel = new JPanel(new GridBagLayout());

        JPanel rightTopPanel = new JPanel(new GridBagLayout());
        panel.add(rightTopPanel, new GridBagConstraints(1, 0, 1, 1, 0.5, 0.5, GridBagConstraints.NORTH, 
                GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

        JPanel rightBottomPanel = new JPanel(new GridLayout(1, 1));

        DefaultTableModel tableModel = new DefaultTableModel(0, 2);
        JTable table = new JTable(tableModel);

        JScrollPane scrollPane = new JScrollPane(table);
        rightBottomPanel.add(scrollPane);

        panel.add(rightBottomPanel, new GridBagConstraints(1, 1, 1, 1, 0.5, 0.5, GridBagConstraints.NORTH, 
                GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

        JPanel leftPanel = new JPanel();
        panel.add(leftPanel, new GridBagConstraints(0, 0, 1, 2, 0.5, 1.0, GridBagConstraints.NORTH, 
                GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

        add(panel);
        setSize(800, 600);
        setVisible(true);
    }

    public static void main(String[] args) {
        new TestFrame();
    }
}

它看起来运行良好。您在 Help.createGridBagConstraints() 方法中放入了什么。看来问题可能出在哪里。

I created this program -

package pkg;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class TestFrame extends JFrame {
    public TestFrame() {
        JPanel panel = new JPanel(new GridBagLayout());

        JPanel rightTopPanel = new JPanel(new GridBagLayout());
        panel.add(rightTopPanel, new GridBagConstraints(1, 0, 1, 1, 0.5, 0.5, GridBagConstraints.NORTH, 
                GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

        JPanel rightBottomPanel = new JPanel(new GridLayout(1, 1));

        DefaultTableModel tableModel = new DefaultTableModel(0, 2);
        JTable table = new JTable(tableModel);

        JScrollPane scrollPane = new JScrollPane(table);
        rightBottomPanel.add(scrollPane);

        panel.add(rightBottomPanel, new GridBagConstraints(1, 1, 1, 1, 0.5, 0.5, GridBagConstraints.NORTH, 
                GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

        JPanel leftPanel = new JPanel();
        panel.add(leftPanel, new GridBagConstraints(0, 0, 1, 2, 0.5, 1.0, GridBagConstraints.NORTH, 
                GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

        add(panel);
        setSize(800, 600);
        setVisible(true);
    }

    public static void main(String[] args) {
        new TestFrame();
    }
}

and it seems to work well. What are you putting in your Help.createGridBagConstraints() methods. It seems that there may be where the problem lies.

遗失的美好 2024-11-25 12:17:51

唯一的解决方案是根本不使用任何布局并手动定位/调整组件大小,因为 Swing 中的内置布局都没有正确实现。

The only solution is to NOT use any layout at all and position/resize components manually, because none of the builtin layouts in Swing are implemented properly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文