⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tiptestmidlet.java

📁 高级java2 大学教程(含源码,经典的Java学习教程
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// TipTestMIDlet.java
// Receives TipTest from Servlet
package com.deitel.advjhtp1.wireless;

// J2ME Java package subset
import java.io.*;

// J2ME packages
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;

public class TipTestMIDlet extends MIDlet {

   private Display display; // display manager

   // Screens displayed to user
   private List mainScreen;
   private List welcomeScreen;
   private Form infoScreen;
   private Form tipScreen;
   private Form answerScreen;

   // actions for soft-buttons
   private Command selectCommand;
   private Command nextCommand;
   private Command backCommand;

   private static final String servletBaseURL = 
      "http://localhost:8080/advjhtp1/";

   private static final String welcomeServletName = "welcome";

   // welcome servlet determines tip test servlet name
   private String tipTestServletName;

   private static final String welcomeServletURL =
      servletBaseURL + welcomeServletName;

   private String sessionID;

   // constructor initializes display and main Screen
   public TipTestMIDlet()
   {
      // create soft button commands
      selectCommand = new Command( "Select", Command.OK, 0 );
      nextCommand = new Command( "Next Tip", Command.OK, 0 );
      backCommand = new Command( "Back", Command.BACK, 1 );

      // create main screen allowing welcome servlet connection
      mainScreen = new List( "TipTestMIDlet", List.IMPLICIT );
      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 )
            {
               // get data from welcome servlet
               String data = getServerData( welcomeServletURL );

               // create welcome Screen using servlet data
               display.setCurrent( createWelcomeScreen( data ) );
            }

         } // end anonymous inner class
      );

      // get appropriate Display for device
      display = Display.getDisplay( this );

   } // end TipTestMIDlet constructor

   // start MIDlet
   public void startApp()
   {
      // set display to main Screen
      display.setCurrent( mainScreen );
   }

   // pause MIDlet
   public void pauseApp() {}

   // destroy MIDlet
   public void destroyApp( boolean unconditional ) {}

   // create "welcome" Screen introducing tip test
   private Screen createWelcomeScreen( String data )
   {
      String list[] = parseData( data, ';' );

      // create Screen welcoming user to tip test
      welcomeScreen = new List( list[ 0 ], List.IMPLICIT );

      welcomeScreen.append( "Take TipTest", null );
      welcomeScreen.addCommand( selectCommand );
      welcomeScreen.addCommand( backCommand );

      // get URL of information page
      final String url = new String( list[ 1 ].toCharArray() );

      // allow soft button access for welcomeScreen
      welcomeScreen.setCommandListener(
         new CommandListener() {

            // invoked when user presses soft button
            public void commandAction( 
               Command command, Displayable displayable )
            {
               // soft button pressed is SELECT button
               if ( command.getCommandType() == Command.OK ) {

                  // get data from static page
                  String data = 
                     getServerData( servletBaseURL + url );

                  // display this data
                  display.setCurrent( 
                     createInformationScreen( data ) );
               }

               // soft button pressed is BACK button
               else if ( command.getCommandType() == 
                  Command.BACK ) {
                  display.setCurrent( mainScreen );
               }

            } // end method commandAction

         } // end anonymous inner class
      );

      return welcomeScreen;

   } // end method createWelcomeScreen

   // create Screen showing servlets to which client can connect
   private Screen createInformationScreen( String data )
   {
      String list[] = parseData( data, ';' );

      // create Form showing available servlets
      infoScreen = new Form( "Information" );

      // create StringItem that provides directions
      StringItem infoTitle = new StringItem( list[ 0 ], null );
      infoScreen.append( infoTitle );

      // create ChoiceGroup allowing user to select servlet
      final ChoiceGroup choices = new ChoiceGroup( "", 
         ChoiceGroup.EXCLUSIVE );
      choices.append( list[ 1 ], null );

      // append ChoiceGroup to Form
      infoScreen.append( choices );

      infoScreen.addCommand( selectCommand );
      infoScreen.addCommand( backCommand );

      // allow soft button access for this Screen
      infoScreen.setCommandListener(
         new CommandListener() {

            // invoked when user presses soft button
            public void commandAction( 
               Command command, Displayable displayable )
            {
               // soft button pressed is SELECT button
               if ( command.getCommandType() == Command.OK ) {

                  // user chooses which servlet to connect
                  int selectedIndex = choices.getSelectedIndex();

                  tipTestServletName = 
                     choices.getString( selectedIndex );

                  // connect to servlet and receive data
                  String data = getServerData( servletBaseURL + 
                     tipTestServletName );

                  // display next Screen according to data
                  display.setCurrent( createTipTestScreen( 
                     servletBaseURL + data ) );
               }

               // soft button pressed is BACK button
               else if ( command.getCommandType() == Command.BACK )
                  display.setCurrent( welcomeScreen );

            } // end method commandAction

         } // end anonymous inner class
      );

      return infoScreen;

   } // end method createInformationScreen

   // create Screen to display Tip Test
   private Screen createTipTestScreen( String data )
   {
      // parse server data
      String list[] = parseData( data, '\n' );

      // create new Form to display test
      tipScreen = new Form( "Tip Test" );

      // create image from server data
      Image serverImage = getServerImage( list[ 0 ] );

      // append image to Form
      if ( serverImage != null )
         tipScreen.append( serverImage );

      String choiceList[] = new String[ 4 ];

      // construct list for ChoiceGroup from data
      for ( int i = 0; i < choiceList.length; i++ )
         choiceList[ i ] = list[ i + 1 ];

      // create ChoiceGroup allowing user to input choice
      final ChoiceGroup choices = new ChoiceGroup( "Tip Test", 
         ChoiceGroup.EXCLUSIVE, choiceList, null );

      // append ChoiceGroup to Form
      tipScreen.append( choices );

      tipScreen.addCommand( selectCommand );

      // allow soft button access for this Screen
      tipScreen.setCommandListener(
         new CommandListener() {

            // invoked when user presses soft button
            public void commandAction( 
               Command command, Displayable displayable )
            {
               // send user selection to servlet
               int selection = choices.getSelectedIndex();

               String result = postData( selection );

               // display results
               display.setCurrent( 
                  createAnswerScreen( result ) );
            }

         } // end anonymous inner class
      );

      return tipScreen;

   } // end method createTipTestScreen

   // create Screen to display Tip Test answer and results
   private Screen createAnswerScreen( String data )
   {
      // parse server data
      String list[] = parseData( data, '\n' );

      // create new Form to display test answers
      answerScreen = new Form( list[ 0 ] );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -