Difference between JPanel, JFrame, JComponent, and JApplet
Asked Answered
D

3

47

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?

Declaim answered 16/8, 2010 at 22:21 Comment(0)
G
45

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.

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.

JComponent - A base class for objects which intend to interact with 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?).

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.

Grindle answered 16/8, 2010 at 22:47 Comment(0)
B
20

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.

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.

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.

Bilyeu answered 16/8, 2010 at 22:43 Comment(0)
H
5

You might find it useful to lookup the classes on Oracle's website, for example:

https://docs.oracle.com/en/java/javase/15/docs/api/java.desktop/javax/swing/JFrame.html

There you can see that JFrame extends JComponent and 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 so that you can add things to them. JFrame provides a window, whereas JPanel is just a panel that goes inside a window.

Hod answered 16/8, 2010 at 22:27 Comment(3)
As a side note, the inheritance chain is ridiculous with some Swing components. It tends to make code completion in IDEs useless as well as clutter up javadocs. They really need to rewrite that thingBalthasar
I'd like to point out that JFrame does not extend JComponent or JContainer, but rather java.awt.Container and java.awt.Component in that order.Tildi
@LucasMorgan They must have made JFrame less dependent on Swing than I thought. Since it uses AWT, I wonder if you can use a JFrame in a JApplet.Grindle

© 2022 - 2024 — McMap. All rights reserved.