質問

I have a flowlayout panel which has a textfield inside. Although the textfield is not that wide but I want to place components on the next line. How do I fill the remaining space with an empty box?

役に立ちましたか?

解決

If you need more deterministic control over the way your components are laid out, you should consider using a different layout manager such as BoxLayout, GridLayout, or GridBagLayout. You may be able to trick FlowLayout into doing what you want, but what happens if the user resizes the window? Or changes their font DPI at the OS level? Instead of having your components be on different lines as a side effect of putting dummy components into a FlowLayout, consider expressing the above/below relationship more explicitly with a layout manager that has those concepts built in (e.g. GridBagConstraints.gridy).

他のヒント

Fill it with empty JPanel:

new JPanel();

No need to overcomplicate things here. This simple solution works (at least it did for me):

panelObject.setLayout(new FlowLayout(FlowLayout.LEADING));

This will flush the components to the left on a computer with left-to-right default reading direction (it’s called orientation), and the opposite way on computers with reading direction from the right. Thus it lends itself well to internationalization. If you want the components to the left on all computers (and the empty space to the right), use new FlowLayout(FlowLayout.LEFT).

Thanks to @nIcE cOw for suggesting this solution in a comment under the question, and for allowing me to post it as an answer.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top