Header javaperspective.com
JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.2. Your first JFrame

5.2. Your first JFrame
Last updated: 1 February 2013.

This tutorial will show you how to create and display a top-level container onscreen with the class JFrame.

A JFrame is a graphical window that has a title, a border and buttons for closing, resizing and iconifying. A convenient way to create a JFrame is to make a subclass of the class JFrame. For example, the class FirstFrame shown below extends JFrame and sets its title, size and location to the desired values by calling the methods setTitle(String title), setSize(int width, int height) and setLocation(int x, int y):

import javax.swing.JFrame;

public final class FirstFrame extends JFrame{

   
public FirstFrame() {
          setTitle
("My first JFrame");
         
setSize(300, 200);
          setLocation
(200, 100);
         
          setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
          setVisible
(true);
   
}
}

In the above sample, the width of the window is set to 300 pixels and its height to 200 pixels in the call setSize(300, 200). The invocation setLocation(200, 100) sets the location of the top-left corner of the frame onscreen to (200, 100). In fact, the screen has a horizontal axis (x-axis) and a vertical axis (y-axis). The top-left corner of the screen is the origin (0, 0) and the x-axis goes from left to right whereas the y-axis goes from top to bottom. Hence, (x, y) defines a location where x is the position on the x-axis and y the position on the y-axis.

If you want the JFrame to be in the middle of the screen, instead of calling the method setLocation(int x, int y), you can invoke the method setLocationRelativeTo(Component c) with a null value like this:

setLocationRelativeTo(null);

The call setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) defines the behaviour of the window when the user clicks the close button. In this case, the application exits. The other possible values are WindowConstants.DO_NOTHING_ON_CLOSE, WindowConstants.HIDE_ON_CLOSE and WindowConstants.DISPOSE_ON_CLOSE. Finally, setVisible(true) sets the visibility of the window to true.

A key point to remember is that when working with a given class like JFrame, you can take advantage of the Swing class hierarchy by using the functionalities provided by the super classes as well. For example, let's say you don't want the created graphical window to be resizable. First, you naturally search in the Java API documentation for a method that meets your needs among the methods of the class JFrame. Generally, in the Java API documentation, method names speak for themselves and you can reasonably expect such a method to have a name like setResizable. Since there is no setResizable method in the class JFrame, the next step is to search in the super class and so on until you find the right method. Luckily, there is a setResizable method in the class Frame which is the direct super class of the class JFrame. To prevent the window from being resizable, just add the following statement: setResizable(false);


As you can see, there is no main method in the class FirstFrame because it is good programming practice to separate the code that builds a GUI from the class containing the main method. The class App shown below contains a main method that creates an instance of the class FirstFrame. The code in the main method will be explained in the next tutorial.

import javax.swing.SwingUtilities;

public final class App {

   
public static void main(String[] args){
         
SwingUtilities.invokeLater(new Runnable() {
               
public void run() {
                     
new FirstFrame();
               
}
          })
;
   
}
}



Here are snapshots of the window on different systems:

Mac OS X

Windows XP

Linux (Ubuntu)

A window in a Mac system A window in a Windows system A window in a Linux system


Yet, the window does not contain any GUI element. The method add(Component comp) provided by the class Container allows you to add a GUI element to a container. For example, the next sample shows how to add a button named Button to the window:

import javax.swing.JButton;
import javax.swing.JFrame;

public final class FirstFrame extends JFrame{

   
public FirstFrame() {
         
setTitle("My first JFrame");
          setSize
(300, 200);
          setLocation
(200, 100);
          setResizable
(false);
         
          JButton button =
new JButton("Button");
          add
(button);
         
          setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
          setVisible
(true);
   
}
}


Here is the result on a Mac OS X system:


A window containing a button


The button takes all the available space in the container because no particular layout or size was specified. You will see how to lay out a container in the tutorial Layouts. If you want the frame's size to fit the preferred size of the elements it contains, call the method pack provided by the class Window.

Generally, GUI elements are simply termed components. Hence, from now on, GUI elements will be referred to as components.

The next tutorial will show you how to make the above button react to user clicks and more generally, how to add event listeners to components.


You are here :  JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.2. Your first JFrame
Next tutorial :  JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.3. Event listeners

Copyright © 2013. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7