Java JPanel、JFrame、JComponent 和 JApplet 之间的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3497889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:19:25  来源:igfitidea点击:

Difference between JPanel, JFrame, JComponent, and JApplet

javajframejpaneljappletjcomponent

提问by Andyroo

I'm making a physics simulator for fun and I was looking up graphics tutorials when I tried to figure out the difference between all these J's. Can somebody elaborate on them or perhaps provide a link to a helpful source?

我正在制作一个有趣的物理模拟器,当我试图找出所有这些 J 之间的区别时,我正在查找图形教程。有人可以详细说明它们,或者提供一个有用来源的链接吗?

回答by Ultimate Gobblement

You might find it useful to lookup the classes on oracle. Eg:

您可能会发现在 oracle 上查找类很有用。例如:

http://download.oracle.com/javase/1.4.2/docs/api/javax/swing/JFrame.html

http://download.oracle.com/javase/1.4.2/docs/api/javax/swing/JFrame.html

There you can see that JFrame extends JComponent and JContainer.

在那里您可以看到 JFrame 扩展了 JComponent 和 JContainer。

JComponent is a a basic object that can be drawn, JContainer extends this so that you can add components to it. JPanel and JFrame both extend JComponent as you can add things to them. JFrame provides a window, whereas JPanel is just a panel that goes inside a window. If that makes sense.

JComponent 是一个可以绘制的基本对象,JContainer 扩展了它,以便您可以向其添加组件。JPanel 和 JFrame 都扩展了 JComponent,因为您可以向它们添加内容。JFrame 提供了一个窗口,而 JPanel 只是一个进入窗口内部的面板。如果这是有道理的。

回答by aco

JFrame and JApplet are top level containers. If you wish to create a desktop application, you will use JFrame and if you plan to host your application in browser you will use JApplet.

JFrame 和 JApplet 是顶级容器。如果您希望创建桌面应用程序,您将使用 JFrame,如果您计划在浏览器中托管您的应用程序,您将使用 JApplet。

JComponent is an abstract class for all Swing components and you can use it as the base class for your new component. JPanel is a simple usable component you can use for almost anything.

JComponent 是所有 Swing 组件的抽象类,您可以将其用作新组件的基类。JPanel 是一个简单可用的组件,您几乎可以将其用于任何事情。

Since this is for a fun project, the simplest way for you is to work with JPanel and then host it inside JFrame or JApplet. Netbeans has a visual designer for Swing with simple examples.

由于这是一个有趣的项目,对您来说最简单的方法是使用 JPanel,然后将其托管在 JFrame 或 JApplet 中。Netbeans 有一个带有简单示例的 Swing 视觉设计器。

回答by Kelly S. French

Those classes are common extension points for Java UI designs. First off, realize that they don't necessarily have much to do with each other directly, so trying to find a relationship between them might be counterproductive.

这些类是 Java UI 设计的常见扩展点。首先,要意识到它们之间不一定有直接关系,因此试图找到它们之间的关系可能会适得其反。

JApplet- A base class that let's you write code that will run within the context of a browser, like for an interactive web page. This is cool and all but it brings limitations which is the price for it playing nice in the real world. Normally JApplet is used when you want to have your own UI in a web page. I've always wondered why people don't take advantage of applets to store state for a session so no database or cookies are needed.

JApplet- 一个基类,可让您编写将在浏览器上下文中运行的代码,例如交互式网页。这很酷,但它带来了限制,这是它在现实世界中表现良好的代价。通常,当您希望在网页中拥有自己的 UI 时,会使用 JApplet。我一直想知道为什么人们不利用小程序来存储会话的状态,因此不需要数据库或 cookie。

JComponent- A base class for objects which intend to interact with Swing.

JComponent- 打算与 Swing 交互的对象的基类。

JFrame- Used to represent the stuff a window should have. This includes borders (resizeable y/n?), titlebar (App name or other message), controls (minimize/maximize allowed?), and event handlers for various system events like 'window close' (permit app to exit yet?).

JFrame- 用于表示窗口应该具有的东西。这包括边框(可调整大小的 y/n?)、标题栏(应用程序名称或其他消息)、控件(允许最小化/最大化?)以及各种系统事件的事件处理程序,如“窗口关闭”(允许应用程序退出?)。

JPanel- Generic class used to gather other elements together. This is more important with working with the visual layout or one of the provided layout managers e.g. gridbaglayout, etc. For example, you have a textbox that is bigger then the area you have reserved. Put the textbox in a scrolling pane and put that pane into a JPanel. Then when you place the JPanel, it will be more manageable in terms of layout.

JPanel- 用于将其他元素聚集在一起的通用类。这对于使用可视布局或提供的布局管理器之一(例如 gridbaglayout 等)更为重要。例如,您有一个比您保留的区域大的文本框。将文本框放入滚动窗格并将该窗格放入 JPanel。然后当你放置 JPanel 时,它在布局方面会更易于管理。