Java Reference
In-Depth Information
Java is equipped with many powerful, yet easy-to-use graphical user interface (GUI)
components, such as the input and output dialog boxes you learned about in Chapter 3.
You can use these to make your programs attractive and user-friendly. The first half of this
chapter introduces you to some basic Java GUI components. Chapter 12 covers GUI in
some detail.
In Chapter 1, you were introduced to the object-oriented design (OOD) problem-
solving methodology. The second half of this chapter outlines a general approach to solving
problems using OOD, and provides several examples to clarify this problem-solving
methodology.
Graphical User Interface (GUI) Components
In Chapter 3, you learned how to use input and output dialog boxes to input data into a
program and show the output of a program. Before introducing the various GUI
components, we will use input and output dialog boxes to write a program to determine
the area and perimeter of a rectangle. We will then discuss how to use additional GUI
components to create a different graphical user interface to determine the area and
perimeter of a rectangle.
The program in Example 6-1 prompts the user to input the length and width of a
rectangle and then displays its area and perimeter. We will use the method
showInputDialog to create an input dialog box and the method
showMessageDialog to create an output dialog box. Recall that these methods are
contained in the class JOptionPane and this class
is contained in the package
javax.swing .
EXAMPLE 6-1
// This Java Program determines the area and
// perimeter of a rectangle.
import javax.swing.JOptionPane;
public class Rectangle
{
public static void main(String[] args)
{
double width, length, area, perimeter;
//Line 1
String lengthStr, widthStr, outputStr;
//Line 2
lengthStr =
JOptionPane.showInputDialog("Enter the length: "); //Line 3
length = Double.parseDouble(lengthStr);
//Line 4
 
Search WWH ::




Custom Search