Why we extends JFrame class in java
843807Jun 1 2010 — edited Jun 1 2010Hello 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 ??