桌面应用程序中的布局
我正在开发一个java桌面应用程序,其中包含许多具有不同和相同布局的面板。现在,当我想设置面板新布局时,我总是创建新布局。 EG
JPanel panel = new JPanel(new GridLayout(0, 1));
创建一些 LayoutFactory
并在此处创建我需要的所有布局然后设置到面板是正确的解决方案吗?
I am developing one java desktop application with a lots of panels with different and same layouts. Now when I want to set to panel new layout I always create new layout. E.G.
JPanel panel = new JPanel(new GridLayout(0, 1));
Is the correct solution to create some LayoutFactory
and here create all the layouts that I need and then set to the panels?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1) 通过 new
GridLayout(0, 1)
放置 JPanel 应该与BorderLayout.CENTER
相同,或者可以使用BoxLayout
,在这种情况下,您整个JPanel
只放置一个JComponent
2) 最简单的方法是通过
Nested Layout
放置JComponents
,其中每个 Container 都可以有不同的 LayoutManager,相同的LayoutManager
或其组合3 )通过使用 GridBagLayout 或 MigLayout 可以(在大多数情况下)为整个容器放置每个
JComponents
一次,或者 <代码>JPanel1) laying JPanel by new
GridLayout(0, 1)
should be same asBorderLayout.CENTER
or possible by usingBoxLayout
, in this case you place only oneJComponent
for wholeJPanel
2) easiest way is lay
JComponents
byNested Layout
where each of Container can have different LayoutManager, sameLayoutManager
or their combinations3) by using GridBagLayout or MigLayout is possible (in most of cases) place every
JComponents
once time for whole container orJPanel
hudi,如果你想让你的代码更简洁,你可以编写创建和分配布局的辅助方法。如果您仅在一个类中设置布局,则辅助方法可以是该类的
私有
方法。或者,如果您要在各种类中设置布局,则可以将受保护的辅助方法添加到公共超类(如果有的话),或者作为静态方法实用程序类。发布您的代码详细信息,我们可以给出更具体的建议。
hudi, if you want to make your code more concise, you can write helper methods which create and assign the layouts. If you are setting up layouts in only one class, the helper methods can be
private
methods of that class. Or if you are setting up layouts in various classes, you may be able to addprotected
helper methods to a common superclass (if you have one), or asstatic
methods on a utility class.Post the details of your code and we can give more specific suggestions.