【问题标题】:gridBagLayout vs absoluteLayoutgridBagLayout 与 absoluteLayout
【发布时间】:2013-08-01 00:47:48
【问题描述】:

我正在尝试为 JAVA 中的银行应用程序制作 GUI。如果我在 Eclipse 中使用 WindowBuilder,那么为我的框架使用绝对​​布局很容易创建任何东西,但它的问题是当我调整框架大小时。所以这就是我选择使用 gridBagLayout 的原因,我可以使用 weightx/y 来简化我的工作。我有点忘了如何正确使用这个布局,所以我第一次尝试在我的 gridBagLayout 主面板中添加一个 JPanel 时遇到了困难。

这就是我想要实现的(绝对布局):

这就是我所拥有的(在 gridBagLayout 中制作):

如果有人能指出我在第一次添加 flowLayout 面板时必须更改/添加的内容,我将不胜感激。基本上是第一个单元格的空白 - 我想摆脱它并控制它!

这里有一些代码:

    setBackground(new Color(255, 255, 255));
    GridBagLayout gridBagLayout = new GridBagLayout();
    gridBagLayout.columnWidths = new int[]{0, 0};
    gridBagLayout.rowHeights = new int[]{0, 0};
    gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
    gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
    setLayout(gridBagLayout);

    JPanel panel = new JPanel();
    FlowLayout flowLayout = (FlowLayout) panel.getLayout();
    flowLayout.setVgap(15);
    flowLayout.setHgap(15);
    flowLayout.setAlignment(FlowLayout.LEFT);
    panel.setBackground(new Color(51, 102, 204));
    GridBagConstraints gbc_panel = new GridBagConstraints();
    gbc_panel.anchor = GridBagConstraints.NORTH;
    gbc_panel.fill = GridBagConstraints.HORIZONTAL;
    gbc_panel.gridx = 0;
    gbc_panel.gridy = 0;
    add(panel, gbc_panel);

    JLabel label = new JLabel("European Bank");
    label.setForeground(Color.WHITE);
    label.setFont(new Font("Tahoma", Font.PLAIN, 25));
    panel.add(label);

    JLabel lblYourBankAccounts = new JLabel("Your Bank Accounts");
    lblYourBankAccounts.setForeground(new Color(153, 153, 153));
    lblYourBankAccounts.setFont(new Font("Tahoma", Font.PLAIN, 19));
    GridBagConstraints gbc_label = new GridBagConstraints();
    gbc_label.insets = new Insets(0, 60, 0, 0);
    gbc_label.anchor = GridBagConstraints.LINE_START;
    gbc_label.gridx = 0;
    gbc_label.gridy = 1;
    add(lblYourBankAccounts, gbc_label);

    JScrollPane scrollPane = new JScrollPane();
    GridBagConstraints gbc_scrollPane = new GridBagConstraints();
    gbc_scrollPane.insets = new Insets(10, 60, 10, 10);
    gbc_scrollPane.weightx = 1.0;
    gbc_scrollPane.weighty = 1.0;
    gbc_scrollPane.gridx = 0;
    gbc_scrollPane.gridy = 2;
    gbc_scrollPane.fill = GridBagConstraints.BOTH;
    add(scrollPane, gbc_scrollPane);

【问题讨论】:

  • 我建议使用可视化表单设计器,它比手动操作更容易、更快捷。 eclipse、jformdesigner、netbeans,有很多选择。
  • 我解决了我的问题。我评论了 /*gridBagLayout.columnWidths = new int[]{0, 0}; gridBagLayout.rowHeights = new int[]{0, 0}; gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE}; gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};*/ 我在 Eclipse 中使用 WindowBuilder 插件。实际上,生成了 50% 的代码。也许这就是为什么我不明白为什么我有那个空白。
  • @Chris :您可以通过创建一个简单的函数,将组件添加到指定的容器中,使您的代码更具可读性,将GridBagLayout 作为它的Layout Manager,如下所示@ 987654321@
  • 如果你在eclipse中使用window builder,你可以看看使用miglayout。

标签: java swing user-interface gridbaglayout


【解决方案1】:

我会推荐 Netbeans 中的内置布局定制器。 很容易从中学习。希望对您有所帮助。

针对您的问题类型,尝试调整您想要拉伸的组件的权重。

下面是截图:http://goo.gl/TBPY9i

【讨论】:

    猜你喜欢
    • 2012-06-24
    • 1970-01-01
    • 1970-01-01
    • 2013-07-12
    • 2011-03-08
    • 2021-11-20
    • 2017-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多