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

📄 midletbrowser.java

📁 这是我在测试手机java时候做的java菜单程序
💻 JAVA
字号:
/*==============================================================================

FILE:  MidletBrowser.java

GENERAL DESCRIPTION:
   MIDlet to browse MIDlets in external JAD/JAR files.

Copyright (c) 2002 Qualcomm Inc.  All rights reserved.
==============================================================================*/

package com.qualcomm.demo.menu;

import java.lang.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
import javax.microedition.io.Connector;
import com.sun.midp.io.j2me.storage.RandomAccessStream;
import com.qualcomm.midp.Main;
import com.qualcomm.qjaeservices.file.FileService;

/**
 * MidletBrowser MIDlet.
 */
public class MidletBrowser extends MIDlet implements CommandListener
{
   // Copyright text to display when "About" softkey pressed
   private final String aboutText =
      "Copyright 2002 Qualcomm Inc.\n" +
      "All Rights Reserved.\n\n" +
      "Memory Size: " + Runtime.getRuntime().totalMemory();

   // Default directory to find MIDlets in
   private final String EXTERNAL_MIDLETS_ROOT_DIR = "";

   // Display for this MIDlet
   private Display display;

   // Softkeys for "main menu" and "info" screens
   private final Command backCommand =
      new Command("Back", Command.BACK, 1);
   private final Command infoCommand =
      new Command("Info", Command.ITEM, 2);
   private final Command aboutCommand =
      new Command("About", Command.HELP, 3);
   private final Command runCommand =
      new Command("Run", Command.SCREEN, 4);
   private final Command exitCommand =
      new Command("Exit", Command.EXIT, 5);

   // "Main menu" screen
   private List menuScreen;

   // "Info" screen
   private Form infoScreen;

   // "About" screen
   private final Alert aboutScreen =
      new Alert("About MidletBrowser", aboutText, null, null);

   // Directory (full pathname) of current MIDlet menu
   private String currentDir;

   // Subdirectories displayed on menu (names only; not full path)
   private String[] subdirs;

   // MIDlets displayed on menu
   private MidletInfo[] midlets;

   // Currently selected subdirectory (null if MIDlet)
   private String selectedSubdir;

   // Currently selected MIDlet (null if subdirectory)
   private MidletInfo selectedMidlet;

   // Displays the menu for the current directory
   private void displayMenu()
   {
      int i;

      // Create new List screen to display menu on
      menuScreen = new List("MIDlets at " + currentDir + "/", List.IMPLICIT);

      // Load subdirectory info and append data to menu
      loadSubdirInfo();
      for (i = 0; i < subdirs.length; i++)
      {
         menuScreen.append(subdirs[i], MidletBrowserIcons.folderIcon);
      }

      // Load MIDlet info and append data to menu
      loadMidletInfo();
      for (i = 0; i < midlets.length; i++)
      {
         menuScreen.append(midlets[i].name, midlets[i].icon);
      }

      // Configure softkeys
      menuScreen.addCommand(backCommand);
      menuScreen.addCommand(infoCommand);
      menuScreen.addCommand(aboutCommand);
      menuScreen.addCommand(exitCommand);
      menuScreen.setCommandListener(this);

      // Display screen
      display.setCurrent(menuScreen);
   }

   // Displays the info for the current MIDlet
   private void displayInfo()
   {
      if (selectedSubdir != null)
      {
         // Create new Form screen to display subdirectory info on
         infoScreen = new Form(selectedSubdir + " information");

         // Append information
         infoScreen.append(new StringItem("Object type:", "Folder"));
      }
      else
      {
         // Create new Form screen to display MIDlet info on
         infoScreen = new Form(selectedMidlet.name + " information");

         // Append information
         infoScreen.append(new StringItem("Object type:", "MIDlet"));
         infoScreen.append(new StringItem("JAD file name:",
                                          selectedMidlet.jadFile));
         infoScreen.append(new StringItem("JAD file contents:",
                                          "\n" + selectedMidlet.jadData));

         // Add MIDlet-specific softkeys
         infoScreen.addCommand(runCommand);
      }

      // Add softkeys applicable to both subdirs and MIDlets, and configure
      infoScreen.addCommand(backCommand);
      infoScreen.setCommandListener(this);

      // Display screen
      display.setCurrent(infoScreen);
   }

   // Loads subdirectory names from currentDir
   private void loadSubdirInfo()
   {
      subdirs = FileService.getDirs(currentDir);
   }

   // Loads MIDlet info from JAD files in currentDir
   private void loadMidletInfo()
   {
      String[] jadFiles = FileService.getFilesWithExtension(currentDir, "jad");
      String[] jadData = new String[jadFiles.length];
      int[] jadMidletCount = new int[jadFiles.length];

      int i, count = 0;

      // Loop through all JAD files
      for (i = 0; i < jadFiles.length; i++)
      {
         RandomAccessStream stream = new RandomAccessStream();
         byte[] data = null;

         // Load JAD file contents
         try
         {
            stream.connect(currentDirAbsolute() + "/" + jadFiles[i],
                           Connector.READ);
            data = new byte[stream.getSizeOf()];
            stream.readBytes(data, 0, data.length);
            stream.disconnect();
         }
         catch (java.io.IOException e)
         {
         }

         if (data != null)
         {
            // Convert any Windows-style "\r\n" to plain "\n"
            // (This just improves the appearance when the data is displayed.)
            int len = data.length;
            int n;
            for (n = 0; n < (len - 1); n++)
            {
               if ((data[n] == '\r') && (data[n+1] == '\n'))
               {
                  len--;
                  System.arraycopy(data, n + 1, data, n, len - n);
               }
            }

            // Store JAD file contents as String
            jadData[i] = new String(data, 0, len);

            // Count MIDlets in this JAD file
            int m;
            for (m = 1; jadData[i].indexOf("MIDlet-" + m + ":") >= 0; m++);
            jadMidletCount[i] = (m - 1);

            // Update total MIDlet count
            count += jadMidletCount[i];
         }
      }

      // Create new midlets array
      midlets = new MidletInfo[count];

      // Fill midlets array
      int item = 0;
      for (i = 0; i < jadFiles.length; i++)
      {
         int midlet;
         for (midlet = 1; midlet <= jadMidletCount[i]; midlet++)
         {
            midlets[item++] = new MidletInfo(currentDirAbsolute(), jadFiles[i],
                                             jadData[i], midlet);
         }
      }
   }

   // Gets actual absolute directory name of currentDir (which is relative)
   private String currentDirAbsolute()
   {
      return FileService.getRoot() +
             ((currentDir.length() > 0) ? ("/" + currentDir) : (""));
   }

   /**
    * Handles commands.
    */
   public void commandAction(Command c, Displayable s)
   {
      // Update current selection if appropriate
      if ((c.getCommandType() == Command.ITEM) || (c == List.SELECT_COMMAND))
      {
         int i = menuScreen.getSelectedIndex();
         if (i < subdirs.length)
         {
            selectedSubdir = subdirs[i];
            selectedMidlet = null;
         }
         else
         {
            selectedSubdir = null;
            selectedMidlet = midlets[i - subdirs.length];
         }
      }

      // Process command
      if (c == aboutCommand)
      {
         // Go to "about" screen
         display.setCurrent(aboutScreen, menuScreen);
      }
      else if (c == infoCommand)
      {
         // Go to "info" screen
         displayInfo();
      }
      else if (c == backCommand)
      {
         if (s != menuScreen)
         {
            // Go back to main menu screen
            display.setCurrent(menuScreen);
         }
         else if (currentDir.compareTo(EXTERNAL_MIDLETS_ROOT_DIR) == 0)
         {
            // Exit this app
            destroyApp(true);
            notifyDestroyed();
         }
         else
         {
            // Go back to previous subdirectory
            int i = currentDir.lastIndexOf('/');
            currentDir = (i > 0) ? currentDir.substring(0, i) : "";
            displayMenu();
         }
      }
      else if (c == exitCommand)
      {
         // Exit this app
         destroyApp(true);
         notifyDestroyed();
      }
      else if ((c == runCommand) || (c == List.SELECT_COMMAND))
      {
         if (selectedSubdir != null)
         {
            // Display menu for selected subdirectory
            currentDir += ((currentDir.length() > 0) ? "/" : "") +
                          selectedSubdir;
            displayMenu();
         }
         else
         {
            // Register this MIDlet as starting app to go back to when new app
            // exits, and new app as starting app to go to when we exit now.
            Main.registerStartApp(this.getClass().getName(), null);
            Main.registerStartApp(selectedMidlet.classname,
                                  selectedMidlet.dir + "/" +
                                  selectedMidlet.jadFile);

            // Exit this app
            destroyApp(true);
            notifyDestroyed();
         }
      }
   }

   /**
    * Starts the MIDlet.
    */
   public void startApp()
   {
      // Save display
      display = Display.getDisplay(this);

      // Go to default directory and display main menu screen
      currentDir = EXTERNAL_MIDLETS_ROOT_DIR;
      displayMenu();
   }

   /**
    * Prepares the MIDlet to be paused (and possibly later started again).
    */
   public void pauseApp()
   {
      // Nothing needs to be cleaned up for this MIDlet
   }

   /**
    * Prepares the MIDlet to be destroyed.
    */
   public void destroyApp(boolean unconditional)
   {
      // Nothing needs to be cleaned up for this MIDlet
   }
}

// Class to hold "hardcoded" images
class MidletBrowserIcons
{
   // Image for folder icon (from folder_icon.png)
   static private byte[] folderIconData =
   {
      (byte)0x89, (byte)0x50, (byte)0x4e, (byte)0x47, (byte)0x0d, (byte)0x0a,
      (byte)0x1a, (byte)0x0a, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x0d,
      (byte)0x49, (byte)0x48, (byte)0x44, (byte)0x52, (byte)0x00, (byte)0x00,
      (byte)0x00, (byte)0x0c, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x0c,
      (byte)0x08, (byte)0x02, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0xd9,
      (byte)0x17, (byte)0xcb, (byte)0xb0, (byte)0x00, (byte)0x00, (byte)0x00,
      (byte)0x48, (byte)0x49, (byte)0x44, (byte)0x41, (byte)0x54, (byte)0x78,
      (byte)0x9c, (byte)0xd5, (byte)0x90, (byte)0x31, (byte)0x0e, (byte)0xc0,
      (byte)0x30, (byte)0x0c, (byte)0x02, (byte)0xcf, (byte)0x55, (byte)0xbe,
      (byte)0xeb, (byte)0xbd, (byte)0x9b, (byte)0xf1, (byte)0x83, (byte)0xb3,
      (byte)0x67, (byte)0x73, (byte)0xb7, (byte)0x2c, (byte)0x8d, (byte)0xea,
      (byte)0xb9, (byte)0x88, (byte)0x05, (byte)0x09, (byte)0x04, (byte)0xc2,
      (byte)0xaa, (byte)0x8a, (byte)0x0e, (byte)0x03, (byte)0xc8, (byte)0xb4,
      (byte)0xad, (byte)0x23, (byte)0x4e, (byte)0x19, (byte)0x89, (byte)0x35,
      (byte)0x7d, (byte)0x53, (byte)0xa2, (byte)0x5e, (byte)0x30, (byte)0xa9,
      (byte)0xe9, (byte)0x8a, (byte)0xa8, (byte)0x01, (byte)0xdc, (byte)0xee,
      (byte)0x1f, (byte)0xa6, (byte)0x4c, (byte)0xbb, (byte)0xda, (byte)0xd5,
      (byte)0xc0, (byte)0x6f, (byte)0x4d, (byte)0xfd, (byte)0x4f, (byte)0xc0,
      (byte)0x03, (byte)0x0c, (byte)0xbf, (byte)0x29, (byte)0x9d, (byte)0xa8,
      (byte)0x58, (byte)0x54, (byte)0x5e, (byte)0x00, (byte)0x00, (byte)0x00,
      (byte)0x00, (byte)0x49, (byte)0x45, (byte)0x4e, (byte)0x44, (byte)0xae,
      (byte)0x42, (byte)0x60, (byte)0x82,
   };
   static Image folderIcon =
      Image.createImage(folderIconData, 0, folderIconData.length);
}

⌨️ 快捷键说明

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