Java Reference
In-Depth Information
4.
Change the println statement so that doubleGross is printed, not empPayRate.
5.
Save the source and run EnterEmpInfo as a Java application.
6.
Enter 6.50 in the Pay Rate text field and click the Gross button.
The value 260.0 should be displayed in the console. Notice that you (the programmer) have to explicitly convert
the text to a double (i.e., define a double variable, parse the text, and assign it to the double variable). However, to
display the double value as text, the println statement automatically converts the double to a string.
Now, the console is not the best way to display information to a user. We should create a new label on the frame
to display the results.
Tutorial: Adding a Result Label
We will create a new label called resultLbl that is:
250 pixels in length
Centered horizontally on the frame
Vertically 10 pixels above the Gross button
Not initially added to the frame
and when the Gross button is clicked:
The label is added to the frame
The label text shows the value of doubleGross as follows:
The Gross Salary is: $260.00
Let's get started:
1.
In the design pane, add a label and set its name to resultLbl.
Notice that a resultLbl variable has been added and that an add statement was inserted in the initialize method.
2.
In the Properties view, define the label's length as 250, location as 75, 332, and alignment
as CENTER.
3.
In the initialize method, cut out the add label statement (that puts the resultLbl on the
frame) and paste it into the actionPerformed method (after the calculation is performed).
This means that the label is added to the frame after the Gross button is clicked. Notice in the design pane that
resultLbl now appears outside of the frame. This means the component is part of the class but initially is not on
the frame.
4.
In the actionPerformed method after the add label statement, add the following setText statement:
resultLbl.setText("Gross Salary is: $" +
doubleGross + "0");
This statement defines the resultLbl's text as “Gross Salary is: $”, concatenated to doubleGross, concatenated to
the character zero (0).
5.
Save the source and run EnterEmpInfo as a Java application.
6.
Enter 6.5 in the Pay Rate text field and click the Gross button.
The frame should look like Figure 5-11 .
 
Search WWH ::




Custom Search