国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

J2ME學習札記3

2019-11-17 06:19:50
字體:
來源:轉載
供稿:網友

  Ticker對象
  
   Ticker對象是一個項目類型的對象,它的作用相當于一個滾動消息欄,在屏幕的上方顯示滾動的信息。 Ticker類的構造函數僅有一個參數,那就是需要滾動顯示的消息。
  package fancy.test;
  
  import javax.microedition.midlet.*;
  import javax.microedition.lcdui.*;
  
  public class ShowTicker extends MIDlet implements CommandListener
  {
  PRivate Display display;
   private Form props;
  
  private Command exitCommand = new Command("Exit", Command.EXIT, 1);
  
  public ShowTicker()
  {
  display = Display.getDisplay(this);
   }
  
  public void startApp()
  {
  props = new Form("Hello World");
  props.append("Hello World!/n");
  Ticker ticker=new Ticker("С¥һҹ
  ;Ìý´ºÓê");
  props.setTicker(ticker);
  props.addCommand(exitCommand);
  props.setCommandListener(this);
  display.setCurrent(props);
   }
  
  public void commandAction(Command c, Displayable s)
  {
  if (c == exitCommand)
  {
  destroyApp(false);
   notifyDestroyed();
  }
   }
  
  public void destroyApp(boolean unconditional)
  {
   }
  
  public void pauseApp()
  {
  display.setCurrent(null);
  props = null;
   }
  
  }
   ShowTicker.java程序的運行效果如下圖所示:
  
  獲取文本框的值
  發信站: 北大未名站 (2001年10月21日00:34:19 星期天) , 站內信件
  
   在前面的例子中,我們已經演示了如何構造J2ME程序的用戶界面。現在有一個問題,那就是如何與用戶界面交互呢?亦即如何獲取用戶通過用戶界面輸入的值呢?請看下面的例子。
  package fancy.test;
  
  import javax.microedition.midlet.*;
  import javax.microedition.lcdui.*;
  
  public class GetTextBoxValue extends MIDlet implements CommandListener
  {
  private Display display;
   private TextBox txtBox;
  
  private Command exitCommand = new Command("Exit", Command.EXIT, 1);
  private Command getCommand = new Command("GETVALUE", Command.OK, 1);
  
  public GetTextBoxValue()
  {
  display = Display.getDisplay(this);
   }
  
  public void startApp()
  {
  //or :
  //String str="hello world";
  //txtBox = new TextBox("Text Box",str,str.length(),0);
  //the follow code is wrong:
  //txtBox = new TextBox("Text Box",str,any number here,0);
  
  txtBox = new TextBox("Text Box",null,200,0);
  
  txtBox.addCommand(exitCommand);
  txtBox.addCommand(getCommand);
  txtBox.setCommandListener(this);
  display.setCurrent(txtBox);
   }
  
  public void valueScreen()
  {
  Form props=new Form("get text box value");
  props.append(txtBox.getString());
  props.addCommand(exitCommand);
  props.setCommandListener(this);
  display.setCurrent(props);
  }
  
  public void commandAction(Command c, Displayable s)
  {
  if (c == exitCommand)
  {
  destroyApp(false);
   notifyDestroyed();
  }
  if(c==getCommand)
  {
  valueScreen();
  }
   }
  
  public void destroyApp(boolean unconditional)
  {
   }
  
  public void pauseApp()
  {
  display.setCurrent(null);
  txtBox = null;
   }
  
  }
   在上面的例子中(GetTextBoxValue.java),當我們往文本框中輸入文本,并按下退出按鈕,接著選擇GETVALUE命令的時候,將會調用valueScreen()方法。valueScreen()方法的源代碼如下
  :
  public void valueScreen()
  {
  Form props=new Form("get text box value");
  props.append(txtBox.getString());
  props.addCommand(exitCommand);
  props.setCommandListener(this);
  display.setCurrent(props);
  }
   valueScreen()方法的邏輯是:首先創建一個容器對象Form,然后調用TextBox對象的getString()方法,獲取文本框中的輸入值,追加到容器對象中,最后將此Form對象作為屏幕的當前顯示對象。GetTextBoxValue.java的運行效果如下面兩圖所示:
  
  Date對象
  發信站: 北大未名站 (2001年10月21日00:35:20 星期天) , 站內信件
  
   Date對象是屬于java.util包的,它的作用是返回當前的時間。請看下面的代碼:
  package fancy.test;
  
  import javax.microedition.midlet.*;
  import javax.microedition.lcdui.*;
  import java.util.*;
  
  public class GetDate extends MIDlet implements CommandListener
  {
  private Display display;
   private Form props;
  private Date date;
  
  private Command exitCommand = new Command("Exit", Command.EXIT, 1);
  
  public GetDate()
  {
  display = Display.getDisplay(this);
   }
  
  public void startApp()
  {
  props = new Form("Hello World");
  props.append("Hello World!/n");
  date=new Date();
  props.append("Now Time:"+date.getTime()+"/n");
  
  props.addCommand(exitCommand);
  props.setCommandListener(this);
  display.setCurrent(props);
   }
  
  public void commandAction(Command c, Displayable s)
  {
  if (c == exitCommand)
  {
  destroyApp(false);
   notifyDestroyed();
  }
   }
  
  public void destroyApp(boolean unconditional)
  {
   }
  
  public void pauseApp()
  {
  display.setCurrent(null);
  props = null;
   }
  
  }
   GetDate.java程序的運行效果如下圖所示:
  
  
  --
  TimeZone對象
  發信站: 北大未名站 (2001年10月21日00:36:16 星期天) , 站內信件
  
   TimeZone對象也是屬于java.util包的。這個對象的作用是提供關于時區的信息。TimeZon
  e類有一個靜態方法----getDefault(),可以獲取與當前系統相關的時區對象。getAvailable
  IDs()方法可以獲取系統中所有可用的時區的ID號,getID()方法可以獲取系統當前所設置的時區。具體的例子如下所示:
  package fancy.test;
  
  import javax.microedition.midlet.*;
  import javax.microedition.lcdui.*;
  import java.util.*;
  
  public class GetTimeZone extends MIDlet implements CommandListener
  {
  private Display display;
   private Form props;
  //private Date date;
  private TimeZone zone;
  
  private Command exitCommand = new Command("Exit", Command.EXIT, 1);
  
  public GetTimeZone()
  {
  display = Display.getDisplay(this);
   }
  
  public void startApp()
  {
  props = new Form("Hello World");
  props.append("Hello World!/n");
  //date=new Date();
  //props.append("Now Time:"+date.getTime()+"/n");
  zone=TimeZone.getDefault();
  String []zoneid=zone.getAvailableIDs();
  for(int i=0;i  {
  props.append(zoneid[i]+"/n");
  }
  props.append("Current Time Zone:"+zone.getID()+"/n");
  props.addCommand(exitCommand);
  props.setCommandListener(this);
  display.setCurrent(props);
   }
  
  public void commandAction(Command c, Displayable s)
  {
  if (c == exitCommand)
  {
  destroyApp(false);
   notifyDestroyed();
  }
   }
  
  public void destroyApp(boolean unconditional)
  {
   }
  
  public void pauseApp()
  {
  display.setCurrent(null);
  props = null;
   }
  
  }
   GetTimeZone.java程序的運行效果如下圖所示:
  
  
  --
  Calendar對象
  發信站: 北大未名站 (2001年10月21日00:37:43 星期天) , 站內信件
  
   Calendar對象歸屬于java.util包,它可以提供更為詳盡的時間信息。具體的例子如下所示

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 柘荣县| 扶风县| 杨浦区| 石台县| 尼玛县| 南丹县| 永济市| 大足县| 双鸭山市| 柏乡县| 桐柏县| 平定县| 建平县| 法库县| 双峰县| 松江区| 稻城县| 咸宁市| 乌兰浩特市| 仁化县| 盘山县| 如东县| 平南县| 门源| 衡南县| 集安市| 磐石市| 康保县| 左权县| 浦东新区| 铜陵市| 如皋市| 榆社县| 那坡县| 廊坊市| 苏尼特左旗| 拜泉县| 淮阳县| 毕节市| 宁乡县| 肇州县|