Difference between JPanel, JFrame, JComponent, and JApplet

后端 未结 3 1912
你的背包
你的背包 2021-01-30 01:52

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

相关标签:
3条回答
  • 2021-01-30 02:18

    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.

    0 讨论(0)
  • 2021-01-30 02:19

    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.

    0 讨论(0)
  • 2021-01-30 02:29

    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.

    0 讨论(0)
提交回复
热议问题