GUI Test in Midlet


<strong>/*</strong>
<strong>Learning Wireless Java</strong>
<strong>Help for New J2ME Developers</strong>
<strong>By Qusay Mahmoud</strong>
<strong>ISBN: 0-596-00243-2</strong>

<strong>*/</strong>
<strong>import javax.microedition.lcdui.*;</strong>
<strong>import javax.microedition.midlet.*;</strong>

<strong>public class GuiTests extends MIDlet implements CommandListener {</strong>
<strong> // display manager</strong>
<strong> Display display = null;</strong>

<strong>// a menu with items</strong>
<strong> List menu = null; // main menu</strong>

<strong>// list of choices</strong>
<strong> List choose = null;</strong>

<strong>// textbox</strong>
<strong> TextBox input = null;</strong>

<strong>// ticker</strong>
<strong> Ticker ticker = new Ticker(”Test GUI Components”);</strong>

<strong>// alerts</strong>
<strong> final Alert soundAlert = new Alert(”sound Alert”);</strong>

<strong>// date</strong>
<strong> DateField date = new DateField(”Today’s date: “, DateField.DATE);</strong>

<strong>// form</strong>
<strong> Form form = new Form(”Form for Stuff”);</strong>

<strong>// gauge</strong>
<strong> Gauge gauge = new Gauge(”Progress Bar”, false, 20, 9);</strong>

<strong>// text field</strong>
<strong> TextField textfield = new TextField(”TextField Label”, “abc”, 50, 0);</strong>

<strong>// command</strong>
<strong> static final Command backCommand = new Command(”Back”, Command.BACK, 0);</strong>
<strong> static final Command mainMenuCommand = new Command(”Main”, Command.SCREEN, 1);</strong>
<strong> static final Command exitCommand = new Command(”Exit”, Command.STOP, 2);</strong>
<strong> String currentMenu = null;</strong>

<strong>// constructor.</strong>
<strong> public GuiTests() {</strong>
<strong> }</strong>

<strong>/**</strong>
<strong> * Start the MIDlet by creating a list of items and associating the</strong>
<strong> * exit command with it.</strong>
<strong> */</strong>
<strong> public void startApp() throws MIDletStateChangeException {</strong>
<strong> display = Display.getDisplay(this);</strong>
<strong> // open a db stock file</strong>

<strong>menu = new List(”Test Components”, Choice.IMPLICIT);</strong>
<strong> menu.append(”Test TextBox”, null);</strong>
<strong> menu.append(”Test List”, null);</strong>
<strong> menu.append(”Test Alert”, null);</strong>
<strong> menu.append(”Test Date”, null);</strong>
<strong> menu.append(”Test Form”, null);</strong>
<strong> menu.addCommand(exitCommand);</strong>
<strong> menu.setCommandListener(this);</strong>
<strong> menu.setTicker(ticker);</strong>

<strong>mainMenu();</strong>
<strong> }</strong>

<strong>public void pauseApp() {</strong>
<strong> display = null;</strong>
<strong> choose = null;</strong>
<strong> menu = null;</strong>
<strong> ticker = null;</strong>
<strong> form = null;</strong>
<strong> input = null;</strong>
<strong> gauge = null;</strong>
<strong> textfield = null; </strong>
<strong> }</strong>

<strong>public void destroyApp(boolean unconditional) {</strong>
<strong> notifyDestroyed();</strong>
<strong> }</strong>

<strong>// main menu</strong>
<strong> void mainMenu() {</strong>
<strong> display.setCurrent(menu);</strong>
<strong> currentMenu = “Main”; </strong>
<strong> }</strong>

<strong>/**</strong>
<strong> * Test the TextBox component.</strong>
<strong> */</strong>
<strong> public void testTextBox() {</strong>
<strong> input = new TextBox(”Enter Some Text:”, “”, 10, TextField.ANY);</strong>
<strong> input.setTicker(new Ticker(”Testing TextBox”));</strong>
<strong> input.addCommand(backCommand);</strong>
<strong> input.setCommandListener(this);</strong>
<strong> input.setString(””);</strong>
<strong> display.setCurrent(input);</strong>
<strong> currentMenu = “input”;</strong>
<strong> }</strong>

<strong>/**</strong>
<strong> * Test the List component.</strong>
<strong> */</strong>
<strong> public void testList() {</strong>
<strong> choose = new List(”Choose Items”, Choice.MULTIPLE);</strong>
<strong> choose.setTicker(new Ticker(”Testing List”));</strong>
<strong> choose.addCommand(backCommand);</strong>
<strong> choose.setCommandListener(this);</strong>
<strong> choose.append(”Item 1?, null);</strong>
<strong> choose.append(”Item 2?, null);</strong>
<strong> choose.append(”Item 3?, null);</strong>
<strong> display.setCurrent(choose);</strong>
<strong> currentMenu = “list”; </strong>
<strong> }</strong>

<strong>/**</strong>
<strong> * Test the Alert component.</strong>
<strong> */</strong>
<strong> public void testAlert() {</strong>
<strong> soundAlert.setType(AlertType.ERROR);</strong>
<strong> //soundAlert.setTimeout(20);</strong>
<strong> soundAlert.setString(”** ERROR **”);</strong>
<strong> display.setCurrent(soundAlert);</strong>
<strong> }</strong>

<strong>/**</strong>
<strong> * Test the DateField component.</strong>
<strong> */</strong>
<strong> public void testDate() {</strong>
<strong> java.util.Date now = new java.util.Date();</strong>
<strong> date.setDate(now);</strong>
<strong> Form f = new Form(”Today’s date”);</strong>
<strong> f.append(date);</strong>
<strong> f.addCommand(backCommand);</strong>
<strong> f.setCommandListener(this);</strong>
<strong> display.setCurrent(f);</strong>
<strong> currentMenu = “date”;</strong>
<strong> }</strong>

<strong>/**</strong>
<strong> * Test the Form component.</strong>
<strong> */</strong>
<strong> public void testForm() {</strong>
<strong> form.append(gauge);</strong>
<strong> form.append(textfield);</strong>
<strong> form.addCommand(backCommand);</strong>
<strong> form.setCommandListener(this);</strong>
<strong> display.setCurrent(form);</strong>
<strong> currentMenu = “form”;</strong>
<strong> }</strong>

<strong>/**</strong>
<strong> * Handle events.</strong>
<strong> */ </strong>
<strong> public void commandAction(Command c, Displayable d) {</strong>
<strong> String label = c.getLabel();</strong>
<strong> if (label.equals(”Exit”)) {</strong>
<strong> destroyApp(true);</strong>
<strong> } else if (label.equals(”Back”)) {</strong>
<strong> if(currentMenu.equals(”list”) || currentMenu.equals(”input”) || </strong>
<strong> currentMenu.equals(”date”) || currentMenu.equals(”form”)) {</strong>
<strong> // go back to menu</strong>
<strong> mainMenu();</strong>
<strong> }</strong>

<strong>} else {</strong>
<strong> List down = (List)display.getCurrent();</strong>
<strong> switch(down.getSelectedIndex()) {</strong>
<strong> case 0: testTextBox();break;</strong>
<strong> case 1: testList();break;</strong>
<strong> case 2: testAlert();break;</strong>
<strong> case 3: testDate();break;</strong>
<strong> case 4: testForm();break;</strong>
<strong> }</strong>

<strong>}</strong>
<strong> }</strong>
<strong>}</strong>

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

Mari kita bersama meningkatkan kesadaran kita - Mulailah berpikir cerdas, Kita semua beragama !!! Agama bukan identitas...jadikanlah agama untuk keluar dari kebodohan

Tulisan di blog ini mungkin sangat ngawur tapi mungkin juga benar. Merdekakan pikiran anda, sentuh hati nurani anda. Yang ada tinggal KASUNYATAN SEJATI

Zoemalang's community at www. zoemalang.wordpress.com

ujung malang adalah Sebuah desa yang hilang terganti dengan ujung harapan

YoYo Games Blog Feed

Tulisan di blog ini mungkin sangat ngawur tapi mungkin juga benar. Merdekakan pikiran anda, sentuh hati nurani anda. Yang ada tinggal KASUNYATAN SEJATI

Dealer Termurah

Menjual Segala Jenis Motor Baru Di Indonesia Cash Maupun Kredit

%d blogger menyukai ini: