[英]How do i use border layouts?
I'm currently working on Layouts in Java.我目前正在研究 Java 布局。 I'm trying to combine different layouts.
我正在尝试结合不同的布局。 So i have created a login Screen using Spring Layout, GridLayout and Border Layout.
所以我使用 Spring Layout、GridLayout 和 Border Layout 创建了一个登录屏幕。 The MainFrame (JFrame) uses GridLayout.
MainFrame (JFrame) 使用 GridLayout。 The GridLayout consists of 2 Panel (North Panel and Main Panel).
GridLayout 由 2 个面板(北面板和主面板)组成。 The Main Panel consists of the Jlabel, JTextfield and JButton of which I have no problem of.
主面板由 Jlabel、JTextfield 和 JButton 组成,我对此没有任何问题。 My problem is in the North Panel which uses Border Layout.
我的问题是在使用边框布局的北面板中。 It contains a JLabel (lblWelcome).
它包含一个 JLabel (lblWelcome)。 I have been trying to bring the label to the center of the panel using Border Layout but it still aligns to the left.
我一直在尝试使用边框布局将标签带到面板的中心,但它仍然向左对齐。 This is the below code:
这是下面的代码:
JLabel lblWelcome = new JLabel("Welcome To The Login Screen");
JPanel northPanel = new JPanel(new BorderLayout());
northPanel.setBackground(Color.green);
northPanel.add(lblWelcome, BorderLayout.CENTER);
Login Screen :登录屏幕:
Your JLabel
is actually correctly centered, but its text isn't.您的
JLabel
实际上正确居中,但它的文本不是。
Simply change its creation to :只需将其创建更改为:
JLabel lblWelcome = new JLabel("Welcome To The Login Screen", SwingConstants.CENTER);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.