无论其大小如何,如何在 JPanel 中居中一组组件
Posted
技术标签:
【中文标题】无论其大小如何,如何在 JPanel 中居中一组组件【英文标题】:how to center a group of components in a JPanel regardless of its size 【发布时间】:2013-11-22 15:10:24 【问题描述】:我是 java 新手,我正在编写一个登录框架,在成功登录(到 mysql 数据库(使用 xampp 的本地主机))后,它会自行关闭并打开主框架。
我的登录屏幕有一个欢迎标签、一个用户名标签、一个密码标签、一个文本字段和一个密码字段以及一个登录按钮。我希望这些组件在窗口中居中并保持居中而不改变它们的大小,无论 jpanel 有多大(一旦它在屏幕上拖动它变大或变小)。
我的问题是,如果面板尺寸发生变化,我如何让它们保持居中并使其位置适应面板尺寸? 假设面板大小为 100*100,按钮为 50/50,面板变为 200/200,但按钮保持在 50/50,如果我将其设置为可调整大小的水平或垂直,它只会变大,但我希望它保持相同的大小只需将其位置调整为 100/100。
我无法发布我的框架的图像,因为这似乎需要我没有的声誉,因为我刚刚创建了我的帐户。我希望你能想象出我想要做什么以及我现在拥有什么。
我使用 GroupLayout 的 LoginFrame 代码如下(我将 actionlistener 排除在外,因为它不相关):
public LoginFrame()
setTitle("LoginFrame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 363, 270);
contentPane = new JPanel();
contentPane.setBackground(Color.ORANGE);
contentPane.setBorder(new EmptyBorder(0, 0, 0, 0));
setContentPane(contentPane);
JLabel lblWelcome = new JLabel("Welcome");
JLabel lblNewLabel = new JLabel("Username: ");
textField = new JTextField();
textField.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("Password: ");
passwordField = new JPasswordField();
JButton btnLogin = new JButton("Login");
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(107)
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addComponent(lblNewLabel)
.addComponent(lblNewLabel_1))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(passwordField, GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE)
.addComponent(textField, GroupLayout.DEFAULT_SIZE, 104, Short.MAX_VALUE))
.addGap(75))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(145)
.addComponent(btnLogin)
.addContainerGap(145, Short.MAX_VALUE))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(149)
.addComponent(lblWelcome)
.addContainerGap(155, Short.MAX_VALUE))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(56)
.addComponent(lblWelcome)
.addGap(18)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lblNewLabel))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lblNewLabel_1))
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(btnLogin)
.addContainerGap(64, Short.MAX_VALUE))
);
contentPane.setLayout(gl_contentPane);
我希望我能得到一些有用的答案,我试着具体一点,如果有什么遗漏,请告诉我。谢谢
【问题讨论】:
【参考方案1】:为此,我建议您使用GridBagLayout
(tutorial) 而不是GroupLayout
,因为它更简单,更灵活。它适用于GridBagConstraints
(read about it parametrs)。
在GridBagLayout
的帮助下,下一个代码可以做你想做的事情。
public class LoginFrame extends JFrame
private JPanel contentPane;
private JTextField textField;
private JPasswordField passwordField;
public LoginFrame()
setTitle("LoginFrame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 363, 270);
contentPane = new JPanel();
contentPane.setBackground(Color.ORANGE);
contentPane.setBorder(new EmptyBorder(0, 0, 0, 0));
setContentPane(contentPane);
JLabel lblWelcome = new JLabel("Welcome");
JLabel lblNewLabel = new JLabel("Username: ");
textField = new JTextField();
textField.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("Password: ");
passwordField = new JPasswordField();
JButton btnLogin = new JButton("Login");
contentPane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(5, 5, 5, 5);
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 2;
c.anchor = GridBagConstraints.CENTER;
contentPane.add(lblWelcome, c);
c.gridwidth = 1;
c.gridy = 1;
contentPane.add(lblNewLabel, c);
c.gridx = 1;
contentPane.add(textField, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridy = 2;
contentPane.add(passwordField, c);
c.fill = GridBagConstraints.NONE;
c.gridx = 0;
contentPane.add(lblNewLabel_1, c);
c.gridy = 3;
c.gridwidth = 2;
contentPane.add(btnLogin, c);
public static void main(String... s)
LoginFrame loginFrame = new LoginFrame();
loginFrame.setVisible(true);
【讨论】:
这真的帮了我很大的忙,它完成了我想要实现的目标!以上是关于无论其大小如何,如何在 JPanel 中居中一组组件的主要内容,如果未能解决你的问题,请参考以下文章