Creating our First Window
In order to create windows in Java, we are going to need the components located in javax.swing. Well, java.awt also contains window components although they appear to be unstable especially when it comes to different operating environments.
So we start by creating our first window:
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class FrameDemo{
public static void main(String args[]){
JFrame myFrame = new JFrame("This is my frame");
myFrame.setSize(300,400);
myFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
myFrame.setVisible(true);
}
}
which would produce an output like this:

Code Explanation
On our first lines of code, we have just imported the library classes which we will use. First is JFrame and the next is WindowConstants. JFrame is used for creating windows. WindowConstants constants needed in creating windows.
On our main method, we create an instance of the JFrame by giving it an object name and a title as well:
JFrame myFrame = new JFrame("This is my frame");
Then we set what will happens when we close the window(such as clicking on the close button):
myFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
we set this in order to make sure that our window exits the moment we close it. Other constants can be used if preferred which are namely:
- DO_NOTHING_ON_CLOSE - nothing happens when you click the close button.
- HIDE_ON_CLOSE - The window dissapears but it is still there. You may show this using the setVisible() method
- DISPOSE_ON_CLOSE - destroys the object created.
try them by replacing the code with these constants to observe the effects. I suggest you use an Integrated Development Environment (IDE) first when you're going to try this. Most IDE's have a stop tool that lets you terminate a process.
And out last line:
myFrame.setVisible(true);
by default, the frame is set by false. Unless you include this code, you won't be seeing it. You may also use this code for hiding frames. We will be using this on our further examples.
Getting Inside
Inheriting our Class
So far, what we were doing on the previous examples was merely just creating a window. If you really want to manipulate a window, you have to build it from inside. One way to do this is to make your class 'be' the window. This method is called, inheritance-which is making your class inherit all properties as well as behaviors. This can be done with the use of the extends keyword.
But before we start, I would like to first introduce to you some container classes:
- JFrame - the basic java application window. It has a title bar and a provision for adding a menu
- JDialog - This usually appears as a prompt for inputting data as well as displaying them.
- JApplet - This is designed to run embedded in a web page. You can draw and add menu components here
- JPanel - used as a container that can be created within a window. Takes also some behaviors of JApplet where you can also draw and add some components except menu types.
Now let us proceed to our program. First we apply these principles to our previous program. The code should look like this:
import javax.swing.JFrame;
public class FrameDemo2 extends JFrame{
/**
* Constructor - This method automatically run whenever an object of FrameDemo2 is created.
**/
public FrameDemo2(){
super("Behold, our new frame");
}
public static void main(String args[]){
FrameDemo2 myFrame = new FrameDemo2();
myFrame.setSize(300,400);
myFrame.setLocationRelativeTo(null);
myFrame.setDefaultCloseOperation(myFrame.EXIT_ON_CLOSE);
myFrame.setVisible(true);
}
}
The advantage of this code is encapsulation, and designing modular, reusable, and extensible code For example, we could then extend the MyFrame class to further customize it. And this way, we can also set up the JFrame without cramming all the code into one .java File or class (which an author of a book I read called, "The Giant Objects Syndrome").
The super() function is for calling the constructor of the base class(the class inherited from) which specifies the title. This is just for giving your frame a default title. I almost forgot, you can set the title of the frame by using the setTitle() property:
myFrame.setTitle("This is the customized title");
Reference:
Beginning Java 2 by Ivor Horton
Thinking in Java by Bruce Eckel
You can also find this topic in Squidoo.com(Basic GUI in JAVA)







MultiQuote









|