我正在尝试为 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);