I'm fairly new to Java and I'm trying to create a GUI application with some labels, buttons, and textfields. The program is pretty simple and I just wanted to use a default layout, which is FlowLayout. I managed to place and size everything fine, but the only thing seem to be not working is the alignment. I want to place buttons and textfields with certain alignments, but whenever I set an alignment, it moves the text inside of whatever the object rather than the object itself. For example, I wrote:
button.setHorizontalAlignment(JButton.RIGHT);
but it seems like it aligns the text inside the button instead of the button itself. Is there any way to align the button itself rather than the text inside of it?
I know the alignment stuff could be easier with some other type of layout (e.g. BoxLayout), but I just want to use the FlowLayout for this one, unless it is impossible to align them using the FlowLayout (which I don't think so).
Thanks in advance.
A flow layout arranges components in a directional flow, much like lines of text in a paragraph. The flow direction is determined by the container's componentOrientation property and may be one of two values: ComponentOrientation.
FlowLayout(): creates a flow layout with centered alignment and a default 5 unit horizontal and vertical gap.
getAlignment(): Returns the alignment for this layout. setAlignment(int align): used to set the alignment for this layout.
The Panel and Applet classes use the FlowLayout as their default layout.
See the constructor FlowLayout(int align)
.
Constructs a new
FlowLayout
with the specified alignment and a default 5-unit horizontal and vertical gap. The value of the alignment argument must be one ofFlowLayout.LEFT
,FlowLayout.RIGHT
,FlowLayout.CENTER
,FlowLayout.LEADING
, orFlowLayout.TRAILING
.
It seems you are after a FlowLayout.RIGHT
as seen in this answer (the combo and check box at the top).
I don't think you can do this with a FlowLayout alone.
My suggestions would be:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With