Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class test extends JFrame {
public static void main(String[] args) // why static???
{
JFrame theFrame = new JFrame ("test");
theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
theFrame.setLayout(new FlowLayout ());
JPanel thePanel = new JPanel ();
thePanel.setPreferredSize(new Dimension (600,600));
theFrame.add(thePanel);
theFrame.pack ();
theFrame.setVisible(true);
}
}
Is this the only way to do it? Of course not. The best way? That's for you to decide. What are the alternatives? That's for you to play with and find out for yourself.
Why static?
In Java, everything's a class (or just about everything).
You can "draw()" a "circle object"; a circle object "has a" radius and "has a" color (both "color" and "radius" being examples of "instance data"), etc etc.
But what happens if you want to declare a variable, or have a method, that's *independent* of any particular object?
That's what the (non-intuitively named) "static" keyword is for in Java: to denote some data item or some operation that doesn't belong to any particular object, but instead to the class as a whole.
Declaring a method "public static void main (String[] args)" is the way to tell the Java runtime "If I tell you to 'run this class', this is the place I want you to begin program execution'".
And guess what - you can have different "public static void main ()" methods in different classes - unlike C/C++, you *don't* have to have just one "main()" in your entire program. That's a neat way to create and drive per-class unit tests.
ANYWAY:
1. The point in the code above is:
a) You have a "JFrame" as your "main window": it has the title bar, it holds your menu bar and tool bars, etc etc
b) It's also a "container" for your user interface
c) As such it has a layout manager (whether or not you explicitly specify one!) that organizes its contents
d) Your "user interface" consists of organizing UI objects - and other containers - inside your JFrame
e) It's a much more sophisticated model than, say, Visual Basic (where you just slap button #1 at x/y 100, 10). The
Java model is much more flexible, and much better at automatically handling different screen resolutions and sizes.
2. A couple of good resources include:
a) Sun'sJava tutorials:
http://java.sun.com
b) "Just Java", Peter van der Linden
http://www.bookpool.com/sm/0131482114
<= AN EXCELLENT BOOK ABOUT JAVA, AN EXCELLENT INTRO TO OO PROGRAMMING
c) "Head First Java", Kathy Sierra/Bert Bates
http://www.bookpool.com/sm/0596009208
<= MAY OR MAY NOT BE TO YOUR TASTES:
LOOK AT BOTH "JUST JAVA" AND "HEADFIRST JAVA", AND TAKE THE ONE THAT APPEALS MORE
... and ...
d) Java Ranch:
http://www.javaranch.com
<= AN EXCELLENT SITE FOR POSTING QUESTIONS
'Hope that helps .. PSM