ui_ex11.java

来自「J2ME 无线通信技术应用开发源代码」· Java 代码 · 共 52 行

JAVA
52
字号
// 程序名UI_Ex11.java
// 利用AlertType对象在程序中播放声音
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class UI_Ex11 extends MIDlet implements CommandListener {
  private Display display; // 引用MIDlet的Display 对象
  private AlertType alertType; // 设置一个AlertType对象
  private TextBox textBox;
  private Command cmdExit; // 设定命令用于退出MIDlet
  private Command cmdSound; // 设定命令用于发声

  // MIDlet构造程序
  public UI_Ex11() {
    display = Display.getDisplay(this);
    alertType = AlertType.ALARM;
    cmdExit = new Command("Exit", Command.SCREEN, 1);
    cmdSound = new Command("Sound", Command.SCREEN, 1);
    textBox = new TextBox("Test AlertType","Press \"Sound\" , and hear the sound!",50,0);
    textBox.addCommand(cmdExit);
    textBox.addCommand(cmdSound);
    textBox.setCommandListener(this);
  }

  // 被应用程序管理器调用来启动MIDlet。
  public void startApp() {
    display.setCurrent(textBox);
  }

  // 一个必要的方法
  public void pauseApp() {
  }

  //一个必要的方法
  public void destroyApp(boolean unconditional) {
  }

  public void commandAction(Command c, Displayable d) {
    // 触发请求播放声音的事件
    if (c == cmdSound) {
      if (alertType.playSound(display)) {
        textBox.setString("Playing the sound.");
      } else {
        textBox.setString("Error when play the sound");
      }
    }
    if (c == cmdExit) {
      destroyApp(false);
      notifyDestroyed();
    }
 }
}

⌨️ 快捷键说明

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