当前位置: 技术问答>java相关
motorola的LWT控件再问!!
来源: 互联网 发布时间:2015-11-03
本文导语: 我改了一个motorolaSDK包里的LWT程序, 就是在屏幕上显示三个按钮(Button)。 然后我想再按下某一个按钮的时候, 出现一个警告(其实出别的也成,现在就是什么也不出:( ), 我用的是ComponentListener接口 和里面的proces...
我改了一个motorolaSDK包里的LWT程序,
就是在屏幕上显示三个按钮(Button)。
然后我想再按下某一个按钮的时候,
出现一个警告(其实出别的也成,现在就是什么也不出:( ),
我用的是ComponentListener接口
和里面的processComponentEvent()方法!
可是我怎么按那个按钮都没有反应,请问我的程序应该怎么改一下?
附源程序:
import com.motorola.lwt.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class MyLWTDemo extends MIDlet {
public Display display = Display.getDisplay(this);
DemoScreen screens = new ButtonScreen(display);
public MyLWTDemo() {}
protected void startApp()throws MIDletStateChangeException {
display.setCurrent(screens);
}
protected void pauseApp(){}
protected void destroyApp(boolean unconditional)
throws MIDletStateChangeException{}
}
/**
* Superclass for all of the demo screens, provides the next/previous commands
**/
class DemoScreen extends ComponentScreen {
/**
* Creates a new DemoScreen, creates and adds the next and previous commands
**/
public DemoScreen() {
Command next = new Command("Next", Command.OK, 1);
Command prev = new Command("Previous", Command.BACK, 1);
addCommand(next);
addCommand(prev);
}
}
/**
* Demo Screen for the Button Component. This screen shows the LWT Button component
* in a variety of sizes and in the disabled state.
**/
class ButtonScreen extends DemoScreen implements ComponentListener,CommandListener{
private Button b1;
private Display display;
public ButtonScreen(Display display) {
this.display = display;
// Add a button to the screen
b1 = new Button("Button");
add(b1);
this.addCommand(new Command("abc",Command.SCREEN,1));
this.setCommandListener(this);
//我在这里加了一个Command,是可以实现的
this.set
// Add another button to the screen
Button b2 = new Button("Large Button");
add(b2);
// Extend the right edge to the right edge of the screen
b2.setRightEdge(Component.SCREEN_RIGHT, 0);
// Make it twice its preferred height
b2.setBottomEdge(Component.HEIGHT, b2.getPreferredHeight() * 2);
// Add a disabled button
Button b3 = new Button("Disabled Button");
b3.setEnabled(false);
add(b3);
}
public void processComponentEvent(java.lang.Object source, int eventType){
Alert alarmAlert= new Alert("警报","这是一个"警报"信息窗口",null,AlertType.ALARM);
alarmAlert.setTimeout(2000);
display.setCurrent(alarmAlert);
}
public void commandAction(Command c, Displayable d){
Alert alarmAlert= new Alert("警报","这是一个"警报"信息窗口",null,AlertType.ALARM);
alarmAlert.setTimeout(2000);
display.setCurrent(alarmAlert);
}
}
是不是我没用setXXXXListener()的缘故?
就是在屏幕上显示三个按钮(Button)。
然后我想再按下某一个按钮的时候,
出现一个警告(其实出别的也成,现在就是什么也不出:( ),
我用的是ComponentListener接口
和里面的processComponentEvent()方法!
可是我怎么按那个按钮都没有反应,请问我的程序应该怎么改一下?
附源程序:
import com.motorola.lwt.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class MyLWTDemo extends MIDlet {
public Display display = Display.getDisplay(this);
DemoScreen screens = new ButtonScreen(display);
public MyLWTDemo() {}
protected void startApp()throws MIDletStateChangeException {
display.setCurrent(screens);
}
protected void pauseApp(){}
protected void destroyApp(boolean unconditional)
throws MIDletStateChangeException{}
}
/**
* Superclass for all of the demo screens, provides the next/previous commands
**/
class DemoScreen extends ComponentScreen {
/**
* Creates a new DemoScreen, creates and adds the next and previous commands
**/
public DemoScreen() {
Command next = new Command("Next", Command.OK, 1);
Command prev = new Command("Previous", Command.BACK, 1);
addCommand(next);
addCommand(prev);
}
}
/**
* Demo Screen for the Button Component. This screen shows the LWT Button component
* in a variety of sizes and in the disabled state.
**/
class ButtonScreen extends DemoScreen implements ComponentListener,CommandListener{
private Button b1;
private Display display;
public ButtonScreen(Display display) {
this.display = display;
// Add a button to the screen
b1 = new Button("Button");
add(b1);
this.addCommand(new Command("abc",Command.SCREEN,1));
this.setCommandListener(this);
//我在这里加了一个Command,是可以实现的
this.set
// Add another button to the screen
Button b2 = new Button("Large Button");
add(b2);
// Extend the right edge to the right edge of the screen
b2.setRightEdge(Component.SCREEN_RIGHT, 0);
// Make it twice its preferred height
b2.setBottomEdge(Component.HEIGHT, b2.getPreferredHeight() * 2);
// Add a disabled button
Button b3 = new Button("Disabled Button");
b3.setEnabled(false);
add(b3);
}
public void processComponentEvent(java.lang.Object source, int eventType){
Alert alarmAlert= new Alert("警报","这是一个"警报"信息窗口",null,AlertType.ALARM);
alarmAlert.setTimeout(2000);
display.setCurrent(alarmAlert);
}
public void commandAction(Command c, Displayable d){
Alert alarmAlert= new Alert("警报","这是一个"警报"信息窗口",null,AlertType.ALARM);
alarmAlert.setTimeout(2000);
display.setCurrent(alarmAlert);
}
}
是不是我没用setXXXXListener()的缘故?
|
1、问一下,屏幕居中是motorola扩展的接口吗?
2、我改写了一下,你试试看
class ButtonScreen
extends DemoScreen
implements ComponentListener,CommandListener
{
private Button b1;
private Display display;
private Button b2;
private Button b3;
public ButtonScreen(Display display)
{
this.display = display;
// Add a button to the screen
b1 = new Button("Button");
add(b1);
this.addCommand(new Command("abc",Command.SCREEN,1));
b2 = new Button("Large Button");
add(b2);
// Extend the right edge to the right edge of the screen
b2.setRightEdge(Component.SCREEN_RIGHT, 0);
// Make it twice its preferred height
b2.setBottomEdge(Component.HEIGHT, b2.getPreferredHeight() * 2);
// Add a disabled button
b3 = new Button("Disabled Button");
b3.setEnabled(false);
add(b3);
this.setCommmandListener(new CommandListener()
{
public void commandAction(Command c, Displayable d)
{
commandPerformed(c,d);
}
});
}
public void commandPerformed(Command c,Displayable d)
{
try
{
if(c == b1)
{
//按下b1触发的事件,以下类同
}
else if(c == b2)
{
}
else if(c == b3)
{
}
}
catch(Exception e)
{
}
}
}
2、我改写了一下,你试试看
class ButtonScreen
extends DemoScreen
implements ComponentListener,CommandListener
{
private Button b1;
private Display display;
private Button b2;
private Button b3;
public ButtonScreen(Display display)
{
this.display = display;
// Add a button to the screen
b1 = new Button("Button");
add(b1);
this.addCommand(new Command("abc",Command.SCREEN,1));
b2 = new Button("Large Button");
add(b2);
// Extend the right edge to the right edge of the screen
b2.setRightEdge(Component.SCREEN_RIGHT, 0);
// Make it twice its preferred height
b2.setBottomEdge(Component.HEIGHT, b2.getPreferredHeight() * 2);
// Add a disabled button
b3 = new Button("Disabled Button");
b3.setEnabled(false);
add(b3);
this.setCommmandListener(new CommandListener()
{
public void commandAction(Command c, Displayable d)
{
commandPerformed(c,d);
}
});
}
public void commandPerformed(Command c,Displayable d)
{
try
{
if(c == b1)
{
//按下b1触发的事件,以下类同
}
else if(c == b2)
{
}
else if(c == b3)
{
}
}
catch(Exception e)
{
}
}
}
|
按名子 监听Button
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。