This is very similar to the second program, but it gets input from the user. The new statements are described below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// ThirdProgram.java
// Michael Maus, 25 August 2004
// This program gets a string from a dialog box.
import javax.swing.*;
public class ThirdProgram {
public static void main(String[] args) {
String name; // A local variable to hold the name.
name = JOptionPane.showInputDialog(null, "What's your name, Earthling");
JOptionPane.showMessageDialog(null, "Take me to your leader, " + name);
System.exit(0); // Stop GUI program
}
}
|
![]() |
Another type of dialog box that JOptionPane
will show can get input from the user. Call the showInputDialog
method for this. It will return a string that can be stored into
a variable. Here we save it in the variable name.
|