GridBagConstraints for Java

时间:2013-05-07 03:26:16

标签: java swing jbutton gridbaglayout

以下是我使用的一些使用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如何在面板上放置控件时的一些解释。我无法完全理解它是如何工作的,所以我在这个论坛上问一个问题。

以下是运行时上述代码的屏幕截图:

enter image description here

我可以帮助您将登录和注册按钮并排放置在面板中间。

修改

这是我目前的工作代码:

    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

是否可以将标签和TextField放在中心和按钮中,但不能放在彼此之上?如果可能的话,我想在中心的所有内容,但按钮要比标签和TextFields低一点。这可能吗?

2 个答案:

答案 0 :(得分:1)

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

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

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

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

然后把它们放在一起......

enter image description here

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

答案 1 :(得分:1)

使用FlowLayout保留这种按钮行。然后,您可以轻松地将按钮位置放置在任何您想要内联的位置。  看看how to use flow layout