路是爬出來的

          [導入] 設計可組裝的j2me UI(六) Dialog(對話框)

                高級UI提供了一個Alert的控件可以有彈出對話框的效果。但是大家想不想自己實現(xiàn)一個呢。想不想知道sun是如何讓Alert工作的呢?好請看下文

               設計思想是。建立一個 abstract   class Dialog extends Canvas。下面的事情就讓我們一步步花出來吧。

               實現(xiàn)理念是先繪制整個Canvas然后通過圖形Graphics描繪成透明,接著在整個屏幕的中間去出一塊區(qū)域來,至于上面你要做什么那就是你的事情了。哈。

              好看代碼,

          java 代碼


           


          1. /******************************************************************** 

          2.  *  

          3.  * 版權說明,此程序僅供學習參考。不能用于商業(yè) 

          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的數(shù)據(jù)的位置 */  

          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.      * 定義超時參數(shù) 

          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.     /** 用于執(zhí)行消失窗口 */  

          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.     }   






          下面這段是彈出一個對話框?qū)崿F(xiàn)

          java 代碼


           


          1. /******************************************************************** 

          2.  *  

          3.  * 版權說明,此程序僅供學習參考。不能用于商業(yè) 

          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.          * 繪制狀態(tài)條 

          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)



          文章來源: http://wuhua.javaeye.com/blog/35933

          posted on 2006-12-30 08:42 路是爬出來的 閱讀(179) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 确山县| 翁牛特旗| 三江| 阿克| 岱山县| 进贤县| 镇原县| 江源县| 建瓯市| 麟游县| 绵阳市| 吉木乃县| 秀山| 琼海市| 安达市| 胶州市| 仁布县| 岳阳县| 菏泽市| 社会| 高台县| 饶平县| 永春县| 清苑县| 青河县| 荔浦县| 石泉县| 绥宁县| 中超| 宜黄县| 平罗县| 福建省| 宣汉县| 富锦市| 武城县| 白银市| 元江| 贺兰县| 日土县| 万山特区| 玛多县|