JFrame (or any frame) - Create VS Extend
Author |
Message |
Aziz
|
Posted: Mon Jul 18, 2005 3:46 pm Post subject: JFrame (or any frame) - Create VS Extend |
|
|
Is there any difference between creating and extending a JFrame?
Example:
Create
Java: |
public class OpenAFrame
{
public static void main (String[] args )
{
JFrame frame = new JFrame("Frame");
//Do some stuff//
frame. pack();
frame. setVisible(true);
}
}
|
Extend
Java: |
public class MyFrame extends JFrame
{
public MyFrame ()
{
setTitle ("This is my frame");
//Do some stuff//
pack ();
setVisible (true);
}
}
public class ShowMyFrame
{
public static void main (String[] args )
{
MyFrame frame = new MyFrame ();
}
}
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Mon Jul 18, 2005 7:33 pm Post subject: (No subject) |
|
|
Yes. There's a very significant semantic difference.
By simple creating a JFrame object, you are constrained to the JFrame class' interface by way of its private members.
By extending the class, you create your own class that "is a" JFrame. It can do everything a JFrame can, but you can also add features, or override features inherited from JFrame. |
|
|
|
|
|
Aziz
|
Posted: Mon Jul 18, 2005 7:54 pm Post subject: (No subject) |
|
|
I think you've looked at my character manager program? The main frame is instantiated JFrame, and the editor frame is a extend JFrame (not proper termenology, i know), and i haven't really noticed a difference, is there a difference for what I'm doing? What form is best for what situation? A, great master, lend me your all-wise knowledge thanks. |
|
|
|
|
|
wtd
|
Posted: Mon Jul 18, 2005 8:40 pm Post subject: (No subject) |
|
|
I have no looked extensively at your character manager program. I have a project currently underway. It may show up in the C/C++ tutorials sections fairly soon.
I will say that neither is "better". They are simply different. Understanding what each method means will enable you to decide which to use in which situations.
Understand OOP. Specifically inheritance and composition. |
|
|
|
|
|
Aziz
|
Posted: Mon Jul 18, 2005 9:08 pm Post subject: (No subject) |
|
|
So then, the question is of effeciency? Or, better yet, (just thought of this) whether you're simply using a frame for an application, or creating a frame to be used more than once? If you create an instance of JFrame, you're simply using JFrame's "features" (I guess thats what'd I called them?). But if I extend JFrame and make my own Frame, it could be a more specific frame, as in FormFrame where it consists of only form values (Labels, text components, etc?) Am I getting anywhere or do I think I know something and am running into a dead-end? It's all about the learning |
|
|
|
|
|
wtd
|
Posted: Mon Jul 18, 2005 10:56 pm Post subject: (No subject) |
|
|
Aziz wrote: But if I extend JFrame and make my own Frame, it could be a more specific frame
By George, I think he's got it! |
|
|
|
|
|
|
|