I have a JPanel with some JLabel added with the add()
method of JPanel. I want to align the JLabel to the right like the image below but I don't know how to do that. Any Idea? Thanks!
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
This can be done in two ways.
JLabel Horizontal Alignment
You can use the
JLabel
constructor:To align to the right:
JLabel
also hassetHorizontalAlignment
:This assumes the component takes up the whole width in the container.
Using Layout
A different approach is to use the layout to actually align the component to the right, whilst ensuring they do not take the whole width. Here is an example with
BoxLayout
: