Guest User

Untitled

a guest
Sep 14th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.86 KB | None | 0 0
  1. How do I add button in J2ME
  2. form = new Form("ePolicia Transgressor Accuse");
  3. tb = new TextField("Complaint Id: ", "", 30, TextField.ANY);
  4. form.append(tb);
  5. ticker = new Ticker("Creating Safer City for You");
  6. form.addCommand(submitCommand);
  7. form.addCommand(exitCommand);
  8. form.setCommandListener(this);
  9. form.setTicker(ticker);
  10. display.setCurrent(form);
  11.  
  12. /*
  13. * To change this template, choose Tools | Templates
  14. * and open the template in the editor.
  15. */
  16.  
  17. package hello;
  18.  
  19. import javax.microedition.midlet.*;
  20. import javax.microedition.lcdui.*;
  21.  
  22. /**
  23. * @author
  24. */
  25. public class HelloMIDlet extends MIDlet implements CommandListener, ItemCommandListener {
  26.  
  27. private boolean midletPaused = false;
  28.  
  29. //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">
  30. private Command exitCommand;
  31. private Command okCommand;
  32. private Command exitCommand1;
  33. private Form form;
  34. private StringItem lblUserName;
  35. private TextField txtUserName;
  36. private StringItem lblPassword;
  37. private TextField txtPassword;
  38. private StringItem lblBlankSpace;
  39. private StringItem cmdOK;
  40. private StringItem stringItem;
  41. //</editor-fold>
  42.  
  43. /**
  44. * The HelloMIDlet constructor.
  45. */
  46. public HelloMIDlet() {
  47. }
  48.  
  49. //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">
  50. //</editor-fold>
  51.  
  52. //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">
  53. /**
  54. * Initilizes the application.
  55. * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
  56. */
  57. private void initialize() {
  58. // write pre-initialize user code here
  59.  
  60. // write post-initialize user code here
  61. }
  62. //</editor-fold>
  63.  
  64. //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">
  65. /**
  66. * Performs an action assigned to the Mobile Device - MIDlet Started point.
  67. */
  68. public void startMIDlet() {
  69. // write pre-action user code here
  70. switchDisplayable(null, getForm());
  71. // write post-action user code here
  72. }
  73. //</editor-fold>
  74.  
  75. //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">
  76. /**
  77. * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
  78. */
  79. public void resumeMIDlet() {
  80. // write pre-action user code here
  81.  
  82. // write post-action user code here
  83. }
  84. //</editor-fold>
  85.  
  86. //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">
  87. /**
  88. * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
  89. * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
  90. * @param nextDisplayable the Displayable to be set
  91. */
  92. public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
  93. // write pre-switch user code here
  94. Display display = getDisplay();
  95. if (alert == null) {
  96. display.setCurrent(nextDisplayable);
  97. } else {
  98. display.setCurrent(alert, nextDisplayable);
  99. }
  100. // write post-switch user code here
  101. }
  102. //</editor-fold>
  103.  
  104. //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">
  105. /**
  106. * Called by a system to indicated that a command has been invoked on a particular displayable.
  107. * @param command the Command that was invoked
  108. * @param displayable the Displayable where the command was invoked
  109. */
  110. public void commandAction(Command command, Displayable displayable) {
  111. // write pre-action user code here
  112. if (displayable == form) {
  113. if (command == exitCommand) {
  114. // write pre-action user code here
  115. exitMIDlet();
  116. // write post-action user code here
  117. }
  118. }
  119. // write post-action user code here
  120. }
  121. //</editor-fold>
  122.  
  123. //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">
  124. /**
  125. * Returns an initiliazed instance of exitCommand component.
  126. * @return the initialized component instance
  127. */
  128. public Command getExitCommand() {
  129. if (exitCommand == null) {
  130. // write pre-init user code here
  131. exitCommand = new Command("Exit", Command.EXIT, 0);
  132. // write post-init user code here
  133. }
  134. return exitCommand;
  135. }
  136. //</editor-fold>
  137.  
  138. //<editor-fold defaultstate="collapsed" desc=" Generated Getter: form ">
  139. /**
  140. * Returns an initiliazed instance of form component.
  141. * @return the initialized component instance
  142. */
  143. public Form getForm() {
  144. if (form == null) {
  145. // write pre-init user code here
  146. form = new Form("Welcome", new Item[] { getLblUserName(), getTxtUserName(), getLblPassword(), getTxtPassword(), getLblBlankSpace(), getCmdOK(), getStringItem() });
  147. form.addCommand(getExitCommand());
  148. form.setCommandListener(this);
  149. // write post-init user code here
  150. }
  151. return form;
  152. }
  153. //</editor-fold>
  154. //</editor-fold>
  155.  
  156.  
  157.  
  158. //<editor-fold defaultstate="collapsed" desc=" Generated Getter: lblUserName ">
  159. /**
  160. * Returns an initiliazed instance of lblUserName component.
  161. * @return the initialized component instance
  162. */
  163. public StringItem getLblUserName() {
  164. if (lblUserName == null) {
  165. // write pre-init user code here
  166. lblUserName = new StringItem("User Name :", "");
  167. lblUserName.setLayout(ImageItem.LAYOUT_CENTER);
  168. // write post-init user code here
  169. }
  170. return lblUserName;
  171. }
  172. //</editor-fold>
  173.  
  174. //<editor-fold defaultstate="collapsed" desc=" Generated Getter: txtUserName ">
  175. /**
  176. * Returns an initiliazed instance of txtUserName component.
  177. * @return the initialized component instance
  178. */
  179. public TextField getTxtUserName() {
  180. if (txtUserName == null) {
  181. // write pre-init user code here
  182. txtUserName = new TextField("", "", 32, TextField.ANY);
  183. txtUserName.setLayout(ImageItem.LAYOUT_CENTER | ImageItem.LAYOUT_NEWLINE_AFTER);
  184. // write post-init user code here
  185. }
  186. return txtUserName;
  187. }
  188. //</editor-fold>
  189.  
  190. //<editor-fold defaultstate="collapsed" desc=" Generated Getter: lblPassword ">
  191. /**
  192. * Returns an initiliazed instance of lblPassword component.
  193. * @return the initialized component instance
  194. */
  195. public StringItem getLblPassword() {
  196. if (lblPassword == null) {
  197. // write pre-init user code here
  198. lblPassword = new StringItem("Password :", "");
  199. lblPassword.setLayout(ImageItem.LAYOUT_CENTER | ImageItem.LAYOUT_NEWLINE_AFTER);
  200. // write post-init user code here
  201. }
  202. return lblPassword;
  203. }
  204. //</editor-fold>
  205.  
  206. //<editor-fold defaultstate="collapsed" desc=" Generated Getter: txtPassword ">
  207. /**
  208. * Returns an initiliazed instance of txtPassword component.
  209. * @return the initialized component instance
  210. */
  211. public TextField getTxtPassword() {
  212. if (txtPassword == null) {
  213. // write pre-init user code here
  214. txtPassword = new TextField("", "", 32, TextField.ANY);
  215. txtPassword.setLayout(ImageItem.LAYOUT_CENTER | ImageItem.LAYOUT_NEWLINE_AFTER);
  216. // write post-init user code here
  217. }
  218. return txtPassword;
  219. }
  220. //</editor-fold>
  221. //</editor-fold>
  222.  
  223. //<editor-fold defaultstate="collapsed" desc=" Generated Getter: lblBlankSpace ">
  224. /**
  225. * Returns an initiliazed instance of lblBlankSpace component.
  226. * @return the initialized component instance
  227. */
  228. public StringItem getLblBlankSpace() {
  229. if (lblBlankSpace == null) {
  230. // write pre-init user code here
  231. lblBlankSpace = new StringItem("", "");
  232. // write post-init user code here
  233. }
  234. return lblBlankSpace;
  235. }
  236. //</editor-fold>
  237.  
  238. //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Items ">
  239. /**
  240. * Called by a system to indicated that a command has been invoked on a particular item.
  241. * @param command the Command that was invoked
  242. * @param displayable the Item where the command was invoked
  243. */
  244. public void commandAction(Command command, Item item) {
  245. // write pre-action user code here
  246. if (item == cmdOK) {
  247. if (command == okCommand) {
  248. // write pre-action user code here
  249.  
  250. // write post-action user code here
  251. }
  252. } else if (item == stringItem) {
  253. if (command == exitCommand1) {
  254. // write pre-action user code here
  255. exitMIDlet();
  256. // write post-action user code here
  257. }
  258. }
  259. // write post-action user code here
  260. }
  261. //</editor-fold>
  262.  
  263. //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand ">
  264. /**
  265. * Returns an initiliazed instance of okCommand component.
  266. * @return the initialized component instance
  267. */
  268. public Command getOkCommand() {
  269. if (okCommand == null) {
  270. // write pre-init user code here
  271. okCommand = new Command("Ok", Command.OK, 0);
  272. // write post-init user code here
  273. }
  274. return okCommand;
  275. }
  276. //</editor-fold>
  277.  
  278. //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdOK ">
  279. /**
  280. * Returns an initiliazed instance of cmdOK component.
  281. * @return the initialized component instance
  282. */
  283. public StringItem getCmdOK() {
  284. if (cmdOK == null) {
  285. // write pre-init user code here
  286. cmdOK = new StringItem("Login", "", Item.BUTTON);
  287. cmdOK.addCommand(getOkCommand());
  288. cmdOK.setItemCommandListener(this);
  289. // write post-init user code here
  290. }
  291. return cmdOK;
  292. }
  293. //</editor-fold>
  294.  
  295. //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem ">
  296. /**
  297. * Returns an initiliazed instance of stringItem component.
  298. * @return the initialized component instance
  299. */
  300. public StringItem getStringItem() {
  301. if (stringItem == null) {
  302. // write pre-init user code here
  303. stringItem = new StringItem("Cancel", "", Item.BUTTON);
  304. stringItem.addCommand(getExitCommand1());
  305. stringItem.setItemCommandListener(this);
  306. // write post-init user code here
  307. }
  308. return stringItem;
  309. }
  310. //</editor-fold>
  311.  
  312. //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand1 ">
  313. /**
  314. * Returns an initiliazed instance of exitCommand1 component.
  315. * @return the initialized component instance
  316. */
  317. public Command getExitCommand1() {
  318. if (exitCommand1 == null) {
  319. // write pre-init user code here
  320. exitCommand1 = new Command("Exit", Command.EXIT, 0);
  321. // write post-init user code here
  322. }
  323. return exitCommand1;
  324. }
  325. //</editor-fold>
  326.  
  327. /**
  328. * Returns a display instance.
  329. * @return the display instance.
  330. */
  331. public Display getDisplay () {
  332. return Display.getDisplay(this);
  333. }
  334.  
  335. /**
  336. * Exits MIDlet.
  337. */
  338. public void exitMIDlet() {
  339. switchDisplayable (null, null);
  340. destroyApp(true);
  341. notifyDestroyed();
  342. }
  343.  
  344. /**
  345. * Called when MIDlet is started.
  346. * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
  347. */
  348. public void startApp() {
  349. if (midletPaused) {
  350. resumeMIDlet ();
  351. } else {
  352. initialize ();
  353. startMIDlet ();
  354. }
  355. midletPaused = false;
  356. }
  357.  
  358. /**
  359. * Called when MIDlet is paused.
  360. */
  361. public void pauseApp() {
  362. midletPaused = true;
  363. }
  364.  
  365. /**
  366. * Called to signal the MIDlet to terminate.
  367. * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
  368. */
  369. public void destroyApp(boolean unconditional) {
  370. }
  371.  
  372. }
  373.  
  374. login = new StringItem("", "Login", StringItem.BUTTON);
  375. select = new Command("Select", Command.OK, 0);
Add Comment
Please, Sign In to add comment