2013-02-02 37 views
2

我试图制作一个简单的“hello world”应用程序,点击按钮后,它会打印一个字符串“hello world”。如何在表单上添加按钮?如何在J2ME应用程序中创建表单上的按钮?

我需要创建一个按钮,当我点击它时可以产生一个字符串。如何在j2me中不使用画布添加按钮?

回答

3

有这个一个API,但你最好三思而后行,你是否真的需要它。

API在Appearance modes section for lcdui Item objects

desctibed的StringItem的和的ImageItem类具有可以在它们的构造来设置的外观模式属性。该属性可以具有PLAIN,HYPERLINK或BUTTON的其中一个值。 PLAIN的外观模式通常用于文本或图形材料的非交互式显示。外观模式值对项目的交互性没有任何副作用。为了进行交互操作,该项目必须有一个或多个命令(最好使用指定的默认命令),并且它必须具有接收命令调用通知的CommandListener ...

A 按钮模式下的StringItem或ImageItem可用于创建基于按钮的用户界面 ...

注意,这部分还介绍了使用按钮的外观时,可能会出现问题的情况下:

...这很容易导致不方便的应用程序 使用。例如,在基于遍历的系统中,用户必须导航到按钮才能调用其上的任何命令。如果按钮分布在一个长表单中,用户可能需要执行大量的导航才能发现所有可用的命令。此外,从窗体另一端的按钮调用命令可能非常麻烦。基于遍历的系统通常提供从任何地方(例如从菜单)调用命令的手段,而不需要遍历特定项目。如果将命令直接添加到表单中,用户通常会更加适合和方便用户,而不是将按钮添加到按钮中。只有在用户直接与用户交互项目的字符串或图像内容对用户理解可从该项目调用的命令而言至关重要的情况下才应使用按钮。

+0

谢谢,这条信息对我来说非常有用。 – NSharma

+1

大拇指(和表决)。谢谢!每天学些新东西。 –

4

从我在一本旧的J2ME书籍中找到的类图中看到,J2ME没有按钮,它在http://www.stardeveloper.com/articles/display.html?article=2002121101&page=2处在线。好的,在老手机上不需要他们。

只需创建一个“hello”命令并将其添加到菜单或表单。系统会将其放在您设备上的任何按钮上。对于触摸屏设备,可能会变成可点击的东西。

下面的代码

import javax.microedition.lcdui.Command; 
import javax.microedition.lcdui.CommandListener; 
import javax.microedition.lcdui.Display; 
import javax.microedition.lcdui.Displayable; 
import javax.microedition.lcdui.Form; 
import javax.microedition.lcdui.TextBox; 
import javax.microedition.lcdui.TextField; 
import javax.microedition.midlet.MIDlet; 
import javax.microedition.midlet.MIDletStateChangeException; 


public class HelloWorld extends MIDlet implements CommandListener { 

    private static final String HELLO_WORLD = "Hello, World!!"; 

    private Form form= new Form (""); 

    private Command exit= new Command("Exit", Command.EXIT, 0x01); 
    private Command ok= new Command("OK", Command.OK, 0x01); 
    private Command hello= new Command("HELLO", Command.SCREEN, 0x01); 

    private TextBox textBox= new TextBox("Hello World", HELLO_WORLD, HELLO_WORLD.length(), TextField.UNEDITABLE); 

    public HelloWorld() { 
     this.form.addCommand(exit); 
     this.form.addCommand(hello); 
     this.form.setCommandListener(this); 
     this.textBox.addCommand(ok); 
     this.textBox.addCommand(exit); 
     this.textBox.setCommandListener(this); 
    } 

    protected void destroyApp(boolean unconditional) 
      throws MIDletStateChangeException { } 

    protected void pauseApp() { } 

    protected void startApp() throws MIDletStateChangeException { 
     Display.getDisplay(this).setCurrent(this.form); 
    } 

    public void commandAction(Command c, Displayable d) { 
     if (c == this.exit) { 
      this.notifyDestroyed(); 
     } 
     if(c == this.ok) { 
      Display.getDisplay(this).setCurrent(this.form);   
     } 
     if(c == this.hello) { 
      Display.getDisplay(this).setCurrent(this.textBox);   
     } 
    } 

} 
+0

非常感谢!我很难弄清楚如何在屏幕上添加按钮,最后我知道j2me应用程序不支持按钮。谢谢。 – NSharma

相关问题