设计可组装的j2me UI(六) Dialog(对话框)

      高级UI提供了一个Alert的控件可以有弹出对话框的效果。但是大家想不想自己实现一个呢。想不想知道sun是如何让Alert工作的呢?好请看下文
     设计思想是。建立一个 abstract   class Dialog extends Canvas。下面的事情就让我们一步步花出来吧。
     实现理念是先绘制整个Canvas然后通过图形Graphics描绘成透明,接着在整个屏幕的中间去出一块区域来,至于上面你要做什么那就是你的事情了。哈。
    好看代码,
java 代码
 
  1. /******************************************************************** 
  2.  *  
  3.  * 版权说明,此程序仅供学习参考。不能用于商业 
  4.  *  
  5.  ********************************************************************/  
  6. package org.pook.ui.form;  
  7.   
  8. import java.util.TimerTask;  
  9.   
  10. import javax.microedition.lcdui.Canvas;  
  11. import javax.microedition.lcdui.Display;  
  12. import javax.microedition.lcdui.Font;  
  13. import javax.microedition.lcdui.Graphics;  
  14.   
  15. import org.pook.log.Log;  
  16. import org.pook.ui.Command;  
  17. import org.pook.ui.SoftButton;  
  18. import org.pook.ui.core.Platform;  
  19. import org.pook.ui.event.CommandListener;  
  20. import org.pook.ui.timer.TimerTaskManager;  
  21.   
  22.    
  23.   
  24. /** 
  25.  * 类名:Dialog.java 
     编写日期: 2006-9-16 
     
  26.  * 程序功能描述:弹出窗�?,因为足球项目要求很多的弹出窗口的形式不同�?�? 定义�?个抽象的弹出窗口,把基本的功能再这个实�?,以下的�?�么绘制则交给具体的要求 
  27.  * 
     Demo: 
     Bug: 
     
  28.  *  
  29.  * 程序变更日期 �?
     变更作�?? �?
     变更说明 �?
     
  30.  *  
  31.  * @author wuhua 
     
     
  32.  */  
  33. public abstract   class Dialog extends Canvas implements Runnable {  
  34.     private static Log log = Log.getLog("Dialog");  
  35.   
  36.     protected final int X = 0;  
  37.   
  38.     protected final int Y = 1;  
  39.   
  40.     protected   final int WIDTH = 2;  
  41.   
  42.     protected final int HEIGHT = 3;  
  43.   
  44.     /** 显示主要部分.比如菜单的Icon,List的数据的位置 */  
  45.     protected int[] view = new int[4];  
  46.   
  47.     /** Preset alternative containing OK */  
  48.     public static final int DIALOG_OK = 0;  
  49.   
  50.     /** Preset alternative containing CANCEL = 1 */  
  51.     public static final int DIALOG_CANCEL = 2;  
  52.   
  53.     /** Preset alternatives containing YES and NO */  
  54.     public static final int DIALOG_YES_NO = 3;  
  55.   
  56.     /** Preset alternatives containing OK and CANCEL */  
  57.     public static final int DIALOG_OK_CANCEL = 4;  
  58.   
  59.     /** Preset alternatives containing YES, NO, and CANCEL */  
  60.     public static final int DIALOG_YES_NO_CANCEL = 5;  
  61.   
  62.     public static final int DIALOG_TEXT = 6;  
  63.   
  64.     /** 
  65.      * 保存当前窗口,主要用�?�是,当前对话筐消失的时�??,要求重绘�?. 永远刷新父类 
  66.      */  
  67.     protected Panel parent;  
  68.   
  69.     protected boolean hasFocus;  
  70.   
  71.     /** 
  72.      * 定义超时参数 
  73.      */  
  74.     long timeOut;  
  75.   
  76.     /** 
  77.      * 对话框类�? 
  78.      */  
  79.     int type;  
  80.   
  81.     /** 对话框标�? */  
  82.     protected String title;  
  83.   
  84.     /** 
  85.      * 把单行标题转换为多行以方便显�? 
  86.      */  
  87.     protected   String[] rowTitle;  
  88.   
  89.     protected int bgColor;  
  90.   
  91.     protected int fontColor;  
  92.   
  93.     protected Font font = Font.getDefaultFont();  
  94.   
  95.     /** 用于执行消失窗口 */  
  96.     protected TimerTask task;  
  97.   
  98.     protected Display display;  
  99.   
  100.     protected SoftButton softButton;  
  101.       
  102.    
  103.    
  104.       
  105.       
  106.     /** 
  107.      * 构�?�一个默认的标题,父类的窗�? 
  108.      *  
  109.      * @param title 
  110.      * @param parent 
  111.      */  
  112.     public Dialog(String title, Panel parent, Display display) {  
  113.         this(title, parent, display, 3000);  
  114.     }  
  115.   
  116.     /** 
  117.      * 构�?�一个指定时间的构�?�窗�? 
  118.      *  
  119.      * @param title 
  120.      * @param parent 
  121.      * @param timeOut 
  122.      */  
  123.     public Dialog(String title, Panel parent, Display display, long timeOut) {  
  124.         this(title, parent, display, Dialog.DIALOG_OK, timeOut);  
  125.     }  
  126.   
  127.     /** 
  128.      * 构�?�一个指定窗口类�?,时间的窗�? 
  129.      *  
  130.      * @param title 
  131.      * @param parent 
  132.      * @param type 
  133.      * @param timeOut 
  134.      */  
  135.     public Dialog(String title, Panel parent, Display display, int type,  
  136.             long timeOut) {  
  137.         setFullScreenMode(true);  
  138.           
  139.         this.parent = parent;  
  140.         checkParent();  
  141.           
  142.         this.timeOut = timeOut;  
  143.         this.type = type;  
  144.         this.title = title;  
  145.         this.display = display;  
  146.           
  147.         softButton = new SoftButton();  
  148.           
  149.        
  150.           
  151.         if (timeOut != 0)  
  152.             task = TimerTaskManager.getInstace().add(this, timeOut);   
  153.           
  154.         //设置默认的风�?  
  155.         setStyle(0x0033FF0xFFFFFF);  
  156.           
  157.     }  
  158.   
  159.     public void setStyle(int bgColor, int fontColor) {  
  160.         this.bgColor = bgColor;  
  161.         this.fontColor = fontColor;  
  162.     }  
  163.   
  164.     public void cancel() {  
  165.         //log.debug("Stop...");  
  166.         if (parent == null)  
  167.             throw new NullPointerException("Parent is not Null.");  
  168.           
  169.         task.cancel();  
  170.         if(parent == null)  
  171.             return;  
  172.           
  173.         display.setCurrent(parent);  
  174.            
  175.     }  
  176.   
  177.     public void addCommand(Command cmd) {  
  178.         this.softButton.addCommand(cmd);  
  179.     }  
  180.   
  181.     public void setSoftButtonListener(CommandListener cml) {  
  182.         this.softButton.setCommandListener(cml);  
  183.     }  
  184.       
  185.     public void setSoftButtonStyle(int bgColor, int fontColor){  
  186.         this.softButton.setStyle(bgColor, fontColor);  
  187.     }  
  188.   
  189.     public void run() {  
  190.         cancel();  
  191.   
  192.     }  
  193.   
  194.     /** 绘制透明�? * */  
  195.    public   void drawRGB(Graphics g) {  
  196.         int ai[] = new int[Platform.WIDTH];  
  197.         for (int j1 = 0; j1 < ai.length; j1++)  
  198.             ai[j1] = 0x90000000;  
  199.         g.drawRGB(ai, 0000, Platform.WIDTH, Platform.HEIGHT, true); // 绘制透明景色  
  200.     }  
  201.   
  202.        
  203.        
  204.   
  205.   
  206.     private void checkParent() {  
  207.         if (parent == null){  
  208.              throw new NullPointerException("Parent is not null");  
  209.         }  
  210.               
  211.     }  
  212.   
  213.     protected void keyPressed(int keyCode) {  
  214.         softButton.onClick(keyCode);  
  215.           
  216.     }  
  217.   
  218.     public void setPanel(Panel panel) {  
  219.         this.parent = panel;  
  220.           
  221.     }  
  222.   
  223.     public void setTimeOut(long timeOut) {  
  224.         this.timeOut = timeOut;  
  225.     }  
  226.   
  227.     public int getType() {  
  228.         return type;  
  229.     }  
  230.   
  231.     public void setType(int type) {  
  232.         this.type = type;  
  233.     }   

下面这段是弹出一个对话框实现
java 代码
 
  1. /******************************************************************** 
  2.  *  
  3.  * 版权说明,此程序仅供学习参考。不能用于商业 
  4.  *  
  5.  ********************************************************************/  
  6. package org.pook.ui.form;  
  7.   
  8. import java.util.Vector;  
  9.   
  10. import javax.microedition.lcdui.Font;  
  11. import javax.microedition.lcdui.Graphics;  
  12.   
  13. import org.pook.Pook;  
  14. import org.pook.log.Log;  
  15. import org.pook.ui.Command;  
  16. import org.pook.ui.SelectedPart;  
  17. import org.pook.ui.core.Platform;  
  18. import org.pook.ui.event.CommandListener;  
  19. import org.pook.ui.util.FontUtil;  
  20. import org.pook.ui.util.GraphicsUtil;  
  21. import org.pook.util.StringUtil;  
  22.    
  23.    
  24. /** 
  25.  * 类名:MessageDialog.java 
     
     
  26.  * 编写日期: 2006-9-26 
     
  27.  * 程序功能描述: 
     
  28.  * Demo: 
     
  29.  * Bug: 
     
  30.  *  
  31.  * 程序变更日期 :
     
     
  32.  * 变更作者 :
     
     
  33.  * 变更说明 :
     
  34.  *  
  35.  * @author wuhua 
     
     
  36.  */  
  37. public class MessageDialog extends TextDialog implements CommandListener{  
  38.     private static Log log = Log.getLog("MessageDialog");  
  39.       
  40.       
  41.     private Command ok;   
  42.       
  43.     private  SelectedPart plan;  
  44.     private int  planHeight;  
  45.    
  46.     private int contentHeight;  
  47.    
  48.     private int viewSize;  
  49.     private int space;  
  50.       
  51.    
  52.     private Vector content;  
  53.     private int selectIndex;  
  54.     private int contentY;  
  55.       
  56.     public MessageDialog(String _title,String _message, long _timeOut) {  
  57.         super(_title,  Pook.MAINMENU, Pook.getDisplay(), _timeOut);  
  58.         ok = new Command("确定",Command.LEFT, Command.FIRST_PRIORITY);  
  59.         this.addCommand(ok);  
  60.         this.setSoftButtonListener(this);  
  61.         this.setMessage(_message);  
  62.         initView();  
  63.         this.setStyle(0x001D680xFFFFFF);  
  64.         this.setSoftButtonStyle(0x0071BD,0xFFFFFF);  
  65.         this.setType(Dialog.DIALOG_OK);  
  66.     }  
  67.       
  68.     public void setMessage(String message){  
  69.    
  70.         skipMessage(message);  
  71.     }  
  72.       
  73.   
  74.     private void skipMessage(String message) {  
  75.           
  76.         if(message == null)  
  77.             return ;  
  78.           
  79.         content = new Vector();  
  80.         String[] text = StringUtil.split(message, "\n");  
  81.           
  82.         for(int j =0; j < text.length; j++){  
  83.            
  84.             String[] t = FontUtil.splitOnlyStringToRowString(font,text[j],view[WIDTH]-6);  
  85.             for(int i=0; i
  86.                 content.addElement(t[i]);      
  87.             }  
  88.             ///content.addElement("");  
  89.         }  
  90.           
  91.         //log.debug(content.size());  
  92.           
  93.         //new Plan  
  94.        
  95.           
  96.            
  97.           
  98.           
  99.     }  
  100.   
  101.     protected void keyPressed(int keyCode) {  
  102.         super.keyPressed(keyCode);  
  103.         this.thisUpAndDown(keyCode);  
  104.         this.repaint();  
  105.     }  
  106.       
  107.     /** 
  108.      * 上下事件 
  109.      *  
  110.      * @param keyCode 
  111.      */  
  112.     private void thisUpAndDown(int keyCode) {  
  113.           
  114.         if (this.content == null)  
  115.             return;  
  116.           
  117.         if(this.content.size() < this.viewSize)  
  118.             return;  
  119.           
  120.         switch (keyCode) {  
  121.   
  122.         case Platform.KEY_UP: {  
  123.   
  124.              selectIndex =  selectIndex <= 0 ? content.size() - viewSize - 1  
  125.                     : --selectIndex;  
  126.             // log.debug("Key");  
  127.             break;  
  128.   
  129.         }  
  130.         case Platform.KEY_DOWN: {  
  131.              selectIndex =  selectIndex >=content.size() - viewSize - 1 ? 0  
  132.                     : ++selectIndex;  
  133.              //log.debug("Key1");  
  134.   
  135.             break;  
  136.         }  
  137.         }  
  138.     }  
  139.   
  140.   
  141.     public void commandAction(Command cmd) {  
  142.         if(cmd == ok)  
  143.             this.cancel();  
  144.           
  145.     }  
  146.     protected void paint(Graphics g) {  
  147.         parent.paint(g);  
  148.         drawRGB(g);  
  149.         this.softButton.paint(g);  
  150.         this.paintMessageDialog(g);  
  151.           
  152.     }  
  153.   
  154.   
  155.   
  156.     private void paintMessageDialog(Graphics g) {  
  157.         initView();  
  158.         g.setFont(font);  
  159.         GraphicsUtil.drawRectWithColor(g,0xFFD84F, view[X]-1,view[Y]-1,view[WIDTH]+1,view[HEIGHT]+1);  
  160.         paintTitleImpl(g);  
  161.         paintContent(g);  
  162.         paintState(g);  
  163.     }  
  164.       
  165.     private void paintTitleImpl(Graphics g) {  
  166.         g.setColor(0x0071BC);  
  167.         g.fillRect(view[X],view[Y],view[WIDTH],font.getHeight() + 2);  
  168.         g.setColor(0xFFFFFF);  
  169.         g.drawString(title, view[X] + 4, view[Y] + 1,Graphics.TOP|Graphics.LEFT);  
  170.     }  
  171.       
  172.     private void paintContent(Graphics g) {  
  173.         if(content.size() > viewSize ){  
  174.                 this.plan = new SelectedPart();  
  175.                 plan.setStyle(0xFFFFFF0x000000);  
  176.                 this.planHeight = contentHeight/(content.size() - viewSize);  
  177.         }  
  178.         contentY = view[Y] + font.getHeight() + 4;  
  179.            
  180.         g.setColor(0x001D68);  
  181.         g.fillRect(view[X],contentY,view[WIDTH],contentHeight);  
  182.    
  183.         paintContentImpl(g);  
  184.           
  185.     }  
  186.       
  187.     private void paintContentImpl(Graphics g) {  
  188.         if(content == null)  
  189.             return ;  
  190.       
  191.         int size = viewSize>content.size()?content.size():viewSize + selectIndex;  
  192.           
  193.         g.setColor(0xFFFFFF);  
  194.           
  195.         for(int i = selectIndex; i
  196.             g.drawString((String) content.elementAt(i),view[X] + 2, contentY + space * (i-selectIndex), Graphics.TOP|Graphics.LEFT);  
  197.         }  
  198.           
  199.         /** 
  200.          * 绘制状态条 
  201.          */  
  202.         if(plan != null){  
  203.             //log.debug("SelectIndex = " + selectIndex);  
  204.             plan.changePosition(view[WIDTH] + 4 , contentY + this.planHeight *selectIndex, 4,this.planHeight);  
  205.             plan.paint(g);  
  206.         }  
  207.     }  
  208.   
  209.     private void paintState(Graphics g) {  
  210.         int height = Platform.HEIGHT - 30 - font.getHeight() - 2;  
  211.         g.setColor(0x0071BC);  
  212.         g.fillRect(view[X], height  ,view[WIDTH],font.getHeight() + 2);  
  213.         g.setColor(0xFFFFFF);  
  214.         g.drawLine(view[X],height,view[WIDTH] + 10, height);  
  215.         g.setColor(0xFFFFFF)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值