Mobile 如何使用LWUIT在单击按钮时显示图像
我正在尝试使用LWUIT编写一个应用程序,其中我希望单击按钮即可显示图像。 我有以下代码。但是我得到一个例外,如果按钮被点击两次。 请帮助我显示图像,没有任何例外Mobile 如何使用LWUIT在单击按钮时显示图像,mobile,java-me,lwuit,lwuit-form,lwuit-button,Mobile,Java Me,Lwuit,Lwuit Form,Lwuit Button,我正在尝试使用LWUIT编写一个应用程序,其中我希望单击按钮即可显示图像。 我有以下代码。但是我得到一个例外,如果按钮被点击两次。 请帮助我显示图像,没有任何例外 final Form f = new Form("Static TAF"); Button TrackMe = new Button("TrackMe"); Image TrackMeicon = null; TrackMeicon = Image.createIma
final Form f = new Form("Static TAF");
Button TrackMe = new Button("TrackMe");
Image TrackMeicon = null;
TrackMeicon = Image.createImage("/hello/follow.jpeg");
final Label TrackMeLabel = new Label(TrackMeicon);
TrackMe.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
System.out.println("Removing the previous Images");
f.addComponent(TrackMeLabel);
}
});
请帮助当您第一次单击按钮时,图像将添加到表单中。当您第二次单击时,该图像已存在于表单中。因此,它将抛出
“组件已存在”
异常
final Form f = new Form("Static TAF");
Button TrackMe = new Button("TrackMe");
Image TrackMeicon = null;
TrackMeicon = Image.createImage("/hello/follow.jpeg");
final Label TrackMeLabel = new Label(TrackMeicon);
TrackMe.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
System.out.println("Removing the previous Images");
f.addComponent(TrackMeLabel);
}
});
你的行动听众应该
TrackMe.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
System.out.println("Removing the previous Images");
f.removeComponent(TrackMeLabel);
f.addComponent(TrackMeLabel);
}
});
当您第一次单击按钮时,图像将添加到表单中。当您第二次单击时,该图像已存在于表单中。因此,它将抛出
“组件已存在”
异常
final Form f = new Form("Static TAF");
Button TrackMe = new Button("TrackMe");
Image TrackMeicon = null;
TrackMeicon = Image.createImage("/hello/follow.jpeg");
final Label TrackMeLabel = new Label(TrackMeicon);
TrackMe.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
System.out.println("Removing the previous Images");
f.addComponent(TrackMeLabel);
}
});
你的行动听众应该
TrackMe.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
System.out.println("Removing the previous Images");
f.removeComponent(TrackMeLabel);
f.addComponent(TrackMeLabel);
}
});
}))
})) 如果只想添加一个图像,可以使用以下方法: 如果你想要一些图片,你需要这样的东西:
如果只想添加一个图像,可以使用以下方法: 如果你想要一些图片,你需要这样的东西:
很抱歉告诉您,此代码不适用于f.removeComponent(TrackMeLabel);这一行永久性地删除了该组件,并且永远不会将其添加到下一行代码中。除了显示带有标签的图像。。。没有其他逻辑吗?根据我的经验,最好只向LWUIT表单中添加一个组件一次,或者使用removeAll()然后重新添加它们。感谢Ajibola和Kalai给出的宝贵答案。请查看我在lwuit命令标签下发布的其他问题。我需要这方面的帮助。很抱歉告诉你,这段代码不适用于f.removeComponent(TrackMeLabel);这一行永久性地删除了该组件,并且永远不会将其添加到下一行代码中。除了显示带有标签的图像。。。没有其他逻辑吗?根据我的经验,最好只向LWUIT表单中添加一个组件一次,或者使用removeAll()然后重新添加它们。感谢Ajibola和Kalai给出的宝贵答案。请查看我在lwuit命令标签下发布的其他问题。我需要关于.f.removeAll()的帮助,只有当TrackMeLabel是表单中唯一的组件时,它才会起作用。如果表单f中已有一些组件,则表示也将删除这些组件。只有当TrackMeLabel是表单中唯一的组件时,f.removeAll()才会起作用。如果表格f中的某些组件已经存在,则表示该组件也将被删除。