Chooser.java
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:3k
源码类别:

J2ME

开发平台:

Java

  1. package ColorDemo;
  2. import javax.microedition.midlet.*;
  3. import javax.microedition.lcdui.*;
  4. public class Chooser extends MIDlet implements CommandListener {
  5.     private Display display; // Our display
  6.     private FontChooser fonts;
  7.     private TextSample sample;
  8.     private ColorChooser colors;
  9.     private Command exitCommand = 
  10. new Command("Exit", Command.EXIT, 1);
  11.     private Command textColorCommand = 
  12. new Command("Text Color", Command.SCREEN, 3);
  13.     private Command backgroundColorCommand = 
  14. new Command("Background Color", Command.SCREEN, 4);
  15.     private Command fontsCommand = 
  16. new Command("Fonts", Command.SCREEN, 11);
  17.     
  18.     private Command okCommand = 
  19. new Command("Ok", Command.SCREEN, 2);
  20.     private Command okFgCommand = 
  21. new Command("Ok", Command.SCREEN, 2);
  22.     private Command okBgCommand = 
  23. new Command("Ok", Command.SCREEN, 2);
  24.     public Chooser() {
  25. display = Display.getDisplay(this);
  26. sample = new TextSample();
  27. sample.addCommand(exitCommand);
  28. sample.addCommand(textColorCommand);
  29. sample.addCommand(backgroundColorCommand);
  30. sample.addCommand(fontsCommand);
  31. sample.setCommandListener(this);
  32.     }
  33.     /**
  34.      * Create the FontChooser and make it current
  35.      */
  36.     public void startApp() {
  37. display.setCurrent(sample);
  38.     }
  39.     /**
  40.      * Pause
  41.      */
  42.     public void pauseApp() {
  43.     }
  44.     /**
  45.      * Destroy must cleanup everything.
  46.      */
  47.     public void destroyApp(boolean unconditional) {
  48.     }
  49.     /*
  50.      * Respond to a commands issued on any Screen
  51.      */
  52.     public void commandAction(Command c, Displayable s) {
  53. if (c == exitCommand) {
  54.     destroyApp(true);
  55.     notifyDestroyed();
  56. } else if (c == fontsCommand) {
  57.     if (fonts == null) {
  58. fonts = new FontChooser();
  59. fonts.setFace(sample.getFace());
  60. fonts.setStyle(sample.getStyle());
  61. fonts.setSize(sample.getSize());
  62. fonts.addCommand(okCommand);
  63. fonts.setCommandListener(this);
  64.     }
  65.     display.setCurrent(fonts);
  66. } else if (c == backgroundColorCommand) {
  67.     if (colors == null) {
  68. colors = new ColorChooser(display.isColor(), 
  69.   display.numColors());
  70. colors.setCommandListener(this);
  71.     }
  72.     colors.addCommand(okBgCommand);
  73.     colors.removeCommand(okFgCommand);
  74.     colors.setColor(sample.getBackgroundColor());
  75.     display.setCurrent(colors);
  76. } else if (c == textColorCommand) {
  77.     if (colors == null) {
  78. colors = new ColorChooser(display.isColor(), 
  79.   display.numColors());
  80. colors.setCommandListener(this);
  81.     }
  82.     colors.removeCommand(okBgCommand);
  83.     colors.setColor(sample.getForegroundColor());
  84.     display.setCurrent(colors);
  85. } else if (c == okCommand) {
  86.     if (s == fonts) {
  87. sample.setStyle(fonts.getStyle());
  88. sample.setFace(fonts.getFace());
  89. sample.setSize(fonts.getSize());
  90.     }
  91.     display.setCurrent(sample);
  92. } else if (c == okFgCommand) {
  93.     sample.setForegroundColor(colors.getColor());
  94.     display.setCurrent(sample);
  95. } else if (c == okBgCommand) {
  96.     sample.setBackgroundColor(colors.getColor());
  97.     display.setCurrent(sample);
  98. }
  99.     } 
  100. }