📄 bookinformationscreen.java
字号:
// Fig. 16.17: BookInformationScreen.java
// BookInformationScreen is a MIDlet screen that accesses and
// displays the results of the BookDetails Web service.
package jws1casestudy.clients.j2me;
// import J2ME packages
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
// import Deitel packages
import jws1casestudy.pricefinder.common.BookDetails;
class BookInformationScreen extends Form {
// Form that selects a book on which to obtain information
BookInformationScreen( final Display display )
{
super( "Select book to obtain info" );
// ChoiceGroup that enables user to choose book
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 ) {
// create object to invoke Web service
BookInformationService informationService =
new BookInformationService();
// obtain isbn of book, based on user selection
int selection = choices.getSelectedIndex();
String isbn = choices.getString( selection );
// invoke Book Details Web service
BookDetails bookDetails =
informationService.getBookDetails( isbn );
// display book details on success
if ( bookDetails != null )
display.setCurrent( new ResultScreen(
bookDetails, 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 BookInformationScreen constructor
// Form to display book details after Web-service invocation
private class ResultScreen extends Form {
private BookDetails bookDetails;
ResultScreen( BookDetails info, final Display display )
{
super( "Book Info" );
bookDetails = info;
// include book title on Form
append( new StringItem( "", "Title: " +
bookDetails.getTitle() + '\n' ) );
// include book ISBN on Form
append( new StringItem( "", "ISBN: " +
bookDetails.getIsbn() + '\n' ) );
// include book authors on Form
append( new StringItem( "", "Author: " +
bookDetails.getAuthors() + '\n' ) );
// include book description on Form
append( new StringItem( "", "Description: " +
bookDetails.getDescription() + '\n' ) );
// create soft button command to view MainScreen
Command okCommand = new Command( "OK", Command.OK, 0 );
addCommand( okCommand );
// allow soft button access
setCommandListener(
new CommandListener() {
// view MainScreen when user presses OK
public void commandAction(
Command command, Displayable displayable )
{
display.setCurrent( new MainScreen( display ) );
}
}
);
} // end ResultSet constructor
} // end class ResultScreen
} // end class BookInformationScreen
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -