📄 bookpricescreen.java
字号:
// Fig. 16.19: BookPriceScreen.java
// BookPriceScreen is a MIDlet screen that invokes the Best Book
// Price Web service and displays its results.
package jws1casestudy.clients.j2me;
// import J2ME packages
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
class BookPriceScreen extends Form {
// create Form for specifying ISBN of book to obtain price
BookPriceScreen( final Display display )
{
super( "Select book to obtain price" );
// ChoiceGroup that enables user to specify book ISBN
final ChoiceGroup choices =
new ChoiceGroup( "", ChoiceGroup.EXCLUSIVE );
choices.append( "0130895725", null );
choices.append( "0130284181", null );
choices.append( "0130341517", null );
choices.append( "0130293636", null );
choices.append( "0130895601", null );
append( choices );
// create soft button commands
Command invokeCommand =
new Command( "Select", Command.OK, 0 );
addCommand( invokeCommand );
Command backCommand =
new Command( "Back", Command.BACK, 1 );
addCommand( backCommand );
// allow soft button access
setCommandListener(
new CommandListener() {
// invoked when user presses soft button
public void commandAction(
Command command, Displayable displayable )
{
// invoke service if user presses Select
if ( command.getCommandType() == Command.OK ) {
BookPriceService priceService =
new BookPriceService();
// invoke Web service through interface
int selection = choices.getSelectedIndex();
String isbn = choices.getString( selection );
// invoke Best Book Price Web service
PriceQuote quote =
priceService.getBestPrice( isbn );
// display PriceQuote results on success
if ( quote != null )
display.setCurrent( new ResultScreen(
quote, display ) );
// display error page on failure
else
{
Alert errorPage = new Alert( "Error",
"Cannot invoke Web Service", null,
AlertType.ERROR );
errorPage.setTimeout( 3000 );
display.setCurrent( errorPage );
}
}
// display mainScreen if user presses Back
else if ( command.getCommandType() ==
Command.BACK )
display.setCurrent( new MainScreen( display ) );
} // end method commandAction
} // end anonymous inner class
);
} // end BookPriceScreen constructor
// create Form to display book information
private class ResultScreen extends Form {
private PriceQuote priceQuote;
ResultScreen( PriceQuote quote, final Display display )
{
super( "Best price" );
priceQuote = quote;
// include book title on Form
append( new StringItem( "", "Price: " +
priceQuote.getPrice() + '\n' ) );
// include book ISBN on Form
append( new StringItem( "", "ISBN: " +
priceQuote.getIsbn() + '\n' ) );
// include book authors on Form
append( new StringItem( "",
"Store Description: " +
priceQuote.getStoreDescription() ) );
// create soft button commands
Command okCommand =
new Command( "Order", Command.OK, 0 );
addCommand( okCommand );
// create soft button commands
Command cancelCommand =
new Command( "Cancel", Command.CANCEL, 1 );
addCommand( cancelCommand );
// allow soft button access for this Screen
setCommandListener(
new CommandListener() {
// invoked when user presses soft button
public void commandAction(
Command command, Displayable displayable )
{
Screen displayScreen = null;
// display order screen if user presses Order
if ( command.getCommandType() == Command.OK ) {
displayScreen = new BookPurchaseScreen(
priceQuote, display );
}
// display mainScreen if user presses Cancel
else if ( command.getCommandType() ==
Command.CANCEL )
displayScreen = new MainScreen( display );
display.setCurrent( displayScreen );
}
}
);
} // end ResultScreen constructor
} // end class ResultScreen
} // end class BookPriceScreen
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -