Java的网格约束
下面是一些使用GridBagConstraints的Java代码:Java的网格约束,java,swing,jbutton,gridbaglayout,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 JTextFiel
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如何在面板上放置控件的解释。我不能确切地理解它是如何工作的,所以我在这个论坛上问了一个问题
以下是运行时上述代码的屏幕截图:
我可以帮你把登录和登记按钮放在面板中间,并排放置。< /P> 编辑
以下是我当前的工作代码: 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);
}
这是一张图片:
是否可以将标签和文本字段放置在中间以及按钮上,但不能相互重叠?如果可能的话,我希望所有的东西都在中间,但是按钮要比标签和文本字段低一点。这可能吗?从查看表单需求开始。你有两个部分。字段和按钮。每个部分都有(稍微)不同的布局要求 首先分离布局以最好地满足这些要求 创建一个
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);
}
});
}
}
使用FlowLayout保留此类按钮行。然后,您可以轻松地将按钮位置放置在任何需要内联的位置。 看一看首先看一看,我希望这也会引起您的兴趣:-)