1

这是我拥有的一些使用 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 中,但不能彼此重叠?如果可能的话,我希望一切都在中心,但按钮比标签和文本字段低一点。这可能吗?

4

2 回答 2

1

使用 FlowLayout 保留这种按钮行。然后您可以轻松地将按钮位置放置在您想要内联的任何位置。看看如何使用流布局

于 2013-05-07T03:57:33.320 回答
1

首先查看您的表单要求。你有两个部分。字段和按钮。这些部分中的每一个都有(略微)不同的布局要求。

首先分离布局以最好地满足这些要求。

创建一个JPanel来保存字段和一个JPanel按钮。如果需要,这些面板可以有不同的布局,但我所包含的示例GridBagLayout适用于每个面板。

然后相应地布局您的组件(在各个面板上)。

然后把所有东西放在一起...

在此处输入图像描述

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class GridBagLayout01 {

    public static void main(String[] args) {
        new GridBagLayout01();
    }

    public GridBagLayout01() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                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);

                JPanel fields = new JPanel(new GridBagLayout());

                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;
                fields.add(userNameLabel, labelGBC);
                fields.add(userNameTextField, fieldGBC);

                fields.add(passwordLabel, labelGBC);
                fields.add(passwordTextField, fieldGBC);

                JPanel buttons = new JPanel(new GridBagLayout());

                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;
                buttons.add(loginButton, loginButtonGBC);
                buttons.add(registerButton, registerButtonGBC);

                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridwidth = GridBagConstraints.REMAINDER;
                guiPanel.add(fields, gbc);
                guiPanel.add(buttons, gbc);

                guiFrame.add(guiPanel, BorderLayout.NORTH);
                guiFrame.setVisible(true);
            }
        });
    }
}
于 2013-05-07T03:34:52.133 回答