更新时间: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 Border
s, resizability (is the JFrame
resizeable?), title bar, controls (minimize/maximize allowed?), and event handlers for various Event
s 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 Component
s together.
使用 LayoutManager
时,这很有用。 GridLayout
fi将组件添加到不同的 JPanel
,然后将其添加到 JFrame
创建gui。它在布局
和可重用性方面更易于管理。
It is useful when working with LayoutManager
s e.g. GridLayout
f.i adding components to different JPanel
s 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 JPanel
s.