简体   繁体   中英

Align JLabel to left or right on JPanel

如何将JPanel的黄色JLabel向右移动/对齐,并将其他JLabel向左移动/对齐?

You use an appropriate layout manager.

One option is to use a panel with a horizontal BoxLayout :

Box panel = Box.createHorizontalBox();
panel.add( leftLabel );
panel.add( Box.createHorizontalGlue() );
panel.add( yellowLabel );

Another option is to use a panel with a BorderLayout :

JPanel panel = new JPanel( new BorderLayout() );
panel.add(leftLabel, BorderLayout.LINE_START);
panel.add(yellowLabel, BorderLayout.LINE_END);

Read the section from the Swing tutorial on Layout Mangers for more information and working examples.

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