Java gridBagLayout vs absoluteLayout
我正在尝试用JAVA为银行应用程序制作GUI。如果我在eclipse中使用WindowBuilder,那么为框架使用绝对布局很容易创建任何内容,但问题是当我调整框架大小时。这就是为什么我选择使用gridBagLayout,在这里我可以使用weightx/y来简化我的工作。我有点忘了如何正确使用这个布局,所以我第一次尝试在gridBagLayout主面板中添加一个JPanel时就被卡住了 这就是我想要实现的(绝对布局): 这就是我(在gridBagLayout中制作的): 如果有人能告诉我,当我第一次添加flowLayout面板时,我需要更改/添加什么,我将不胜感激。基本上是第一个细胞的空白-我想摆脱它,并控制它 下面是一些代码:Java gridBagLayout vs absoluteLayout,java,swing,user-interface,gridbaglayout,Java,Swing,User Interface,Gridbaglayout,我正在尝试用JAVA为银行应用程序制作GUI。如果我在eclipse中使用WindowBuilder,那么为框架使用绝对布局很容易创建任何内容,但问题是当我调整框架大小时。这就是为什么我选择使用gridBagLayout,在这里我可以使用weightx/y来简化我的工作。我有点忘了如何正确使用这个布局,所以我第一次尝试在gridBagLayout主面板中添加一个JPanel时就被卡住了 这就是我想要实现的(绝对布局): 这就是我(在gridBagLayout中制作的): 如果有人能告诉我,当
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);
我建议使用Netbeans中的内置布局定制器。 从中学习很容易。希望能有帮助 对于您的问题类型,请尝试调整要拉伸的组件的重量
下面是一个如何操作的屏幕截图:我建议使用可视化表单设计器,它比手动操作简单、快速得多。eclipse,jformdesigner,netbeans,有很多选择。我解决了我的问题。我已经注释了/*gridBagLayout.columnWidths=newint[]{0,0};gridBagLayout.rowHeights=newint[]{0,0};gridBagLayout.columnWeights=新的double[]{1.0,double.MIN_值};gridBagLayout.rowWeights=new double[]{1.0,double.MIN_VALUE};*/我正在eclipse中使用WindowBuilder插件。实际上,这段代码有50%是生成的。也许这就是为什么我不明白为什么我有那个空白。@Chris:如果您在eclipse中使用window builder,您可以制作一个简单的函数,将组件添加到指定的容器中,将
GridBagLayout
作为其Layout管理器
,使代码更具可读性,您可以看看如何使用miglayout。