简体   繁体   中英

How do i use border layouts?

I'm currently working on Layouts in Java. I'm trying to combine different layouts. So i have created a login Screen using Spring Layout, GridLayout and Border Layout. The MainFrame (JFrame) uses GridLayout. The GridLayout consists of 2 Panel (North Panel and Main Panel). The Main Panel consists of the Jlabel, JTextfield and JButton of which I have no problem of. My problem is in the North Panel which uses Border Layout. It contains a 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.

Simply change its creation to :

JLabel lblWelcome = new JLabel("Welcome To The Login Screen", SwingConstants.CENTER);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM