Professional Documents
Culture Documents
Interface (GUI)
• Learn about Java basic GUI components
• Explore how the GUI components JFrame,
JLabel, JTextField and JButton
work
• Layout different components in a window
application
• Design and run an interactive window
application in Java
import javax.swing.JOptionPane;
public class RectangleGUI {
public static void main(String[] args) {
double width, length, area, perimeter;
String widthStr, lengthStr, outputStr;
lengthStr = JOptionPane.showInputDialog(null, "Enter Length: ");
length = Double.parseDouble(lengthStr);
JOptionPane.showMessageDialog(null, outputStr);
System.exit(0);
}
}
JOptionPane Class
JOptionPane is a standard, predefined class
that comes with every installation of Java used for
producing special windows—called dialog
windows, dialog boxes, or simply dialogs—that
either obtain input or display output from your
program. To make it available to your program,
you write
import javax.swing.JOptionPane;
showInputDialog() method
The method showInputDialog produces a
dialog for obtaining input.
lengthStr = JOptionPane.showInputDialog
(null, "Enter Length: ");
length = Double.parseDouble(lengthStr);
widthStr = JOptionPane.showInputDialog
(null. "Enter Width: ");
width = Double.parseDouble(widthStr);
showInputDialog() method
Syntax:
String_Variable = JOptionPane.showInputDialog
(null, String_Expression);
showMessageDialog() method
• This method displays a dialog window that shows
some output.
outputStr = "Length: " + length + "\n" +
"Width: " + width + "\n" +
"Area: " + area + "sq. units \n" +
"Perimeter: " + perimeter + "units \n";
JOptionPane.showMessageDialog(null, outputStr);
showMessageDialog() method
Syntax:
JOptionPane.showMessageDialog(null,
String_Expression);
ICONS Used By JOptionPane
plain message
//default title and icon
JOptionPane.showMessageDialog(null, "Eggs
aren’t supposed to be green.");
//custom title, warning icon
JOptionPane.showMessageDialog(null, "Eggs
aren’t supposed to be green.", "Inane
warning", JOptionPane.WARNING_MESSAGE);
//custom title, error icon
JOptionPane.showMessageDialog(null, "Eggs
aren’t supposed to be green.", "Inane
error", JOptionPane.ERROR_MESSAGE);
//custom title, custom icon
JOptionPane.showMessageDialog(null, "Eggs
aren’t supposed to be green.", "Inane
custom dialog",
JOptionPane.INFORMATION_MESSAGE, icon);
//custom title, no icon
JOptionPane.showMessageDialog(null, "Eggs
aren’t supposed to be green.", "A plain
message", JOptionPane.PLAIN_MESSAGE);
System.exit(0) statement
Required for an application program that uses a
windowing interface
Really ends the program’s execution.
JOptionPane.showMessageDialog(null,
outputStr);
System.exit(0);
showConfirmDialog() method
A confirmation dialog box gives buttons to select,
and when a user clicks one of the buttons, it
returns an integer value.
int selection;
do {
// play one Tic Tac Toe game
selection = JOptionPane.showConfirmDialog(null, //#1
"Would you like to play another Tic Tac Toe game?", //#2
"Confirmation", //#3
JOptionPane.YES_NO_OPTION); //#4
} while(selection == JOptionPane.YES_OPTION);
showConfirmDialog() method
int selection;
do {
// play one Tic Tac Toe game
selection = JOptionPane.showConfirmDialog(null, //#1
"Would you like to play another Tic Tac Toe game?", //#2
"Confirmation", //#3
JOptionPane.YES_NO_OPTION); //#4
} while(selection == JOptionPane.YES_OPTION);
.YES_NO_OPTION
.YES_NO_CANCEL_OPTION
.OK_CANCEL_OPTION
//default icon, custom title
int n =
JOptionPane.showConfirmDialog(
null, "Would you like green
eggs and ham?", "An Inane
Question",
JOptionPane.YES_NO_OPTION);