【问题标题】:Incorrect GridBagLayout behaviour不正确的 GridBagLayout 行为
【发布时间】:2011-07-04 11:52:29
【问题描述】:

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

我还注意到 JFrame 有一个特定的高度阈值,当 JFrame 的高度低于此阈值时,所有 GridBagLayouts 的行为都正确(例如,所有面板的正确宽度和高度)。当高度高于此阈值时,两个右侧面板都会获得额外的宽度,而当降低 JFrame 的高度时,rightTopPanel 会比 rightBottomPanel 更快地失去其高度。

阈值似乎是 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

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

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

【问题讨论】:

  • 你是如何创建你的GridBagConstraints的?
  • 我在问题中说过。简单地说,我创建它的实例,并设置这些属性。
  • sscce 可以补充您的图片。
  • @trashgod:图片不言自明。但是,这取决于您是否提供帮助,或者...
  • 显然您在打开图片时遇到了一些问题。正如我已经说过的,它们是不言自明的。

标签: java swing jscrollpane layout-manager gridbaglayout


【解决方案1】:

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

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

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

阅读 How to Use GridBagLayout 上的 Swing 教程部分了解更多信息。

如果您仍有问题,请发布您的 SSCCE 来证明问题。

【讨论】:

  • 所以,当涉及到这种情况时,本质上 GridBagLayout 是无用的。与此同时,我不妨开始开发一些功能性的东西......
【解决方案2】:

尝试为 GridBagConstraints 指定 ipadx 和 ipady 值。

【讨论】:

  • 这只是填充。一点用都没有。
【解决方案3】:

我创建了这个程序 -

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() 方法中添加了什么。看来问题出在哪里了。

【讨论】:

  • 然后我们使用 2 个不同版本的 Java - 一个 GridBagLayout 可以工作,一个不工作。 pastebin.com/RudBxiLh
【解决方案4】:

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

【讨论】:

  • 你实际上是在说实现一个 LayoutManager。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多