📄 bookpurchasescreen.java
字号:
// Fig. 16.22: BookPurchaseScreen.java
// BookPurchaseScreen is a MIDlet screen that invokes the Book
// Purchase Web service and displays its results.
package jws1casestudy.clients.j2me;
// import J2ME packages
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
// import Deitel packages
import jws1casestudy.pricefinder.common.Customer;
class BookPurchaseScreen extends Form {
PriceQuote priceQuote;
// create Form for specifying customer information
BookPurchaseScreen( PriceQuote quote, final Display display )
{
super( "Customer info" );
priceQuote = quote;
// create TextField to enter first name
final TextField firstNameField = new TextField(
"First name:", "Jon", 15, TextField.ANY );
// create TextField to enter last name
final TextField lastNameField = new TextField(
"Last name:", "Smith", 15, TextField.ANY );
// create TextField to enter email address
final TextField emailField = new TextField(
"Email Address:", "yourName@yourAddress.com", 32,
TextField.EMAILADDR );
// create TextField to enter street address
final TextField streetField = new TextField(
"Street Address:", "12 Clock Tower Place", 40,
TextField.ANY );
// create TextField to enter city
final TextField cityField = new TextField(
"City:", "Maynard", 15, TextField.ANY );
// create TextField to enter state
final TextField stateField = new TextField(
"State:", "MA", 10, TextField.ANY );
// create TextField to enter zip code
final TextField zipCodeField = new TextField(
"Zip code:", "01754", 15, TextField.ANY );
// create TextField to enter country
final TextField countryField = new TextField(
"Country:", "USA", 15, TextField.ANY );
// create TextField to enter credit-card number
final TextField creditCardField = new TextField(
"Credit card number:", "XXXXXXXXXXXX1234", 24,
TextField.ANY );
// append TextFields to form
append( firstNameField );
append( lastNameField );
append( emailField );
append( streetField );
append( cityField );
append( stateField );
append( zipCodeField );
append( countryField );
append( creditCardField );
// create soft button commands
Command okCommand = new Command( "OK", Command.OK, 0 );
addCommand( okCommand );
Command cancelCommand =
new Command( "Cancel", Command.CANCEL, 0 );
addCommand( cancelCommand );
// allow soft button access
setCommandListener(
new CommandListener() {
// invoked when user presses soft button
public void commandAction(
Command command, Displayable displayable )
{
// invoke Book Purchase service if user presses OK
if ( command.getCommandType() == Command.OK ) {
BookPurchaseService purchaseService =
new BookPurchaseService();
// create Customer from TextField info
Customer customer = new Customer();
customer.setFirstName(
firstNameField.getString() );
customer.setLastName(
lastNameField.getString() );
customer.setEmailAddress(
emailField.getString() );
customer.setStreetAddress(
streetField.getString() );
customer.setCity( cityField.getString() );
customer.setState( stateField.getString() );
customer.setZipCode( zipCodeField.getString() );
customer.setCountry( countryField.getString() );
customer.setCreditCardNumber(
creditCardField.getString() );
// invoke Book Purchase Web service
purchaseService.placeOrder( priceQuote,
customer );
// display results
display.setCurrent( new ResultScreen(
display ) );
}
// display main screen if user presses Cancel
else if ( command.getCommandType() ==
Command.CANCEL ) {
display.setCurrent( new MainScreen( display ) );
}
}
}
);
} // end BookPurchaseScreen constructor
// create Form to display book information
private class ResultScreen extends Form {
ResultScreen( final Display display )
{
super( "" );
append( new StringItem( "", "** Book Ordered **\n" ) );
append( new StringItem( "",
"Confirmation will be sent via email" ) );
// create soft button commands
Command okCommand = new Command( "OK", Command.OK, 0 );
addCommand( okCommand );
// allow soft button access for this Screen
setCommandListener(
new CommandListener() {
// invoked when user presses soft button
public void commandAction(
Command command, Displayable displayable )
{
// display main screen when user presses button
display.setCurrent( new MainScreen( display ) );
}
}
);
} // end ResultScreen constructor
} // end class ResultScreen
} // end class BookPurchaseScreen
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -