程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> J2ME >> JavaMe開發:繪制文本框TextEdit(1)

JavaMe開發:繪制文本框TextEdit(1)

編輯:J2ME

【問題描述】在JavaMe連載(3)-也說MVC設計模式 一文中提到了一個TextEdit類,但沒有給出具體實現,TextEdit是采用GameCanvas繪制的文本編輯器。本文結合實例給出實現的方法。

【原理】

1 運用Graphics、GameCanvas繪制文本框和光標。

2 檢測到輸入事件時,跳轉到 高級界面->TextBox 。通過系統調用輸入法完成輸入。

3 將TextBox輸入的值返回給TextEdit對象。

【設計模式】

這個過程有點類似裝飾模式,實際上,實現輸入的還是TextBox,只是給TextBox裝飾了一下,形成了一個漂亮的外觀。

【代碼清單】

TextEdit.Java

  1. package com.token.vIEw.components;
  2. import Javax.microedition.lcdui.Font;
  3. import Javax.microedition.lcdui.Graphics;
  4. import Javax.microedition.lcdui.game.GameCanvas;
  5. public class TextEdit extends GameCanvas
  6. {
  7. private Font ft;
  8. public int width;
  9. public int height;
  10. public TextEdit(GameCanvas canvas)
  11. {
  12. super(false);
  13. }
  14. //繪制文本框
  15. public void drawTextBox(GameCanvas canvas, Graphics graphics, String text, int x, int y, boolean cursorBlinkOn)
  16. {
  17. //System.out.println("draw");
  18. int padding = 4;
  19. int margin = 2;
  20. ft = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN,Font.SIZE_MEDIUM);
  21. graphics.setFont(ft);
  22. width = 3*canvas.getWidth()/5+2*padding;
  23. height = ft.getHeight()+2*padding;
  24. graphics.setColor(Color.frame);
  25. graphics.fillRect(x+1,y+1,width+margin,height+margin);
  26. graphics.setColor(Color.frameBg);
  27. graphics.drawRect(x, y,width, height);
  28. graphics.setColor(Color.background);
  29. graphics.fillRect(x+1, y+1,width-1,height-1);
  30. graphics.setColor(Color.text);
  31. graphics.drawString(text, x+padding, y+padding, Graphics.TOP|Graphics.LEFT);
  32. drawCursor(graphics, x+ft.stringWidth(text)+padding, y+padding, 1, ft.getHeight(), cursorBlinkOn);
  33. canvas.flushGraphics(x,y,width,height);
  34. }
  35. //繪制光標
  36. public void drawCursor(Graphics graphics, int x, int y, int width, int height, boolean cursorBlinkOn)
  37. {
  38. if(cursorBlinkOn)
  39. {
  40. ft = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN,Font.SIZE_MEDIUM);
  41. graphics.setFont(ft);
  42. graphics.setColor(0x0,0x0,0x0);
  43. graphics.drawLine(x+width,y,x+width,y+height);
  44. }
  45. }
  46. }

PopUpTextBox.Java

  1. package com.token.vIEw;
  2. import Javax.microedition.lcdui.Command;
  3. import Javax.microedition.lcdui.CommandListener;
  4. import Javax.microedition.lcdui.Displayable;
  5. import Javax.microedition.lcdui.TextBox;
  6. import com.token.util.UIController;
  7. public class PopUpTextBox extends TextBox {
  8. private UIController controller;
  9. protected String canvasText = "";
  10. private Command okCommand;
  11. private Command cancelCommand;
  12. private String object_name = null;
  13. private String editor = null;
  14. private Object args_t[];
  15. private Object[] args;
  16. public PopUpTextBox(UIController control, String title, String text, int maxsize, int constraints)
  17. {
  18. super(title, text, maxsize, constraints);
  19. controller = control;
  20. args = new Object[6];
  21. okCommand = new Command("確定", Command.OK, 1);
  22. cancelCommand = new Command("取消", Command.CANCEL, 1);
  23. this.addCommand(okCommand);
  24. this.addCommand(cancelCommand);
  25. this.setCommandListener(new TextBoxListener());
  26. }
  27. public void init(Object[] args)
  28. {
  29. object_name = ((String)args[0]!=null)?(String)args[0]:"";
  30. editor = ((String)args[1]!=null)?(String)args[1]:"";
  31. //System.out.println(object_name);
  32. //System.out.println(editor);
  33. args_t = args;
  34. this.setString("");
  35. }
  36. protected void closeTextBox(boolean update) {
  37. if (update) canvasText = this.getString();
  38. //System.out.println(canvasText);
  39. if(object_name.equals("registScreen"))
  40. {
  41. if(editor.equals("regist_name"))
  42. {
  43. if(args_t[3]!=""||args_t[3]!=null||args_t[4]!=""||args_t[4]!=null)
  44. {
  45. args[0] = object_name;
  46. args[1] = editor;
  47. args[2] = this.canvasText;
  48. args[3] = args_t[3];
  49. args[4] = args_t[4];
  50. }
  51. controller.handleEvent(UIController.EventID.EVENT_USER_REGIST_EDIT_BACK,args);
  52. }
  53. else if(editor.equals("regist_passwd"))
  54. {
  55. if(args_t[2]!=""||args_t[2]!=null||args_t[4]!=""||args_t[4]!=null)
  56. {
  57. args[0] = object_name;
  58. args[1] = editor;
  59. args[2] = args_t[2];
  60. args[3] = this.canvasText;
  61. args[4] = args_t[4];
  62. }
  63. controller.handleEvent(UIController.EventID.EVENT_USER_REGIST_EDIT_BACK,args);
  64. }
  65. else if(editor.equals("regist_passwd_re"))
  66. {
  67. if(args_t[2]!=""||args_t[2]!=null||args_t[3]!=""||args_t[3]!=null)
  68. {
  69. args[0] = object_name;
  70. args[1] = editor;
  71. args[2] = args_t[2];
  72. args[3] = args_t[3];
  73. args[4] = this.canvasText;
  74. }
  75. controller.handleEvent(UIController.EventID.EVENT_USER_REGIST_EDIT_BACK,args);
  76. }
  77. }
  78. //...
  79. }
  80. private class TextBoxListener implements CommandListener
  81. {
  82. public void commandAction(Command command, Displayable disp)
  83. {
  84. if(command==okCommand)
  85. {
  86. closeTextBox(true);
  87. }
  88. else if(command==cancelCommand)
  89. {
  90. closeTextBox(false);
  91. }
  92. }
  93. }
  94. }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved