Java Reference
In-Depth Information
Checkbox is selected, then the user has not chosen one of the visible option
buttons on the interface, thus causing a message box to display a message
prompting the user to select nonsmoking or smoking (line 106).
102
public void actionPerformed ( ActionEvent e )
103
{
104
if ( hidden.getState ())
105
{
106
JOptionPane.showMessageDialog ( null , "You must select Nonsmoking or Smoking." ,
"Error" ,JOptionPane.ERROR_MESSAGE ) ;
107
}
108
else
109
{
110
int available = room.bookRoom ( smoking.getState ()) ;
111
112
if ( available > 0 ) //room is available
113
{
114
roomDisplay [ available ] .setBackground ( lightRed ) ; //display room as occupied
115
roomDisplay [ available ] .setText (
116
roomDisplay [ available ] .getText () +
117
"\n" +
118
nameField.getText () +
119
" " +
120
phoneField.getText () +
121
"\nparty of " +
122
numberOfGuests.getSelectedItem ()
123
) ; //display info in room
124
clearFields () ;
125
}
126
else //room is not available
127
{
128
if ( smoking.getState ())
129
JOptionPane.showMessageDialog ( null , "Smoking is full." , "Error" ,
JOptionPane.INFORMATION_MESSAGE ) ;
130
else
131
JOptionPane.showMessageDialog ( null , "Nonsmoking is full." , "Error" ,
JOptionPane.INFORMATION_MESSAGE ) ;
132
hidden.setState ( true ) ;
133
} //end of else block that checks the available room number
134
} //end of else block that checks the state of the hidden option button
135
} //end of actionPerformed() method
FIGURE 5-33
After the bookRoom() method is called in line 110, the program evaluates
the value of available, which represents the returned room number. If the value
of available is greater than zero, the program sets the background color of the
TextArea component that matches the room number (line 114). Lines 115
through 123 are one long statement that concatenates the user input data of
customer's name, phone number, and number of guests in the party, then sets
that String to display in the TextArea component.
Improving Readability in Long Lines of Code
In TextPad, you may press the ENTER key to continue lines at
certain locations, such as after operators, commas, or parentheses;
Java ignores the extra white space. Breaking long statements into
multiple lines improves readability. When you press the ENTER key,
TextPad inserts a new line number. If a long line wraps, TextPad
does not insert a new line number.
Search WWH ::




Custom Search