Java GridBagLayout,当我';我在放置我的组件?
我使用GridBagLayout,我非常喜欢它,但是我在放置和移动组件时遇到了屏幕拉伸问题。我想这和我的插图有关。我用插图来移动和调整我的组件,我想这在编码界是一个很大的禁忌,但我真的不知道如何用另一种方式来做。我正在尝试做一个表格,有人可以填写,我希望它看起来整洁 以下是我的节目图片: 正如您所看到的,name文本字段比其他所有字段都扩展得更远,而且随着我使用insets修改jcombobox,这种扩展变得越来越糟糕 这是我的密码:Java GridBagLayout,当我';我在放置我的组件?,java,swing,gridbaglayout,Java,Swing,Gridbaglayout,我使用GridBagLayout,我非常喜欢它,但是我在放置和移动组件时遇到了屏幕拉伸问题。我想这和我的插图有关。我用插图来移动和调整我的组件,我想这在编码界是一个很大的禁忌,但我真的不知道如何用另一种方式来做。我正在尝试做一个表格,有人可以填写,我希望它看起来整洁 以下是我的节目图片: 正如您所看到的,name文本字段比其他所有字段都扩展得更远,而且随着我使用insets修改jcombobox,这种扩展变得越来越糟糕 这是我的密码: vframe = new JFrame("MES Ban
vframe = new JFrame("MES Banking App");
vpanel = new JPanel();
titlelabel = new JLabel("Verification Page");
namelabel = new JLabel("Name: ");
namefield = new JTextField(20);
birthdaylabel = new JLabel("Birth date: ");
dayDD = new JLabel("Day:");
daysArray = new String[] {"1","2","3","4","5","6","7","8","9", "10", "11", "12", "13", "14","15","16","17","18"};//31
BDday = new JComboBox(daysArray);
monthDD = new JLabel("Month:");
monthsArray = new String[] {"1","2","3","4","5","6","7","8","9","10","11","12"}; //12 months
BDmonth = new JComboBox(monthsArray);
//title section
vpanel.setLayout(new GridBagLayout());
grid = new GridBagConstraints();
grid.fill = GridBagConstraints.PAGE_START;
grid.weightx = 0;
grid.gridx = 0;
grid.gridy = 0;
vpanel.add(titlelabel,grid);
//name section
grid.fill = GridBagConstraints.HORIZONTAL;
grid.gridx = 0;
grid.gridy = 1;
grid.insets = new Insets(10,0,0,0);
vpanel.add(namelabel,grid);//adds name label
grid.gridx = 1;
grid.gridy = 1;
grid.insets = new Insets(0,0,0,0);
vpanel.add(namefield,grid); //adds name text field
//birthday section
grid.fill = GridBagConstraints.HORIZONTAL;
grid.gridx = 0;
grid.gridy = 2;
grid.insets = new Insets(0,0,0,0);
vpanel.add(birthdaylabel,grid); //adds bday label
grid.gridx = 1;
grid.gridy = 2;
vpanel.add(dayDD,grid); //add day label
grid.fill = GridBagConstraints.HORIZONTAL;
grid.gridx = 1;
grid.gridy = 2;
grid.insets = new Insets(0,30,0,350);//sets day drop down
vpanel.add(BDday,grid);
grid.fill = GridBagConstraints.HORIZONTAL;
grid.gridx = 1;
grid.gridy = 2;
grid.insets = new Insets(0,100,0,250);//month label
vpanel.add(monthDD,grid);
grid.fill = GridBagConstraints.HORIZONTAL;
grid.gridx = 1;
grid.gridy = 2;
grid.insets = new Insets(0,150,0,250);
vpanel.add(BDmonth,grid);
vframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
vframe.getContentPane().add(vpanel);
vframe.pack();
vframe.setVisible(true);
有人可以演示如何以不拉伸屏幕的方式操纵组件吗?编辑我的代码就像用文字解释一样有效,但我非常直观,代码片段对我来说最有效
感谢您使用了GridBagLayout,尤其是GridBagConstraints,因为您将组件放在了彼此的正上方。您需要确保没有为同一组件提供相同的gridx和gridy位置。使用GridWidth拉伸组件,但为下一个组件重新设置。不要为此目的使用插图。注意,对每个组件使用新的GridBagConstraints通常更好。我经常为此创建一个GridBagConstraints创建方法 e、 g
因此,使用两种方法来简化事情:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.*;
@SuppressWarnings("serial")
public class LayoutFoo2 extends JPanel {
private static final Insets DEFAULT_INSETS = new Insets(5, 5, 5, 5);
private static final double DEFAULT_WEIGHTX = 1.0;
private static final double DEFAULT_WEIGHTY = 1.0;
private static JFrame vframe;
private static JPanel vpanel;
private static JLabel titlelabel;
private static JLabel namelabel;
private static JTextField namefield;
private static JLabel birthdaylabel;
private static JLabel dayDD;
private static String[] daysArray;
private static JComboBox BDday;
private static JLabel monthDD;
private static String[] monthsArray;
private static JComboBox BDmonth;
private static GridBagConstraints grid;
public static void main(String[] args) {
vframe = new JFrame("MES Banking App");
vpanel = new JPanel();
titlelabel = new JLabel("Verification Page");
namelabel = new JLabel("Name: ");
namefield = new JTextField(20);
birthdaylabel = new JLabel("Birth date: ");
dayDD = new JLabel("Day:");
daysArray = new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "11", "12", "13", "14", "15", "16", "17", "18" };// 31
BDday = new JComboBox(daysArray);
monthDD = new JLabel("Month:");
monthsArray = new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "11", "12" }; // 12 months
BDmonth = new JComboBox(monthsArray);
// title section
vpanel.setLayout(new GridBagLayout());
grid = createGbc(0, 0, 5, 1);
grid.fill = GridBagConstraints.CENTER;
titlelabel.setHorizontalTextPosition(JLabel.CENTER);
vpanel.add(titlelabel, grid);
// name section
grid = createGbc(0, 1);
vpanel.add(namelabel, grid);// adds name label
grid = createGbc(1, 1, 4, 1);
vpanel.add(namefield, grid); // adds name text field
// birthday section
grid = createGbc(0, 2);
vpanel.add(birthdaylabel, grid); // adds bday label
grid = createGbc(1, 2);
vpanel.add(dayDD, grid); // add day label
grid = createGbc(2, 2);
vpanel.add(BDday, grid);
grid = createGbc(3, 2);
vpanel.add(monthDD, grid);
grid = createGbc(4, 2);
vpanel.add(BDmonth, grid);
vframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
vframe.getContentPane().add(vpanel);
vframe.pack();
vframe.setVisible(true);
}
public static GridBagConstraints createGbc(int x, int y, int width, int height) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = width;
gbc.gridheight = height;
// default set ups
gbc.insets = DEFAULT_INSETS;
gbc.weightx = DEFAULT_WEIGHTX;
gbc.weighty = DEFAULT_WEIGHTY;
gbc.fill = GridBagConstraints.HORIZONTAL;
return gbc;
}
public static GridBagConstraints createGbc(int x, int y) {
return createGbc(x, y, 1, 1);
}
}
您使用的GridBagLayout,尤其是GridBagConstraints是错误的,因为您将组件放在彼此的顶部。您需要确保没有为同一组件提供相同的gridx和gridy位置。使用GridWidth拉伸组件,但为下一个组件重新设置。不要为此目的使用插图。注意,对每个组件使用新的GridBagConstraints通常更好。我经常为此创建一个GridBagConstraints创建方法 e、 g
因此,使用两种方法来简化事情:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.*;
@SuppressWarnings("serial")
public class LayoutFoo2 extends JPanel {
private static final Insets DEFAULT_INSETS = new Insets(5, 5, 5, 5);
private static final double DEFAULT_WEIGHTX = 1.0;
private static final double DEFAULT_WEIGHTY = 1.0;
private static JFrame vframe;
private static JPanel vpanel;
private static JLabel titlelabel;
private static JLabel namelabel;
private static JTextField namefield;
private static JLabel birthdaylabel;
private static JLabel dayDD;
private static String[] daysArray;
private static JComboBox BDday;
private static JLabel monthDD;
private static String[] monthsArray;
private static JComboBox BDmonth;
private static GridBagConstraints grid;
public static void main(String[] args) {
vframe = new JFrame("MES Banking App");
vpanel = new JPanel();
titlelabel = new JLabel("Verification Page");
namelabel = new JLabel("Name: ");
namefield = new JTextField(20);
birthdaylabel = new JLabel("Birth date: ");
dayDD = new JLabel("Day:");
daysArray = new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "11", "12", "13", "14", "15", "16", "17", "18" };// 31
BDday = new JComboBox(daysArray);
monthDD = new JLabel("Month:");
monthsArray = new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "11", "12" }; // 12 months
BDmonth = new JComboBox(monthsArray);
// title section
vpanel.setLayout(new GridBagLayout());
grid = createGbc(0, 0, 5, 1);
grid.fill = GridBagConstraints.CENTER;
titlelabel.setHorizontalTextPosition(JLabel.CENTER);
vpanel.add(titlelabel, grid);
// name section
grid = createGbc(0, 1);
vpanel.add(namelabel, grid);// adds name label
grid = createGbc(1, 1, 4, 1);
vpanel.add(namefield, grid); // adds name text field
// birthday section
grid = createGbc(0, 2);
vpanel.add(birthdaylabel, grid); // adds bday label
grid = createGbc(1, 2);
vpanel.add(dayDD, grid); // add day label
grid = createGbc(2, 2);
vpanel.add(BDday, grid);
grid = createGbc(3, 2);
vpanel.add(monthDD, grid);
grid = createGbc(4, 2);
vpanel.add(BDmonth, grid);
vframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
vframe.getContentPane().add(vpanel);
vframe.pack();
vframe.setVisible(true);
}
public static GridBagConstraints createGbc(int x, int y, int width, int height) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = width;
gbc.gridheight = height;
// default set ups
gbc.insets = DEFAULT_INSETS;
gbc.weightx = DEFAULT_WEIGHTX;
gbc.weighty = DEFAULT_WEIGHTY;
gbc.fill = GridBagConstraints.HORIZONTAL;
return gbc;
}
public static GridBagConstraints createGbc(int x, int y) {
return createGbc(x, y, 1, 1);
}
}
气垫船速度更快,我的
GridBagLayout
解决方案仅从多个方面清理示例
口是心非:
package com.zetcode;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class GridBagBankingApp extends JFrame {
public GridBagBankingApp() {
initUI();
setTitle("MES Banking App");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void initUI() {
JLabel titlelabel = new JLabel("Verification Page");
JLabel namelabel = new JLabel("Name: ");
JTextField namefield = new JTextField(10);
JLabel birthdaylabel = new JLabel("Birth date: ");
JLabel dayDD = new JLabel("Day:");
String[] daysArray = new String[]{"1", "2", "3", "4", "5",
"6", "7", "8", "9", "10", "11", "12", "13", "14", "15",
"16", "17", "18"};// 31
JComboBox BDday = new JComboBox(daysArray);
JLabel monthDD = new JLabel("Month:");
String[] monthsArray = new String[]{"1", "2", "3", "4", "5",
"6", "7", "8", "9", "10", "11", "12"}; // 12 months
JComboBox BDmonth = new JComboBox(monthsArray);
//title section
setLayout(new GridBagLayout());
GridBagConstraints grid = new GridBagConstraints();
grid.fill = GridBagConstraints.CENTER;
grid.insets = new Insets(5, 5, 5, 5);
grid.gridwidth = 5;
add(titlelabel, grid);
//name section
grid.fill = GridBagConstraints.HORIZONTAL;
grid.gridwidth = 1;
grid.gridy = 1;
add(namelabel, grid);
grid.gridx = 1;
grid.gridwidth = 4;
add(namefield, grid);
//birthday section
grid.gridx = 0;
grid.gridy = 2;
grid.gridwidth = 1;
add(birthdaylabel, grid);
grid.gridx = 1;
add(dayDD, grid);
grid.gridx = 2;
add(BDday, grid);
grid.gridx = 3;
add(monthDD, grid);
grid.gridx = 4;
add(BDmonth, grid);
pack();
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
GridBagBankingApp ex = new GridBagBankingApp();
ex.setVisible(true);
}
});
}
}
不要使用GridBagLayout
,而是使用MigLayout
管理器。与MigLayout不同,MigLayout创建
在GridBagLayout
中,我们必须定义每个单元格,一次完成整个网格
个别地。这很烦人,而且容易出错
MigLayout
更便于携带。在GridBagLayout的解决方案中,我们有
定义的插入方式如下所示:
grid.insets = new Insets(5, 5, 5, 5);
这可能是确定的决议。对其他人来说,这是不正确的。(它可以
太小会破坏我们的整个布局。)MigLayout
会在
独立于屏幕分辨率和DPI的组件。因此,,
我们应该避免使用GridBagLayout
,使用MigLayout
或GroupLayout
经理
package com.zetcode;
import java.awt.EventQueue;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;
public class MigLayoutBankingApp extends JFrame {
public MigLayoutBankingApp() {
initUI();
setTitle("MES Banking App");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void initUI() {
JPanel pnl = new JPanel(new MigLayout());
JLabel titleLbl = new JLabel("Verification Page");
JLabel nameLbl = new JLabel("Name: ");
JTextField nameField = new JTextField(10);
JLabel bdLbl = new JLabel("Birth date: ");
JLabel dayDD = new JLabel("Day:");
String[] daysArray = new String[]{"1", "2", "3", "4", "5",
"6", "7", "8", "9", "10", "11", "12", "13", "14", "15",
"16", "17", "18" };
JComboBox BDday = new JComboBox(daysArray);
JLabel monthDD = new JLabel("Month:");
String[] monthsArray = new String[]{"1", "2", "3", "4", "5",
"6", "7", "8", "9", "10", "11", "12"}; // 12 months
JComboBox BDmonth = new JComboBox(monthsArray);
pnl.add(titleLbl, "spanx, center, wrap");
pnl.add(nameLbl);
pnl.add(nameField, "spanx, growx, wrap");
pnl.add(bdLbl);
pnl.add(dayDD);
pnl.add(BDday);
pnl.add(monthDD);
pnl.add(BDmonth);
add(pnl);
pack();
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
MigLayoutBankingApp ex = new MigLayoutBankingApp();
ex.setVisible(true);
}
});
}
}
我们可以清楚地看到,代码要短得多
气垫船速度更快,我的GridBagLayout
解决方案仅从多个方面清理示例
口是心非:
package com.zetcode;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class GridBagBankingApp extends JFrame {
public GridBagBankingApp() {
initUI();
setTitle("MES Banking App");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void initUI() {
JLabel titlelabel = new JLabel("Verification Page");
JLabel namelabel = new JLabel("Name: ");
JTextField namefield = new JTextField(10);
JLabel birthdaylabel = new JLabel("Birth date: ");
JLabel dayDD = new JLabel("Day:");
String[] daysArray = new String[]{"1", "2", "3", "4", "5",
"6", "7", "8", "9", "10", "11", "12", "13", "14", "15",
"16", "17", "18"};// 31
JComboBox BDday = new JComboBox(daysArray);
JLabel monthDD = new JLabel("Month:");
String[] monthsArray = new String[]{"1", "2", "3", "4", "5",
"6", "7", "8", "9", "10", "11", "12"}; // 12 months
JComboBox BDmonth = new JComboBox(monthsArray);
//title section
setLayout(new GridBagLayout());
GridBagConstraints grid = new GridBagConstraints();
grid.fill = GridBagConstraints.CENTER;
grid.insets = new Insets(5, 5, 5, 5);
grid.gridwidth = 5;
add(titlelabel, grid);
//name section
grid.fill = GridBagConstraints.HORIZONTAL;
grid.gridwidth = 1;
grid.gridy = 1;
add(namelabel, grid);
grid.gridx = 1;
grid.gridwidth = 4;
add(namefield, grid);
//birthday section
grid.gridx = 0;
grid.gridy = 2;
grid.gridwidth = 1;
add(birthdaylabel, grid);
grid.gridx = 1;
add(dayDD, grid);
grid.gridx = 2;
add(BDday, grid);
grid.gridx = 3;
add(monthDD, grid);
grid.gridx = 4;
add(BDmonth, grid);
pack();
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
GridBagBankingApp ex = new GridBagBankingApp();
ex.setVisible(true);
}
});
}
}
不要使用GridBagLayout
,而是使用MigLayout
管理器。与MigLayout不同,MigLayout创建
在GridBagLayout
中,我们必须定义每个单元格,一次完成整个网格
个别地。这很烦人,而且容易出错
MigLayout
更便于携带。在GridBagLayout的解决方案中,我们有
定义的插入方式如下所示:
grid.insets = new Insets(5, 5, 5, 5);
这可能是确定的决议。对其他人来说,这是不正确的。(它可以
太小会破坏我们的整个布局。)MigLayout
会在
独立于屏幕分辨率和DPI的组件。因此,,
我们应该避免使用GridBagLayout
,使用MigLayout
或GroupLayout
经理
package com.zetcode;
import java.awt.EventQueue;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;
public class MigLayoutBankingApp extends JFrame {
public MigLayoutBankingApp() {
initUI();
setTitle("MES Banking App");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void initUI() {
JPanel pnl = new JPanel(new MigLayout());
JLabel titleLbl = new JLabel("Verification Page");
JLabel nameLbl = new JLabel("Name: ");
JTextField nameField = new JTextField(10);
JLabel bdLbl = new JLabel("Birth date: ");
JLabel dayDD = new JLabel("Day:");
String[] daysArray = new String[]{"1", "2", "3", "4", "5",
"6", "7", "8", "9", "10", "11", "12", "13", "14", "15",
"16", "17", "18" };
JComboBox BDday = new JComboBox(daysArray);
JLabel monthDD = new JLabel("Month:");
String[] monthsArray = new String[]{"1", "2", "3", "4", "5",
"6", "7", "8", "9", "10", "11", "12"}; // 12 months
JComboBox BDmonth = new JComboBox(monthsArray);
pnl.add(titleLbl, "spanx, center, wrap");
pnl.add(nameLbl);
pnl.add(nameField, "spanx, growx, wrap");
pnl.add(bdLbl);
pnl.add(dayDD);
pnl.add(BDday);
pnl.add(monthDD);
pnl.add(BDmonth);
add(pnl);
pack();
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
MigLayoutBankingApp ex = new MigLayoutBankingApp();
ex.setVisible(true);
}
});
}
}
我们可以清楚地看到,代码要短得多
尝试vframe.setresizeable(false)代码>查看使用miglayet作为创建表单的更好布局。不要使用插入来“移动和调整大小”。如果您需要一个空的组件占位符,请在该网格点中插入一个空的JLabel。@HovercraftFullOfEels这真的有效吗?有什么内置的东西我可以使用吗?@SoloSpirit:是的,它确实有效,是的,你可以使用GridBagLayout,但要正确使用它。在使用它之前,请阅读有关它的教程。请检查编辑以回答以下问题。请尝试vframe.setresizeable(false)代码>查看使用miglayet作为创建表单的更好布局。不要使用插入来“移动和调整大小”。如果您需要一个空的组件占位符,请在该网格点中插入一个空的JLabel。@HovercraftFullOfEels这真的有效吗?有什么内置的东西我可以使用吗?@SoloSpirit:是的,它确实有效,是的,你可以使用GridBagLayout,但要正确使用它。在使用它之前,请阅读有关它的教程。请查看下面要回答的编辑。首先,问题是关于使用GridBagLayout,不要求替换。其次,您没有解决依赖第三方库的成本问题。最后,一个字符串是否比显式GridBagConstraints更不容易出错还有争议。1+向上投票反对向下投票。您的解决方案是一个不错的解决方案,它使用了我认为更强大、更易于使用、更易于增强的布局管理器。老实说,我非常喜欢您的两个答案,谢谢各位。首先,问题是关于使用GridBagLayout,不要求更换。其次,您没有解决依赖第三方库的成本问题。最后,一个字符串是否比显式GridBagConstraints更不容易出错还有争议。1+向上投票反对向下投票。你的是一个不错的解决方案,它使用了我认为更强大、更易于使用、更易于增强的布局管理器