这是我使用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以及按钮放置在中心,但不能彼此重叠?我希望中心中的所有内容都可以,但是按钮要比Labels和TextFields稍微低一点。这可能吗?
最佳答案
首先查看表单要求。您有两个部分。字段和按钮。这些部分中的每一个都有(略微)不同的布局要求。
首先要分离布局以最好地满足这些要求。
创建一个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);
}
});
}
}