J2ME常用控制項說明

2022-04-03 07:22:26 字數 3863 閱讀 9138

form與item

form是惟一可以在介面上新增元件的類。

form form = new form("視窗標題")

從item繼承的子類都可以新增到form中。(midp2.0)1. stringitemstringitem("標題", "文字")//顯示為: 標題文字stringitem("標題", "文字", 型別)型別:item.plain-- 普通item.button-- 按鈕item.hyperlink --超連結button、hyperlink型別需要setdefaultcommand(cmd)來設定乙個預設的按鈕元件,並用setitemcommandlistener(midlet) 《注:不是setcommandlistener,因此需要引用itemcommandlistener介面》

2. textfieldtextfield("標籤", "初始文字", 最大長度, 型別)型別:同textbox (textbox是screen的子類)getstring()方法取文字框中的值。

3. datefield日期、時間輸入datefield("標籤", 輸入模式)datefield("標籤", 輸入模式, 時區)輸入模式:datefield.date - 只輸入日期datefield.time - 只輸入時間datefield.date_time - 同時輸入日期、時間setdate() 方法 設定輸入框的日期、時間getdate() 方法 取得輸入框中的日期、時間calendar.get()方法可以取得輸入的年、月、日等數值。

4. gauge進度條gauge("標籤", 是否互動模式, 最大值, 初始值)初始值 >= 0 and <= 最大值,否則illegalargumentexceptin異常。非互動模式:setvalue()方法設定gauge的當前值。互動模式:可通過左右鍵指定當前值。

5. choicegroup與list類似choicegroup("標籤", 型別)choicegroup("標籤", 型別, , )型別:choice.exclusive - 單選choice.multiple - 多選choice.popup - 彈出選單單選

6. imageitem顯示乙個image物件。imageitem("標籤", , 布局, alttext)imageitem("標籤", , 布局, alttext, 顯示樣式)布局:imageitem.layout_defaultimageitem.layout_leftimageitem.layout_rightimageitem.layout_centerimageitem.layout_newline_beforeimageitem.layout_newline_afteralttext: 當不能顯示時,顯示此文字樣式:同stringitem有: item.plain、item.hyperlink、item.button

7. spacer在item組個間加間隔。spacer(最小寬度, 最小高度)

8. customitem用來建立自定義外觀的item元件。抽象類,必須實現下抽象方法:protected abstract int getmincontentheight()//返回最小區域的高度protected abstract int getmincontentwidth()//返回最小區域的寬度protected abstract int getprefcontentheight(int width) //返回最佳區域的寬度protected abstract int getprefcontentwidth(int height) //返回最佳區域的寬度protected abstract void paint(graphics g, int w, int h)//畫元件

9. alert訊息提示框。public alert("標題")public alert("標題", "提示內容", 圖示, 型別)型別:alerttype.alarm - 鬧鈴資訊alerttype.confirmation - 確認資訊alerttype.error - 出錯資訊alerttype.info - 提示資訊alerttype.warning - 警告

注: 1. 根據midp 規範,永遠不要從midlet 的構造方法中呼叫getdisplay()方法,因為此時應用程式管理器能會錯誤地初始化該方法。

final關鍵字的作用

final - 最終的

加了final關鍵字的變數、方法、類不能被繼承、覆蓋。

一旦初始化後,就不能再變化。

list元件

列表框,screen的子類。

構造:list list = new list("標題", 型別)建立乙個空的列表

list list = new list("標題", 型別, 列表字串陣列, 列表項陣列)以第3、4個引數為初始列表專案建立乙個列表框。

list.size() - 得到列表專案數。

型別:list.exclusive 單選列表框getselectedindex() - 得到第幾個列表項被選中。getstring(int index) - 得到第index項的文字。與comand和commandlistener一起使用來確認選擇。command okcmd = new command("確認", command.ok, 1);list.addcommand(okcmd);list.setcommandlistener(this);

list.multiple 多選列表框不能用getseelctedindex()方法。 要用isselected(int index)來檢測index項是否被選中。還可以用getselectedflags(boolean flags),通過布林型別陣列flags來確定各項目的選擇情況。

list.implicit也是單選列表,提供乙個command物件,可能用這個型別的列表來做為程式選單。在commandaction中可直接引用list.select_command來執行相應的**:if (c == list.select_command) 也可以用setselectcommand(command command)方法來指定乙個自定義的command物件。

附:字串陣列: private string strary = ;圖示:image img1 = null;image img2 = null;try image imgary = ;

for迴圈:for (int i = 0; i < icount; i++) {

stringbuffer - 可用來處理字串的連線。

textbox元件

screen的乙個子類

構造方法: textbox = new textbox("標題文字", "預設顯示在文字框中的文字", 最多字元個數, 型別);型別引數取值:

textfield.any

任意字元

textfield.emailaddr

email位址

textfield.numeric

整數textfield.phonenumber

**號碼

textfield.url

url位址

textfield.decimal

小數or

textfield.password

密碼*顯示

textfield.uneditable

唯讀textfield.sensitive

敏感內容,不使用聯想或自動完成

textfield.initial_caps_word

自動將每個單詞的首字母改成大寫

textfield.initial_cpas_sentence

將每句話首字母改成大寫

方法: gettitle();獲取文字框標題文字 settitle(string title);設定文字框標題 getticker();得到設定給文字框的ticker物件 setticker(ticker ticker); 給文字框設定乙個ticker物件

ticker物件:ticker ticker = new ticker("這乙個滾動顯示的文字");textbox.setticker(ticker);

J2ME 文字滾動

private int tipstringpos screen width 當前字串左邊的位置 private int tipstringspeed 2 字串移動速度 private static final int tipstr left 5 5 左邊消失繪製座標 private static f...

j2me記憶體優化

out momory 一陣天旋地轉記憶體又溢位了。在手機上這種痛苦經常都有,套一句俗話在手機上用記憶體必須勒緊褲腰帶。雖然現在pc記憶體上g都不奇怪,可是在手機上卻只能以k來記,可能某位同志會馬上跳出來說也有上m的,記住中國還不富大多數手機都是低端手機。寫手機程式讓我彷彿回到了dos時代 自我安慰一...

j2me學習總結

j2me隨著最後乙個遊戲的完成,可以畫上乙個小句號了。做了幾個東西,簡單說一下。環境winxp eclipse netbeans 手機 nokia5230,s60v5平台 1 冷笑話 這個就是入門,執行之後可以看一些冷笑話,還有謎語之類的,用的是j2me自帶的ui 2.貪食蛇。就是貪食蛇,用了遊戲的...