gridBagLayout vs absoluteLayout

Chr*_*ris 5 java user-interface swing gridbaglayout

我正在尝试为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);
Run Code Online (Sandbox Code Playgroud)

fan*_*e89 1

我推荐Netbeans中内置的布局定制器。从中学习很容易。希望能帮助到你。

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

这是如何执行此操作的屏幕截图: http: //goo.gl/TBPY9i