Java Reference
In-Depth Information
17
"You'll have " + (money * 32) +
18
"dollars at age " + (age + 5) + "!");
19 }
20 }
The Integer.parseInt and Double.parseDouble methods throw exceptions of
type NumberFormatException if you pass them String s that cannot be converted into
valid numbers, such as "abc" , "five" , or "2 × 2" . To make your code robust against such
invalid input, you can enclose the code in a try/catch statement such as the following:
int age;
try {
age = Integer.parseInt(ageText);
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(null, "Invalid integer.");
}
Table 14.2 summarizes the static methods in the wrapper classes that can be used
to convert String s.
Working with Frames
JOptionPane is useful, but on its own it is not flexible or powerful enough to create
rich graphical user interfaces. To do that, you'll need to learn about the various types
of widgets, or components, that can be placed on the screen in Java.
An onscreen window is called a frame.
Frame
A graphical window on the screen.
The graphical widgets inside a frame, such as buttons or text input fields, are col-
lectively called components.
Component
A widget, such as a button or text field, that resides inside a graphical window.
Table 14.2
Useful Methods of Wrapper Classes
Method
Description
Returns the integer represented by the given String as
an int
Integer.parseInt(str)
Returns the real number represented by the given
String as a double
Double.parseDouble(str)
Returns the boolean value represented by the given
String (if the text is "true" , returns true ; otherwise,
returns false ).
Boolean.parseBoolean(str)
 
Search WWH ::




Custom Search