📄 booktitlemidlet.java
字号:
// Fig. 14.8: BookTitleMIDlet.java.// BookTitleMIDlet invokes the Book Titles Web service from a// servlet to receive a list of book titles.package com.deitel.jws1.j2me.client;// import J2ME packagesimport javax.microedition.midlet.*;import javax.microedition.lcdui.*;// import Deitel packagesimport com.deitel.jws1.services.BookTitles;public class BookTitleMIDlet extends MIDlet { private Display display; // display manager private List mainScreen; // list of available Web services private final static int SERVLET = 0; private final static int ENHYDRA = 1; public BookTitleMIDlet() { // create main screen with Web-service list mainScreen = new List( "Web services", List.IMPLICIT ); mainScreen.append( "Get Book Titles", null ); // create soft button commands Command selectCommand = new Command( "Select", Command.OK, 0 ); mainScreen.addCommand( selectCommand ); // allow soft button access for mainScreen mainScreen.setCommandListener( new CommandListener() { // invoked when user presses soft button public void commandAction( Command command, Displayable displayable ) { display.setCurrent( createServiceAccessForm() ); } } ); // get appropriate Display manager for MIDP device display = Display.getDisplay( this ); } // start MIDlet and enable user input public void startApp() { // set display to main Screen display.setCurrent( mainScreen ); } // pause MIDlet public void pauseApp() {} // terminate MIDlet lifecycle public void destroyApp( boolean unconditional ) {} // create Form for selecting how to access Web service private Screen createServiceAccessForm() { Form accessScreen = new Form( "Invoke Web service via:" ); // ChoiceGroup that enables user to specify means of access final ChoiceGroup choices = new ChoiceGroup( "", ChoiceGroup.EXCLUSIVE ); choices.append( "Servlet", null ); choices.append( "Enhydra", null ); // append ChoiceGroup to Form accessScreen.append( choices ); // create soft button commands Command invokeCommand = new Command( "Invoke", Command.OK, 0 ); accessScreen.addCommand( invokeCommand ); Command backCommand = new Command( "Back", Command.BACK, 1 ); accessScreen.addCommand( backCommand ); // allow soft button access for this Screen accessScreen.setCommandListener( new CommandListener() { // invoked when user presses soft button public void commandAction( Command command, Displayable displayable ) { // invoke service if user presses SELECT button if ( command.getCommandType() == Command.OK ) { BookTitles service = null; // determine means of Web-service access int selection = choices.getSelectedIndex(); if ( selection == SERVLET ) service = new BookTitleServiceViaServlets(); else if ( selection == ENHYDRA ) service = new BookTitleServiceViaEnhydra(); String titles[] = null; // invoke Web service through interface if ( service != null ) titles = service.getBookTitles(); // display book titles in List if ( titles != null ) { List titlesScreen = new List( "Book Titles", List.IMPLICIT ); // store book titles in List for ( int i = 0; i < titles.length; i++ ) titlesScreen.append( titles[ i ], null ); // display List of book titles display.setCurrent( titlesScreen ); } // display error page for 3 seconds if error 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 button else if ( command.getCommandType() == Command.BACK ) display.setCurrent( mainScreen ); } // end method commandAction } // end anonymous inner class ); return accessScreen; } // end method createServiceAccessForm} // end class BookTitleMIDlet
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -