且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java中的JPanel vs JFrame

更新时间:2023-12-05 08:43:04

你不应该扩展 JFrame 类不必要的(仅当您在 JFrame 类中添加额外功能时)

You should not extend the JFrame class unnecessarily (only if you are adding extra functionality to the JFrame class)

JFrame

JFrame extends Component 容器

JFrame extends Component and Container.

它是一个***容器,用于表示窗口的最低要求。这包括边框 s,可恢复性( JFrame 可调整大小?),标题栏,控件(最小化/最大化允许? ),以及各种事件的事件处理程序,如 windowClose windowOpened 等。

It is a top level container used to represent the minimum requirements for a window. This includes Borders, resizability (is the JFrame resizeable?), title bar, controls (minimize/maximize allowed?), and event handlers for various Events like windowClose, windowOpened etc.

JPanel

JPanel extends Component Container JComponent

JPanel extends Component, Container and JComponent

这是一个泛型类,用于将其他组件组合在一起。

It is a generic class used to group other Components together.


  • 使用 LayoutManager 时,这很有用。 GridLayout fi将组件添加到不同的 JPanel ,然后将其添加到 JFrame 创建gui。它在布局和可重用性方面更易于管理。

  • It is useful when working with LayoutManagers e.g. GridLayout f.i adding components to different JPanels which will then be added to the JFrame to create the gui. It will be more manageable in terms of Layout and re-usability.

在Swing中绘画/绘图时,你会覆盖 paintComponent(..)当然还有双缓冲的全部乐趣。

It is also useful for when painting/drawing in Swing, you would override paintComponent(..) and of course have the full joys of double buffering.

没有***容器就不能存在Swing GUI ( JWindow Window JFrame Frame Applet ),但可能没有 JPanel s。

A Swing GUI cannot exist without a top level container like (JWindow, Window, JFrame Frame or Applet), while it may exist without JPanels.