我正试图用JAVA为一个银行应用程序创建一个GUI。如果我在eclipse中使用WindowBuilder,那么为我的框架使用绝对布局是非常容易的,但是问题是当我调整帧的大小时。这就是为什么我选择使用gridBagLayout,在这里我可以使用加权/y来简化我的工作。我有点忘记了如何正确地使用这个布局,所以我第一次尝试在我的JPanel主面板中添加一个gridBagLayout就被困住了。
这就是我想要达到的目标(绝对布局):

这就是我拥有的(用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);发布于 2013-11-30 16:54:17
我推荐Netbeans中内置的布局定制器。从中很容易学到。希望能帮上忙。
对于您的问题类型,尝试调整您想要分散的组件的权重。
下面是一个截图,如何做到这一点:http://goo.gl/TBPY9i
https://stackoverflow.com/questions/17971306
复制相似问题