JaVa
   

What Is an app?

An app is a program that can either be a simple console window, which merely outputs text, or it can be a visual windowed program. We have so far been glued to the console window for our apps. In this section, we look at how we can create a windowed app in Java.

A Simple Java app

To create a windowed app in Swing, you can use an object of type JFrame. We can do this by making our own class extend the JFrame class or contain a JFrame object. The JFrame class is defined in the Swing package, which is imported as javax.swing.*. Note that the equivalent component in the AWT package java.awt.* is simply called Frame. This is consistent with most of the GUI objects (i.e., all the Swing GUI objects are named the same as the AWT versions, except they are prefixed by the letter "J"). Let's now look at two very simple examples of how we can create a 400x300 pixel window with "My app" in the title bar. They both have the same outcome, but the first extends a JFrame class, whereas the second just has a JFrame as a member of the class. Here are the two code listings.

Code Listing 8-1: Simple app (extending a JFrame)
import javax.swing.*;
public class Myapp extends JFrame
{
 public Myapp()
 {
 super("My app");
 setSize(400, 300);
 setDefaultCloseOperation(EXIT_ON_CLOSE);
 setVisible(true);
 }
 public static void main(String args[])
 {
 Myapp theApp = new Myapp();
 }
}


Java End example
Code Listing 8-2: Simple app (JFrame as a member)
import javax.swing.*;
public class Myapp
{
 public static void main(String args[])
 {
 JFrame appFrame = new JFrame("My app");
 appFrame.setSize(400, 300);
 appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 appFrame.setVisible(true);
 }
}


Java End example

When we execute either of these two example apps, we will see that they create a window that looks like this:

Java Click To expand
Screenshot-1: Our basic app window

Let's now look at the first example, which extends the JFrame class in order to create the window. First, we need to include the Swing package, which is accomplished using the following import statement:

import javax.swing.*;


Now we declare our Myapp class to extend the JFrame class, which will make it inherit all the members and methods of the JFrame class (if you are unsure about inheritance, refer back to ). The class declaration can be seen in the following line of code:

public class Myapp extends JFrame


Next, we need to declare a main method that is the entry point to the app code. All we need to do in this method is instantiate our Myapp class so that the constructor will be called. The complete main method can be seen here:

public static void main(String args[])
{
 Myapp theApp = new Myapp();
}


In the constructor of Myapp, we first make a call to the constructor of the super class (i.e., the JFrame class that we are extending) and pass a single argument that represents the title of our app. This can be seen here:

super("My app");


Note you can also use the method setTitle of the JFrame to later change the title. Next, we need to set the size of our app window, which we do by making a call to the setSize method, passing the width and the height as parameters, respectively. Note that the setSize method is a member of the JFrame class. This can be seen in the following line of code:

setSize(400, 300);


Then we set the default close operation for the app (e.g., what happens when the user clicks the "x" in the top-right corner of the window or presses Alt+F4 in Windows). For this, we specify that we wish the app to EXIT_ON_CLOSE. This can be seen in the following line of code:

setDefaultCloseOperation(EXIT_ON_CLOSE);


We will look at closing windowed apps properly in the next chapter, so do not worry about this section of code in this chapter. Finally, we make a call to the setVisible inherited method. Passing true as a parameter informs the JFrame heavyweight object to make itself visible. This can be seen in this final line of code:

setVisible(true);


For the other example code, we do not extend the JFrame. Instead, we create a new JFrame object in the main method and assign it to our member variable appFrame. Note again how we pass the title of our app as the constructor. This can be seen in the following line of code:

JFrame appFrame = new JFrame("My app");


Once we have our object instantiated, we can then use the object to call the setSize, setVisible, and setDefaultCloseOperation methods in a similar way to the first example code. This can be seen in the final three lines of code, as well as the default close operation line.

appFrame.setSize(400, 300);
appFrame.setVisible(true);
appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


Note here that the EXIT_ON_CLOSE definition is a public constant member of the JFrame class, and hence we have to specify it as such (i.e., JFrame.EXIT_ON_CLOSE). In the previous code listing, we extended the JFrame, and hence the EXIT_ON_CLOSE variable was therefore a member of our own class as it extended the JFrame, inheriting this member.

JaVa
   
Comments