views:

134

answers:

3

Hi, im 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. could somebody elaborate on them or perhaps provide a link to a helpful source?

+1  A: 

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

http://download.oracle.com/javase/1.4.2/docs/api/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 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.

DrDipshit
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 thing
TheLQ
+2  A: 

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

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

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

aco
+1  A: 

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.

Kelly French