public interface IMenuItemListener {
public abstract void setTask();
}
z自定义的抽象事件,因为接口可以当成对象来使用,只不过自使用时需重写其中 的抽象方法。自定义的菜单响应事件接口。
import com.sun.lwuit.Button;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Image;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
public class IMenuItem extends Button{
private IMenuItemListener menuItemListener=null;
public IMenuItem(){
super();
run();
}
public IMenuItem(String title){
super(title);
run();
}
public IMenuItem(Image icon){
super(icon);
run();
}
public IMenuItem(String text, Image icon){
super(text,icon);
run();
}
private void run(){
this.getStyle().setBorder(null);
this.getPressedStyle().setBorder(null);
this.getSelectedStyle().setBorder(null);
this.getPressedStyle().setBgColor(0X53123);
this.getSelectedStyle().setBgColor(0Xbcbcbc);
this.getStyle().setMargin(0, 0, 0,0);
this.getSelectedStyle().setMargin(0, 0,0,0);
this.getPressedStyle().setMargin(0, 0,0,0);
this.setTextPosition(Button.LEFT);
this.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
Dialog dialog=(Dialog)((Button)evt.getSource()).getComponentForm();
if(dialog!=null)
dialog.dispose(); //释放所在的资源
if(menuItemListener!=null)
menuItemListener.setTask();
}
});
}
public void addMenuItemListener(IMenuItemListener Itemlistener){
menuItemListener=Itemlistener;
}
}
/**********自定义的菜单项继承Button***********/
import java.util.Vector;
import com.peptalk.kaikai.client.ui.BaseUiData;
import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Display;
import com.sun.lwuit.Image;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BoxLayout;
public class IMenu extends Command{
private int height=0;
private int width=0;
private Vector vec=new Vector();
private Dialog dialog=null;
private BoxLayout lay=new BoxLayout(BoxLayout.Y_AXIS);
private boolean orgion=RIGHT;
public static final boolean LEFT=false;
public static final boolean RIGHT=true;
public IMenu(String title){
super(title);
dialog=new Dialog(){
public void menuDisplose() { //重写按键释放方法
this.dispose();
}
};
dialog.setTransitionInAnimator(BaseUiData.getInstance().getSlideLeft());
dialog.setTransitionOutAnimator(BaseUiData.getInstance().getSlideTop());
}
public void actionPerformed(ActionEvent evt) {
for (int i = 0; i<dialog.getComponentCount();i++) {
dialog.getComponentAt(i).getStyle().setBgTransparency(0);
}
dialog.setScrollableY(false);
showMenuItem();
}
private boolean isFlush=true;
public void flush(){
height=0;
width=0;
dialog.removeAll();
dialog.setLayout(lay);
for(int i=0;i<vec.size();i++){
IMenuItem item=(IMenuItem)vec.elementAt(i);
if(item!=null){
height+=item.getPreferredH();
width=Math.max(item.getPreferredW()+item.getBaselineResizeBehavior()*2,width);
dialog.addComponent(item);
}
}
isFlush=false;
}
private void restartWidth(){//重新计算宽度和高度
width=0;
for(int i=0;i<vec.size();i++){
IMenuItem item=(IMenuItem)vec.elementAt(i);
width=Math.max(item.getPreferredW()+item.getBaselineResizeBehavior()*2,width);
}
}
public void showMenuItem(){
if(isFlush)
flush();
if(orgion)
dialog.show(Display.getInstance().getDisplayHeight()-30-height,25,Display.getInstance().getDisplayWidth()-width,0,true);
else
dialog.show(Display.getInstance().getDisplayHeight()-30-height,25,2,Display.getInstance().getDisplayWidth()-width-8,true);
}
public void addMenuItem(IMenuItem item){//添加菜单
if(!vec.contains(item)&&!dialog.contains(item)){
vec.addElement(item);
dialog.addComponent(item);
}
}
public void removeAllMenuItem(){
vec.removeAllElements();
dialog.removeAll();
}
public void addMenuItem(IMenuItem item,int index){//把菜单添加到指定的位置
if(!vec.contains(item)&&!dialog.contains(item)){
vec.insertElementAt(item, index);
dialog.addComponent(index, item);
}
}
public void removeMenuItem(int index){//删除指定位置的菜单
if(vec.size()>index&&index>-1){
IMenuItem tempItem=(IMenuItem)vec.elementAt(index);
height-=tempItem.getPreferredH();
restartWidth();
dialog.removeComponent(tempItem);
vec.removeElementAt(index);
}
}
public void removeMenuItem(IMenuItem obg){ //删除菜单
if(dialog.contains(obg)){
height-=obg.getPreferredH();
dialog.removeComponent(obg);
if(vec.contains(obg))
vec.removeElement(obg);
restartWidth();
}
}
public int getMenuItemIndex(IMenuItem item){//得到菜单的位置
if(dialog.contains(item))
return dialog.getComponentIndex(item);
return -1;
}
public void setMenuItem(boolean oright){
orgion=oright;
}
}
菜单根类,
public void menuDisplose() { //重写按键释放方法
是自己偷懒在Form类中假的一个空方法,在子类中重写就ok了
Form中加的方法如下,主要响应某一个特殊的按键代码如下:
public void menuDisplose(){//菜单隐藏自定义方法
}
/**
* @inheritDoc
*/
public void keyReleased(int keyCode) {
int game = Display.getInstance().getGameAction(keyCode);
if(keyCode == rightSK ){
menuDisplose();//菜单消失
}
if (keyCode == leftSK || (keyCode == rightSK || keyCode == rightSK2) || keyCode == backSK ||
(keyCode == clearSK && clearCommand != null) ||
(keyCode == backspaceSK && clearCommand != null) ||
(Display.getInstance().isThirdSoftButton() && game == Display.GAME_FIRE)) {
menuBar.keyReleased(keyCode);
return;
}
//Component focused = focusManager.getFocused();
if (focused != null) {
if (focused.getComponentForm() == this) {
focused.keyReleased(keyCode);
}
}
// prevent the default action from stealing the behavior from the popup/combo box...
if (game == Display.GAME_FIRE) {
Command defaultCmd = getDefaultCommand();
if (defaultCmd != null) {
defaultCmd.actionPerformed(new ActionEvent(defaultCmd, keyCode));
actionCommandImpl(defaultCmd);
}
}
fireKeyEvent(keyListeners, keyCode);
fireKeyEvent(gameKeyListeners, game);
}