Introduction to GUI in Java

Most of the example programs we've looked at so far on this website have been Java applications. More specifically, they have been command-line applications, which interact with the user only through simple text prompts. A Java application can have graphical components as well. Regularly on this website, we will explore the capabilities of Java to create programs with graphical user interfaces (GUIs). In this post, we establish the basic issues regarding graphics-based applications.

A GUI component is an object that represents a screen element that is used to display information or to allow the user to interact with the program in a certain way. GUI components include labels, buttons, text fields, scroll bars, and menus.

Java components and other GUI-related classes are defined primarily in two packages: java.awt and javax.swing. (Note the x in javax.swing.) The Abstract Windowing Toolkit (AWT) was the original Java GUI package. It still contains many important classes, such as the Color class that we used in Chapter 2. The Swing package was added later and provides components that are more versatile than those of the AWT package. Both packages are needed for GUI development, but we will use Swing components whenever there is an option.

A container is a special type of component that is used to hold and organize other components. Frames and panels are two examples of Java containers. Let’s explore them in more detail.

A frame is a container that is used to display GUI-based Java applications. A frame is displayed as a separate window with its own title bar. It can be repositioned on the screen and resized as needed by dragging it with the mouse. It contains small buttons in the corner of the frame that allow the frame to be minimized, maximized, and closed. A frame is defined by the JFrame class.

A panel is also a container. However, unlike a frame, it cannot be displayed on its own. A panel must be added to another container for it to be displayed. Generally a panel doesn’t move unless you move the container that it’s in. Its primary role is to help organize the other components in a GUI. A panel is defined
by the JPanel class.

We can classify containers as either heavyweight or lightweight. A heavyweight container is one that is managed by the underlying operating system on which the program is run, whereas a lightweight container is managed by the Java program itself. Occasionally this distinction will be important as we explore GUI development. A frame is a heavyweight component, and a panel is a lightweight component.

Heavyweight components are more complex than lightweight components in general. A frame, for example, has multiple panes, which are responsible for various characteristics of the frame window. All visible elements of a Java interface are displayed in a frame’s content pane.

Generally, we can create a Java GUI-based application by creating a frame in which the program interface is displayed. The interface is often organized onto a primary panel, which is added to the frame’s content pane. The components in the primary panel are often organized using other panels as needed.

Containers are generally not useful unless they help us organize and display other components. Let’s examine another fundamental GUI component. A label is a component that displays a line of text in a GUI. A label can also display an image, a topic discussed later in this chapter. Usually, labels are used to display
information or identify other components in the GUI. Labels can be found in almost every GUI-based program.

Let's look at this example that uses frames, panels, and labels.

import java.awt.*;
import javax.swing.*;

public class Authority
{
public static void main (String[] args)
{
JFrame frame = new JFrame ("Authority");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

JPanel primary = new JPanel();
primary.setBackground (Color.yellow);
primary.setPreferredSize (new Dimension(250, 75));

JLabel label1 = new JLabel ("Question authority,");
JLabel label2 = new JLabel ("but raise your hand first.");
primary.add (label1);
primary.add (label2);

frame.getContentPane().add(primary);
frame.pack();
frame.setVisible(true);
}
}
When the program in above is executed, a new window appears on the screen displaying a phrase.


The text of the phrase is displayed using two label components. The labels are organized in a panel, and the panel is displayed in the content pane of the frame.

The JFrame constructor takes a string as a parameter, which it displays in the itle bar of the frame. The call to the setDefaultCloseOperation method determines what will happen when the close button (the X) in the corner of the frame is clicked. In most cases we’ll simply let that button terminate the program, as
indicated by the EXIT_ON_CLOSE constant.

A panel is created by instantiating the JPanel class. The background color of the panel is set using the setBackground method. The setPreferredSize method accepts a Dimension object as a parameter, which is used to indicate the width and height of the component in pixels. The size of many components can be set
this way, and most also have setMinimumSize and setMaximumSize methods to help control the look of the interface.

The labels are created by instantiating the JLabel class, passing to its constructor the text of the label. In this program two separate label components are created.

Containers have an add method that allows other components to be added to them. Both labels are added to the primary panel and are from that point on considered to be part of that panel. The order in which components are added to a container often matters. In this case, it determines which label appears above the other.

Finally, the content pane of the frame is obtained using the getContentPane method, immediately after which the add method of the content pane is called to add the panel. The pack method of the frame sets its size appropriately based on its contents—in this case the frame is sized to accommodate the size of the panel it contains. This is a better approach than trying to set the size of the frame explicitly, which should change as the components within the frame change. The call to the setVisible method causes the frame to be displayed on the monitor screen.

The Authority program is not interactive. In general, labels do not allow the user to interact with a program.

However, you can interact with the frame itself in various ways. You can move the entire frame to another point on the desktop by grabbing the title bar of the frame and dragging it with the mouse. You can also resize the frame by dragging the bottom-right corner of the frame. Note what happens when the frame is made wider: the second label pops up next to the first label. Every container is managed by an object called a layout manager that determines how the components in the container are laid out. The layout manager is consulted when important things happen to the interface, such as when the frame is resized.

Unless you specify otherwise, the components in a panel will try to arrange themselves next to one another in a row, and a component will move down to the next row only when the width of the panel won’t accommodate it. Experiment with this program to see how the layout manager changes the organization of
the labels as the window size is changed.


No comments :

Post a Comment

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact