Domanda

I'm a high-school student who is taking java I usually don't have many problems that I cant figure out without a bit of research however my teacher insists all student use panels when building GUI's.I have no problem building them however I haven't been able to get a decent answer clarifying the purpose of putting components in a Panel

È stato utile?

Soluzione 3

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

Altri suggerimenti

By using Panels it's easier to group components and arrange them in a Frame. Every Panel can have it's own layout. So you could give your outer Frame a BorderLayout and put a Panel into the center. The Panel then can have let's say a GridBagLayout for it's components.

The more complex the UIs are which you are building the more grateful you are for this possibility.

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. 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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top