这是我拥有的一些使用 GridBagConstraints 的 Java 代码:
public AuctionClient() {
JFrame guiFrame = new JFrame();
JPanel guiPanel = new JPanel(new GridBagLayout());
JLabel userNameLabel = new JLabel("UserName:");
JTextField userNameTextField = new JTextField(30);
JButton loginButton = new JButton("Login");
JButton registerButton = new JButton("Register");
JLabel passwordLabel = new JLabel("Password:");
JTextField passwordTextField = new JPasswordField(30);
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("Auction Client");
guiFrame.setSize(500, 250);
guiFrame.setLocationRelativeTo(null);
GridBagConstraints labelGBC = new GridBagConstraints();
labelGBC.insets = new Insets(3, 3, 3, 3);
GridBagConstraints fieldGBC = new GridBagConstraints();
fieldGBC.insets = new Insets(3, 3, 3, 3);
fieldGBC.gridwidth = GridBagConstraints.REMAINDER;
guiPanel.add(userNameLabel, labelGBC);
guiPanel.add(userNameTextField, fieldGBC);
guiPanel.add(passwordLabel, labelGBC);
guiPanel.add(passwordTextField, fieldGBC);
GridBagConstraints loginButtonGBC = new GridBagConstraints();
loginButtonGBC.insets = new Insets(3, 3, 3, 3);
GridBagConstraints registerButtonGBC = new GridBagConstraints();
registerButtonGBC.insets = new Insets(3, 3, 3, 3);
registerButtonGBC.gridwidth = GridBagConstraints.REMAINDER;
guiPanel.add(loginButton, loginButtonGBC);
guiPanel.add(registerButton, registerButtonGBC);
guiFrame.add(guiPanel, BorderLayout.NORTH);
guiFrame.setVisible(true);
}
我在网上查看了一些关于 GridBagConstraints 如何在面板上放置控件的解释。我无法确切理解它是如何工作的,所以我在这个论坛上问了一个问题。
下面是上面代码运行时的截图:
能否请我帮忙将“登录”和“注册”按钮并排放置在面板中间。
编辑
这是我当前的工作代码:
public AuctionClientLogon() {
JFrame guiFrame = new JFrame();
JPanel guiFieldsPanel = new JPanel(new GridBagLayout());
JPanel guiButtonsPanel = new JPanel(new GridBagLayout());
JLabel userNameLabel = new JLabel("UserName:");
JTextField userNameTextField = new JTextField(30);
JButton loginButton = new JButton("Login");
JButton registerButton = new JButton("Register");
JLabel passwordLabel = new JLabel("Password:");
JTextField passwordTextField = new JPasswordField(30);
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("Auction Client");
guiFrame.setSize(500, 250);
guiFrame.setLocationRelativeTo(null);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.CENTER;
guiFieldsPanel.add(userNameLabel, gbc);
gbc.gridx++;
guiFieldsPanel.add(userNameTextField, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
guiFieldsPanel.add(passwordLabel, gbc);
gbc.gridx++;
guiFieldsPanel.add(passwordTextField, gbc);
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridx = 0;
gbc.gridy = 1;
guiButtonsPanel.add(loginButton, gbc);
gbc.gridx++;
guiButtonsPanel.add(registerButton, gbc);
guiFrame.add(guiFieldsPanel, BorderLayout.NORTH);
guiFrame.add(guiButtonsPanel, BorderLayout.CENTER);
guiFrame.setVisible(true);
}
这是一张图片:
http://canning.co.nz/Java/Positioning_Image2.png
是否可以将 Labels 和 TextFields 以及按钮放在 Center 中,但不能彼此重叠?如果可能的话,我希望一切都在中心,但按钮比标签和文本字段低一点。这可能吗?