Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Why we extends JFrame class in java

843807Jun 1 2010 — edited Jun 1 2010
Hello Java Developers,

I am new to Java & Currently I am trying my hands on Java Swings.

I wrote a very simple program in Java to display a JFrame on screen. My code is as follows:

import javax.swing.*;

public class JavaFrame {
public static void main(String args[]){
JFrame frame = new JFrame();
frame.setTitle("My Frame");
frame.setSize(400, 150);
frame.setLocation(300, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

I saw some examples on internet & they are extending "JFrame" in their every example. I want to know what is the benefit of this ??? here is code:

import javax.swing.*;

public class JavaFrame extends JFrame {
public static void main(String args[]){
JFrame frame = new JFrame();
frame.setTitle("My Frame");
frame.setSize(400, 150);
frame.setLocation(300, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

---------------------------------

My second question is what is the meaning of line "frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);"

frame - This object of class created by me.

setDefaultCloseOperation() - This is predefined method in JFrame class.

JFrame.EXIT_ON_CLOSE - what is the meaning of this ??
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 29 2010
Added on Jun 1 2010
4 comments
299 views