Sure. Like most classes,
the JFrame
class can be extended.
JFrame
Class
The usual way of writing a GUI is to define your own
class by extending the JFrame
class.
Here is a program that does that.
It also includes several other new features.
import java.awt.*; import javax.swing.*; class MyFrame extends JFrame { // paint() is called automatically by the system // to display your customizations to the frame. public void paint ( Graphics g ) { // draw a String at location x=10 y=50 g.drawString("A MyFrame object", 10, 50 ); } } public class TestFrame2 { public static void main ( String[] args ) { MyFrame frame = new MyFrame(); // construct a MyFrame object frame.setSize( 150, 100 ); // set it to 150 wide by 100 high frame.setVisible( true ); // ask it to become visible frame.setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE ); } }
A JFrame
is painted
on the screen by the Java system whenever needed to keep the screen up to date.
The Java system does this automatically.
Your program does not need to keep track of what has been happening on the screen.
The class MyFrame
extends the class JFrame
.
MyFrame
objects, also, will be automatically re-painted when needed.
The standard parts of the frame—the background, the borders, the controls—are
drawn automatically by the Java system.
The paint()
method of your frame
is called by the Java system (not by you)
to draw your customizations to the frame.
You write a paint()
method
to customize what is painted.
Why would it be incorrect for main()
to do this:
frame.paint();