I've been self teaching myself swing for a few days for a project and right now I'm trying to figure out how to position components with a grid bag layout. I got most of it except a few small issues. If anyone could help, it would be very appreciated. I've tried this so many different ways D:
我一直在自学为自己的项目摆动几天,现在我正在试图弄清楚如何用网格袋布局来定位组件。除了一些小问题,我得到了大部分内容。如果有人可以提供帮助,我们将非常感激。我尝试过这么多不同的方式D:
...
titlePanel.setLayout(new GridBagLayout());
titlePanel.setBackground(BLUE);
header = new JLabel ("Gradebook");
header.setLocation(200,400);
header.setFont(new Font("Serif", Font.BOLD, 50));
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.CENTER;
titlePanel.add(header,gbc);
date = new Date();
currentDate = new JLabel (fmt.format(date));
currentDate.setFont(new Font("Serif", Font.PLAIN, 14));
ActionListener updateTime = new ActionListener() {
public void actionPerformed (ActionEvent e) {
date = new Date();
currentDate.setText(fmt.format(date));
}
};
Timer timer = new Timer (1000, updateTime);
timer.start();
gbc.gridx = 0;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.CENTER;
titlePanel.add(currentDate, gbc);
JLabel userName = new JLabel ("Username: ");
gbc.gridx = 0;
gbc.gridy = 2;
gbc.anchor = GridBagConstraints.EAST;
titlePanel.add(userName, gbc);
JTextField username = new JTextField (10);
gbc.gridx = 1;
gbc.gridy = 2;
gbc.anchor = GridBagConstraints.WEST;
titlePanel.add(username, gbc);
JLabel password = new JLabel ("Password: ");
gbc.gridx = 0;
gbc.gridy = 3;
gbc.anchor = GridBagConstraints.EAST;
titlePanel.add(password, gbc);
JPasswordField Password = new JPasswordField (10);
gbc.gridx = 1;
gbc.gridy = 3;
gbc.anchor = GridBagConstraints.WEST;
titlePanel.add(Password, gbc);
JButton login = new JButton ("Login");
gbc.gridx = 0;
gbc.gridy = 4;
gbc.anchor = GridBagConstraints.CENTER;
titlePanel.add(login, gbc);
JButton newAccount = new JButton ("Create New Account");
gbc.gridx = 0;
gbc.gridy = 5;
gbc.anchor = GridBagConstraints.CENTER;
titlePanel.add(newAccount, gbc);
mainFrame.add(titlePanel);
So when I run the code for the login screen, it comes up with this
因此,当我运行登录屏幕的代码时,它就会出现这个问题
I need a way to center the username and password so they match up with everything else and also add some blank vertical space between the 2 buttons at the bottom. Sorry if this is a dumb question :|
我需要一种方法来使用户名和密码居中,以便它们与其他所有内容匹配,并在底部的2个按钮之间添加一些空白垂直空间。对不起,如果这是一个愚蠢的问题:|
1 个解决方案
#1
2
Your username/password contains two components in two different columns. So if you want all the components centered you have two options:
您的用户名/密码包含两个不同列中的两个组件。因此,如果您希望所有组件都居中,那么您有两个选择:
-
Create a separate panel for each of the label/text field components. Then you can add the panel as a single component which means it will be placed in the first column with all the other components.
为每个标签/文本字段组件创建单独的面板。然后,您可以将面板添加为单个组件,这意味着它将与所有其他组件一起放置在第一列中。
-
Have all the other component "span" two columns. So now they will take up the same width as the label/text field components. In this case you will need to specify the
gridWidth
constraint.让所有其他组件“跨越”两列。所以现在它们将占用与标签/文本字段组件相同的宽度。在这种情况下,您需要指定gridWidth约束。
Read the section from the Swing tutorial on How to Use GridBagLayout for more information on the various constraints used by GridBagLayout.
阅读有关如何使用GridBagLayout的Swing教程中的部分,以获取有关GridBagLayout使用的各种约束的更多信息。
also add some blank vertical space between the 2 buttons at the bottom
还可以在底部的2个按钮之间添加一些空白垂直空间
Again, look at the constraints. You could use the insets
constraint.
再看一下约束。您可以使用insets约束。
#1
2
Your username/password contains two components in two different columns. So if you want all the components centered you have two options:
您的用户名/密码包含两个不同列中的两个组件。因此,如果您希望所有组件都居中,那么您有两个选择:
-
Create a separate panel for each of the label/text field components. Then you can add the panel as a single component which means it will be placed in the first column with all the other components.
为每个标签/文本字段组件创建单独的面板。然后,您可以将面板添加为单个组件,这意味着它将与所有其他组件一起放置在第一列中。
-
Have all the other component "span" two columns. So now they will take up the same width as the label/text field components. In this case you will need to specify the
gridWidth
constraint.让所有其他组件“跨越”两列。所以现在它们将占用与标签/文本字段组件相同的宽度。在这种情况下,您需要指定gridWidth约束。
Read the section from the Swing tutorial on How to Use GridBagLayout for more information on the various constraints used by GridBagLayout.
阅读有关如何使用GridBagLayout的Swing教程中的部分,以获取有关GridBagLayout使用的各种约束的更多信息。
also add some blank vertical space between the 2 buttons at the bottom
还可以在底部的2个按钮之间添加一些空白垂直空间
Again, look at the constraints. You could use the insets
constraint.
再看一下约束。您可以使用insets约束。