Layout managers to set this specific layout in java

做~自己de王妃 提交于 2019-12-02 01:24:21
Andrew Thompson

This is how I would approach it. Instead of adding the outer panel to a frame though, it would be added to a tab of the tabbed pane.

The above is an example of a nested or compound layout. The titled borders show the layouts used and the arguments (if any) used to construct them.

The size of the button is suggested by the content (the text). The sizes of the text fields and text area is suggested in the constructor (which itself has been included as the text value).

To get the TALL effect in the text fields, set an HUGE font but use less columns for the constructor.

See also

Another nested layout.

DSquare

GridBagLayout is the most powerful Layout that you can use to easily implement grid-like displays. It's a layout with n rows and m columns where each cell is customizable independently of the others in several aspects. In this layout you have to attach a GridBagConstraints object to each panel.add(JComponent, Constraints) as constraints. In the tutorial it's clearly specified what is customizable. It might look a little harsh at the beginning but once you get the hang of it it's great. It's powerful and flexible and you don't have to worry about the uncustomizable restrictions you might encounter with other Layouts.

In your layout, the most inconvenient thing I see is having the "Account ID" label aligned by its center with the TextField AND with the empty space over both of them. It would be easier if the label was aligned with the bottom of the TextField. To solve this I'll assume that the Label and the TextField are inside a panel I constructed beforehand that aligns each other correctly (easily with BorderLayout or GridBagLayout... or anything really) and I'll just place the panels in the Layout.

Then I see this Layout as a GridBagLayout with 3 rows and 2 columns that looks like this:

This is an overview of how I'd put the constraints to specify each component in the layout.

Panel 1 (Account ID Label + TextField)

gridx = 0
gridy = 0
weighty = 0.5
weightx = 0.5
anchor = PAGE_END
fill = HORIZONTAL

Panel 2 (Amount Label + TextField)

gridx = 0
gridy = 1
weighty = 0.0
fill = HORIZONTAL

Button

gridx = 0
gridy = 2
anchor = PAGE_START
weighty = 0.5

TextArea

gridx = 1
gridy = 0
gridheight = 3
weightx = 0.5
fill = BOTH

There is a couple of details I overlooked but the core issues can be aproached with these constraints. The least obvious thing to learn about the GridBagLayout is how do weights work in complex cases, for example what happens when there are several different weightx values in the same column. Does it count the max? or the sum?...

For the sake of discussion, you could avoid having those panels using an extra initial row with an invisible component with weighty > 0 and then having 2 columns: one for the JLabels and the other one for the JTextFields, with the apropiate anchors; the button would have gridwith = 2... but that's totally unnecesary, go for the two auxiliar panels.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!