简体   繁体   English

在java中Panels的目的是什么

[英]What is the purpose of Panels in java

我是一名正在服用java的高中生我通常没有很多问题,但我没有一点研究就弄清楚但是我的老师坚持要求所有学生在构建GUI时都使用面板。我没有问题但是我没有问题无法得到一个体面的答案,澄清将组件放入Panel中的目的

By using Panel s it's easier to group components and arrange them in a Frame . 通过使用Panel可以更轻松地对组件进行分组并将其排列在Frame Every Panel can have it's own layout. 每个Panel都可以拥有自己的布局。 So you could give your outer Frame a BorderLayout and put a Panel into the center. 因此,您可以为外部框架提供BorderLayout并将Panel放入中心。 The Panel then can have let's say a GridBagLayout for it's components. 然后, Panel可以为它的组件说一个GridBagLayout

The more complex the UIs are which you are building the more grateful you are for this possibility. 您正在构建的UI越复杂,您就越感激这种可能性。

Panels exist for a few reasons. 小组存在的原因有几个。

One is to make organization easier (both from a code and a UI standpoint) in windows with clear multiple areas. 一种是在具有明确多个区域的窗口中使组织更容易(从代码和UI角度来看)。 Having more than one panel in a single frame would let you separate vastly different code into different classes. 在一个框架中有多个面板可以让您将截然不同的代码分成不同的类。

Another reason is for frames that totally change appearance on an action. 另一个原因是框架完全改变了动作的外观。 Many beginners are inclined to keep one panel in the frame for situations like this and simply wipe the panel and put new components in on each change. 许多初学者倾向于在这种情况下将一个面板保留在框架中,只需擦拭面板并在每次更改时放入新组件。 However, a much simpler (and more efficient) panel-driven solution is to have one panel for each different version of the frame and swap them out. 然而,更简单(也更有效)的面板驱动解决方案是为每个不同版本的框架提供一个面板并将其交换出来。

Lastly, it lets you use different types of layouts in different areas of the frame, which leads to more dynamic and flexible UIs. 最后,它允许您在框架的不同区域使用不同类型的布局,从而实现更加动态和灵活的UI。

A panel is a container. 面板是一个容器。

  • It allows you to group components together 它允许您将组件组合在一起
  • It allows you to devise complex interfaces, as each panel can have a different layout, allowing you to leverage the power of different layout managers. 它允许您设计复杂的界面,因为每个面板可以具有不同的布局,允许您利用不同布局管理器的强大功能。
  • It allows you to build reusable components and isolate responsibility 它允许您构建可重用的组件并隔离责任
  • But most of all, it gives you the bases for deciding how the panel should be deployed. 但最重要的是,它为您决定如何部署面板提供了基础。 With a panel you can add it to a frame or applet or another component as needed... 使用面板,您可以根据需要将其添加到框架或小程序或其他组件中...
  • A panel also makes a good surface onto which to perform custom painting. 面板也是一个很好的表面,可以在其上执行自定义绘画。 For all the benefits mentioned above - its isolated and reusable... 对于上面提到的所有好处 - 它是孤立的和可重复使用的......

There are many other reasons for using panels, these are just a few that pop to mind 使用面板还有很多其他原因,这些只是我想到的一些原因

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

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