J2ME GUI实战之二 ----------LWUIT实现手机九宫图
2009-09-12 00:00:00 来源:WEB开发网首先,想让大家看看九宫图实现之后的界面:
本文所用的代码,是来自LWUIT所提供的Sample例子,但是对例子做出以下修改:
1.设计自主的res资源文件
2.把builde.xml里面的<target name="post-compile"> 。</target> 删除
3.把所用的res文件放到一个名为res的文件夹,并与源代码放在一起
4.把ResourceBuilder.jar也删除,util文件夹里的ResourceBuilder.jar绝对不能删,是用于设计res文件
针对以上4点,说明一下为什么这么做:
原Sample例子的res文件,是使用util文件夹里的ResourceEdit.exe设计,而且还在builde.xml中使用ResourceBuilder.jar再重新生成一次res文件并与源文件组合,这显然有点冗余,因为直接用ResourceEdit.exe设计的res文件放到源文件目录里即可。也许Sample作者直接使用ANT编译或者是其他IDE要求,本文所用的IDE为netbeans。
OK,接下来就是源代码修改了,Sample源代码可是写得很长很长,要看也得花不少时间,但是,我保证大家看完后一定领略到JAVA大牛的编程功力之深!!!为了方便以后使用,所以我把Sample中实现九宫图和一些小操作的代码保留,其他都去掉了,请JAVA大牛切勿见笑哈。
以下给出关键代码:
/*
* Copyright ? 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
*/
package com.sun.lwuit.uidemo;
import com.sun.lwuit.Button;
import com.sun.lwuit.animations.CommonTransitions;
import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.animations.Transition;
import com.sun.lwuit.animations.Transition3D;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.events.FocusListener;
import com.sun.lwuit.layouts.GridLayout;
import com.sun.lwuit.plaf.Style;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.util.Resources;
public class UIDemoMIDlet extends javax.microedition.midlet.MIDlet implements ActionListener {
private static final Command exitCommand = new Command("Exit", 1);
private String[] btn_name = new String[]{//九宫图的名称,同时也是图标的名称
"Image 1","Image 2","Image 3","Image 4","Image 5","Image 6","Image 7",
"Image 8","Image 9"
};
private static Transition componentTransitions;
private static Form mainMenu;//主窗体
private int cols;
private int elementWidth;
protected void startApp() {
try {
Display.init(this);
Resources r = Resources.open("/res/resources.res");//读取资源文件
setMainForm(r);
} catch (Throwable ex) {
ex.printStackTrace();
Dialog.show("Exception", ex.getMessage(), "OK", null);//hellogv最爱的提示框弹出方式
}
}
protected void pauseApp() {
}
protected void destroyApp(boolean arg0) {
}
public static void setMenuTransition(Transition in, Transition out) {
mainMenu.setMenuTransitions(in, out);
UIManager.getInstance().getLookAndFeel().setDefaultMenuTransitionIn(in);
UIManager.getInstance().getLookAndFeel().setDefaultMenuTransitionOut(out);
}
public static void setComponentTransition(Transition t) {
componentTransitions = t;
mainMenu.setSmoothScrolling(false);
}
public static Transition getComponentTransition() {
return componentTransitions;
}
public static void backToMainMenu(){
mainMenu.refreshTheme();
mainMenu.show();
// for series 40 devices
System.gc();
System.gc();
}
private void setMainForm(Resources r) {
//设置主题
UIManager.getInstance().setThemeProps(r.getTheme("businessTheme"));
mainMenu = new MainScreenForm("LWUIT Demo") ;
int width = Display.getInstance().getDisplayWidth(); //取得全屏的宽度
elementWidth = 0;
//效果,可以实现类似IPhone、S1智能机的Touch Flo的动态窗体切换效果!**********************
mainMenu.setTransitionOutAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_HORIZONTAL, false, 500));
mainMenu.setTransitionInAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_HORIZONTAL, true, 500));
UIDemoMIDlet.setComponentTransition(Transition3D.createCube(600, false));
//***************************************************************************************************************
Image[] selectedImages = new Image[btn_name.length];//按钮被选中时的图片
Image[] unselectedImages = new Image[btn_name.length];//按钮未被选中的图片
for (int i = 0; i < btn_name.length; i++) {
//---------读取选中时的图片
selectedImages[i] = r.getImage(btn_name[i]);
//--------读取未被选中的图片
unselectedImages[i] = r.getImage(btn_name[i]);
final Button button = new Button(btn_name[i], unselectedImages[i])
{
public Image getPressedIcon() {
Image i = getIcon();
return i.scaled((int) (i.getWidth() * 0.8), (int) (i.getHeight() * 0.8));
}
};
button.setRolloverIcon(selectedImages[i]);
Style s = button.getStyle();
s.setBorder(null);
s.setBgTransparency(0);
s.setBgSelectionColor(0xffffff);
button.setAlignment(Label.CENTER);
button.setTextPosition(Label.BOTTOM);
mainMenu.addComponent(button);
button.addActionListener(this);
//按钮焦点处理,用于实现按钮特效
button.addFocusListener(new FocusListener() {
public void focusGained(Component cmp) {//按钮取得焦点时的特效
if (componentTransitions != null) {
mainMenu.replace(button, button, componentTransitions);
}
}
public void focusLost(Component cmp) {}//按钮失去焦点时的特效
});
elementWidth = Math.max(button.getPreferredW(), elementWidth);
}
cols = width / elementWidth;
int rows = btn_name.length / cols;
mainMenu.setLayout(new GridLayout(rows, cols));//GridLayout为九宫图的排列方式
mainMenu.addCommand(exitCommand);
mainMenu.setCommandListener(this);
mainMenu.show();
}
/**
* 控件处理函数
*/
public void actionPerformed(ActionEvent evt) {
try{//通过异常处理来判断事件来源
Command cmd = evt.getCommand();
if(cmd.getCommandName().equals("Exit"))
notifyDestroyed();
}catch(Exception e)
{
String sel_button=((Button)(evt.getSource())).getText();//取得所选按钮的名称
if(sel_button.equals("Image 1"))
new AnimationDemo().form.show();
else if(sel_button.equals("Image 2"))
new PaintingDemo().form.show();
else if(sel_button.equals("Image 3"))
new LayoutDemo().form.show();
}
}
}
- ››实战:企业使用交换机VLAN路由配置
- ››实战案例分析:高质量软文对网站百度排名的影响
- ››实战经验浅谈网站搬家后的优化工作
- ››GUI库:使本机应用程序具备Windows窗体的简易性
- ››实战Active Directory站点部署与管理,Active Dir...
- ››实战操作主机角色转移,Active Directory系列之十...
- ››实战经验:巧用微博推广淘宝网店
- ››实战iPhone GPS定位系统
- ››实战Linux环境配置DBD:Oracle模块
- ››实战DeviceIoControl系列之一:通过API访问设备驱...
- ››实战DeviceIoControl系列之二:获取软盘/硬盘/光盘...
- ››实战DeviceIoControl系列之三:制作磁盘镜像文件
更多精彩
赞助商链接