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

J2ME

开发平台:

Java

  1. package ColorDemo;
  2. import javax.microedition.lcdui.*;
  3. /**
  4.  * A Font chooser.  This screen can be used to
  5.  * choose fonts.  A form is used to select from
  6.  * the various choices for size, style, and face.
  7.  */
  8. public class FontChooser extends Form implements ItemStateListener {
  9.     int face;
  10.     int style;
  11.     int size;
  12.     ChoiceGroup faceChoice;
  13.     ChoiceGroup styleChoice;
  14.     ChoiceGroup sizeChoice;
  15.     /** 
  16.      * Create a new font chooser form.
  17.      * Create each of the form entries
  18.      */
  19.     public FontChooser() {
  20. super("Choose Attributes");
  21. faceChoice = new ChoiceGroup("Face", Choice.EXCLUSIVE);
  22. faceChoice.append("System", null);
  23. faceChoice.append("Monospace", null);
  24. faceChoice.append("Proportional", null);
  25. styleChoice = new ChoiceGroup("Style", Choice.MULTIPLE);
  26. styleChoice.append("Bold", null);
  27. styleChoice.append("Italic", null);
  28. styleChoice.append("Underlined", null);
  29. sizeChoice = new ChoiceGroup("Size", Choice.EXCLUSIVE);
  30. sizeChoice.append("Small", null);
  31. sizeChoice.append("Medium", null);
  32. sizeChoice.append("Large", null);
  33. append("Face");
  34. append(faceChoice);
  35. append("Style");
  36. append(styleChoice);
  37. append("Size");
  38. append(sizeChoice);
  39. setItemStateListener(this);
  40.     }
  41.     /**
  42.      * Set the Style of font to display.
  43.      * @param style the style to select
  44.      * @see Font.getStyle;
  45.      */
  46.     public void setStyle(int style) {
  47. this.style = style;
  48.     }
  49.     /**
  50.      * Get the style of font currently being displayed.
  51.      * @return the current style being used for text
  52.      * @see Font.getStyle
  53.      */
  54.     public int getStyle() {
  55. return style;
  56.     }
  57.     /**
  58.      * Set the Face of font to display.
  59.      * @param face the face to select
  60.      * @see Font.getFace;
  61.      */
  62.     public void setFace(int face) {
  63. this.face = face;
  64.     }
  65.     /**
  66.      * Get the face of font currently being displayed.
  67.      * @return the current face of the font
  68.      * @see Font.getFace
  69.      */
  70.     public int getFace() {
  71. return face;
  72.     }
  73.     /**
  74.      * Set the Size of font to display.
  75.      * @param size of the font to set
  76.      * @see Font.getSize;
  77.      */
  78.     public void setSize(int size) {
  79. this.size = size;
  80.     }
  81.     /**
  82.      * Get the size of font currently being displayed.
  83.      * @return the current size of the font
  84.      * @see Font.getSize
  85.      */
  86.     public int getSize() {
  87. return size;
  88.     }
  89.     /**
  90.      * Reflect changes in the item states into the states.
  91.      * @param item that to which some change occurred
  92.      */
  93.     public void itemStateChanged(Item item) {
  94. if (item == faceChoice) {
  95.     int f = faceChoice.getSelectedIndex();
  96.     switch (f) {
  97. case 0: face = Font.FACE_SYSTEM; break;
  98. case 1: face = Font.FACE_MONOSPACE; break;
  99. case 2: face = Font.FACE_PROPORTIONAL; break;
  100.     }
  101. } else if (item == styleChoice) {
  102.     style = 0;
  103.     if (styleChoice.isSelected(0))
  104. style += Font.STYLE_BOLD;
  105.     if (styleChoice.isSelected(1))
  106. style |= Font.STYLE_ITALIC;
  107.     if (styleChoice.isSelected(2))
  108. style |= Font.STYLE_UNDERLINED;
  109. } else if (item == sizeChoice) {
  110.     int s = sizeChoice.getSelectedIndex();
  111.     switch (s) {
  112. case 0: size = Font.SIZE_SMALL; break;
  113. case 1: size = Font.SIZE_MEDIUM; break;
  114. case 2: size = Font.SIZE_LARGE; break;
  115.     }
  116. }
  117.     }
  118. }