Java Reference
In-Depth Information
Don't forget that the JVM often does the conversion from primitive to string automatically (for instance, when
displaying primitives with the println method).
Table 5-4 summarizes the conversion methods between primitive types and strings and gives an example of each.
Table 5-4.
From
To
How
Example
doubleTest = intTest ;
Smaller
Primitive
Larger
Primitive
Assignment
intTest = (int) doubleTest ;
Larger
Primitive
Smaller
Primitive
Casting
intString = String.valueOf(intTest);
intString = Integer.toString(intTest);
Primitive
String
String.valueOf( )
Wrapper .toString( )
intTest = Integer.parseInt(intString);
String
Primitive
Wrapper .parse Xxx ( )
TextField
Sorry for all this pain, but the parse method will be required to retrieve and manipulate numeric information from
a GUI component. Speaking of which, our next new GUI component is a text field. A text field looks like a rectangle.
Users can enter information into a text field and, like a label, information can also be displayed in a text field. You can
probably guess the syntax for creating a TextField object, but here it is anyway:
TextField empNameTF = new TextField();
Of course, the text field must be sized and placed on the frame (like our other GUI components) and we will need
to retrieve the text from the text field. Like the other GUI components, TextField objects have getters and setters
for manipulating properties. One of a text field's properties is its text. The getText method returns the contents of
the text field as characters, and the setText method places text in the text field. We will use these methods to modify
EmployeeApp so that it can accept input from a user.
Until now, EmployeeApp has simply displayed “hard-coded” employee information. We will modify the
application to accept employee information using a new frame called EnterEmpInfo. We will also perform a gross
salary calculation and display the results.
Truly, the EnterEmpInfo frame is not the correct place for the salary calculation method. EnterEmpInfo should
only have methods that pertain to entering employee information. Calculating an employee's gross salary really
belongs in the Employee class. For right now, it is simply easier to put it in the frame. Later we will move it to Employee.
Tutorial: Inputting Data
Let's do a little data input:
1.
In c5, create a visual class called EnterEmpInfo that is a subclass of Frame, implements an
action listener, and has a main method.
2.
In the source code, change the superclass from Frame to UsefulFrame.
 
Search WWH ::




Custom Search