简体   繁体   English

在单个JFrame中添加多个JPanel

[英]Adding more than one JPanel in a single JFrame

I have created a JFrame - now I want to add the 4 JPanel in that frame at a particular location. 我已经创建了一个JFrame现在我想在该框架中的特定位置添加4个JPanel How can set the location of panels in the frame? 如何设置面板在框架中的位置?

Use (possibly nested 1 ) layouts for the logic. 为逻辑使用(可能嵌套1 )布局。 See Laying Out Components Within a Container for details. 有关详细信息,请参见在容器中布置组件 They can: 他们能:

  • Include default spacing in the constructor (often) 在构造函数中包含默认间距(通常)
  • Calculate how big the GUI needs to be in order to display the components (in whatever PLAF, on whatever system the app. is deployed). 计算为了显示组件(在任何PLAF中,在部署了应用程序的任何系统上),GUI需要多大。

Extra spacing can be organized by adding an EmtpyBorder to child components. 可以通过向子组件添加EmtpyBorder来组织额外的间距。

  1. See the nested layout example 请参阅嵌套布局示例

Placing components in a container is quite a complicated subject in Swing. 在Swing中,将组件放置在容器中是一个非常复杂的主题。 Instead of defining the exact places for your components, you would normally use a layout manager that arranges them in a certain way. 通常不使用定义组件的确切位置的方法,而使用布局管理器以某种方式排列它们。

Here is the tutorial you should read to get a (visual) clue about the different layout managers: A Visual Guide to Layout Managers 这是您应该阅读的教程,以获得有关不同布局管理器的(直观)提示:布局管理器的可视指南

However, the standard layout managers of Swing can be cumbersome for more complex layouts. 但是,对于更复杂的布局,Swing的标准布局管理器可能会很麻烦。 Either, you could use nested layouts to get the desired result, or you could use a very powerful third-party library: JGoodies Forms . 您可以使用嵌套的布局来获得所需的结果,也可以使用功能非常强大的第三方库: JGoodies Forms The downside is of course that you have to learn yet another library. 缺点当然是您必须学习另一个图书馆。 Therefore, I would only recommend it for a bigger project. 因此,我只推荐用于更大的项目。

If its 4 locations, you can use BorderLayout ,by default its the CENTRE, but it also have EAST, WEST , NORTH, SOUTH locations for the placement of the components. 如果有4个位置,则可以使用BorderLayout ,默认情况下使用CENTRE,但也有EAST,WEST,NORTH,SOUTH位置用于放置组件。 You can also use setLocation to put the panels in the appropriate locations, if a layout isn't used . 如果layout isn't used ,也可以使用setLocation将面板放置在适当的位置。

Its even better to use GroupLayout developed my NetBeans team in 2005, use Windows Builder Pro, now provided by google for free. 使用GroupLayout最好在2005年开发了我的NetBeans团队,使用Windows Builder Pro,现在由Google免费提供。

For me it is good way to set GridbagLayout for the container of the frame. 对我来说,为框架的容器设置GridbagLayout是一个好方法。 There are several visual swing GUI editors available to do this easily. 有几种可视化的摆动GUI编辑器可轻松实现此目的。 You can use NetBeans GUI editor or GWT Designer (https://developers.google.com/web-toolkit/tools/gwtdesigner/) for complex GUI designing tasks 您可以使用NetBeans GUI编辑器或GWT Designer(https://developers.google.com/web-toolkit/tools/gwtdesigner/)完成复杂的GUI设计任务

  1. set the layout of the Frame to be null via setLayout(null) 通过setLayout(null)将Frame的布局设置为setLayout(null)
  2. create 4 JPanel and set their location using setLocation method 创建4个JPanel并使用setLocation方法设置其位置
  3. add these panels using JFrame's add method 使用JFrame的add方法添加这些面板

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM