?? addwordscreen.java
字號:
package org.zblog.zenghelper.screen;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import org.zblog.zenghelper.util.Navigator;
import org.zblog.zenghelper.screen.sub.IsSenScreen;
import org.zblog.zenghelper.screen.sub.AddInfoScreen;
import org.zblog.zenghelper.screen.sub.SelectSymbolScreen;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import java.util.Vector;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Image;
import org.zblog.zenghelper.screen.sub.EnWordScreen;
import javax.microedition.lcdui.Alert;
import org.zblog.zenghelper.util.PublicRes;
import javax.microedition.lcdui.AlertType;
import org.zblog.zenghelper.dbtool.DbTool;
/**
* <br><strong>Z英語學習助手-查看/添加/修改單詞屏幕</strong><br>
* <br>該屏幕是采用低級API來實現.主要包括:
* <br>1.標題欄和分隔橫線
* <br>2.正文:單詞,音標,生詞,解釋
* <br>3.一個Back按鈕用于返回主菜單
* <br>4.其它Command:修改單詞,修改音標,標記生詞,添加解釋,提交表單
*
* @author <a href="mailto:zcw@zblog.org">朱傳偉</a><br>
* <a href="http://www.zblog.org">www.zblog.org</a></p>
* @version <strong>ZEnHelper</strong> Ver 1.0
*/
public class AddWordScreen extends Canvas implements CommandListener,Runnable{
private static AddWordScreen aws=null;
public PublicRes pr=PublicRes.getInstance();
public int rsId=-1;
public int groupId=-1;
public int senId=-1;
public Graphics gps=null;
//以下變量主要用于上下滾動時使用,base表示當前應該跳過的行數,標題和橫線除外
public int base=0;
public int step=1;
/** 英文單詞 */
public String enWord=null;
/** 音標 */
public Vector ybs=null;
/** 中文解釋 */
public Vector cnWords=null;
/** 是否標記為生詞 */
public boolean sen=true;
/** 相關命令 */
private Command back=null;
private Command updateEn=null;
private Command updateYb=null;
private Command updateSen=null;
private Command updateCn=null;
private Command addWord=null;
private AddWordScreen() {
enWord="";
ybs=new Vector();
cnWords=new Vector();
back=new Command("返回",Command.BACK,1);
updateEn=new Command("修改單詞",Command.ITEM,1);
updateYb=new Command("修改音標",Command.ITEM,2);
updateSen=new Command("標記生詞",Command.ITEM,3);
updateCn=new Command("添加解釋",Command.ITEM,4);
addWord=new Command("提交表單",Command.ITEM,5);
addCommand(back);
addCommand(updateEn);
addCommand(updateYb);
addCommand(updateSen);
addCommand(updateCn);
addCommand(addWord);
setCommandListener(this);
}
/**
* 清除該對象的各種屬性,將對象還原為初始狀態,因為該類的對象采用static方式創建,所以
* 有時需要將對象的屬性還原為初始狀態.
*/
public void clear(){
rsId=-1;
groupId=-1;
senId=-1;
base=0;
enWord="";
cnWords.removeAllElements();
ybs.removeAllElements();
sen=true;
}
/**
* 工廠方法,用于創建該類AddWordScreen的實例對象
* @return AddWordScreen
*/
public synchronized static AddWordScreen getInstance(){
if(aws==null)
aws=new AddWordScreen();
else
aws.clear();
return aws;
}
/**
* 該方法覆蓋了父類中的keyPressed方法,用于捕獲上,下鍵事件,以對屏幕進行上下滾動
* @param keycode int
*/
public void keyPressed(int keycode){
//向上滾動
if(keycode==-1){
base=base-step;
if(base<0)base=0;
}
//向下滾動
else if(keycode==-2){
base=base+step;
//保證屏幕中至少顯示4條解釋
// if(base>(cnWords.size()+4-4))base=cnWords.size()+4-4;
if(base>cnWords.size())base=cnWords.size();
}
repaint();
}
/**
* 實現paint方法,繪制屏幕圖像
* @param gp Graphics
*/
protected void paint(Graphics gp) {
gps=gp;
int xx=gp.getClipWidth();
int yy=gp.getClipHeight();
//游動的y軸坐標
int py=0;
//固定的起始x軸坐標
int px=3;
//y軸間隔
int ppy=1;
//x軸間隔
int ppx=2;
//任意變量
int ti=0;
//清除屏幕
gp.setColor(255,255,255);
gp.fillRect(0,0,xx,yy);
//繪制標題
gp.setColor(0,0,123);
Font fontBT=Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_MEDIUM);
Font fontZW=Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,Font.SIZE_MEDIUM);
gp.setFont(fontBT);
if(rsId==-1)
gp.drawString("添加單詞表單",5,3,Graphics.TOP | Graphics.LEFT);
else
gp.drawString("單詞詳細信息",5,3,Graphics.TOP | Graphics.LEFT);
py=fontBT.getHeight()+3;
gp.drawLine(0,py,xx,py);
py=py+1;
gp.setColor(88,168,255);
gp.drawLine(0,py,xx,py);
gp.setColor(0,0,123);
//針對上下滾動的響應,現在開始計數
int point=0;
py=py+ppy;
//繪制"單詞"及內容
char[]tcs="單詞:".toCharArray();
int twoL=fontBT.charsWidth(tcs,0,tcs.length);
if(point>=base){
gp.drawString("單詞:",px,py,Graphics.TOP | Graphics.LEFT);
ti=px+twoL+ppx;
gp.setFont(fontZW);
gp.drawString(enWord,ti,py,Graphics.TOP | Graphics.LEFT);
py=py+fontZW.getHeight()+ppx;
}
//繪制"音標"及內容
point++;
if(point>=base){
gp.setFont(fontBT);
gp.drawString("音標:",px,py,Graphics.TOP | Graphics.LEFT);
ti=px+twoL+ppx;
Image pim=null;
try{
for (int i = 0, n = ybs.size(); i < n; i++) {
pim = Image.createImage("/org/zblog/zenghelper/res/symbol/" +
ybs.elementAt(i) + ".png");
gp.drawImage(pim,ti,py,Graphics.TOP | Graphics.LEFT);
ti=ti+pim.getWidth();
}
catch(Exception e){e.printStackTrace();}
py=py+fontBT.getHeight()+ppy;
}
//繪制"生詞"及標記
point++;
if(point>=base){
gp.drawString("生詞:",px,py,Graphics.TOP | Graphics.LEFT);
gp.setFont(fontZW);
ti=px+twoL+ppx;
if(sen)
gp.drawString("是",ti,py,Graphics.TOP | Graphics.LEFT);
else
gp.drawString("否",ti,py,Graphics.TOP | Graphics.LEFT);
py=py+fontBT.getHeight()+ppy;
}
//繪制"解釋"及標記
point++;
if(point>=base){
gp.setFont(fontBT);
gp.drawString("解釋:",px,py,Graphics.TOP | Graphics.LEFT);
py = py + fontBT.getHeight();
}
gp.setFont(fontZW);
for(int i=0;i<cnWords.size();i++){
point++;
if(point>=base){
gp.drawString((String) cnWords.elementAt(i), px + 2, py,
Graphics.TOP | Graphics.LEFT);
py = py + fontZW.getHeight();
}
}
}
/**
* Command監聽實現方法,用于捕獲該對象定義的各種Command
* @param command Command
* @param displayable Displayable
*/
public void commandAction(Command command, Displayable displayable) {
String lb=command.getLabel();
if(lb.equals("返回")){
Navigator.current=Navigator.Main_Screen;
Navigator.show();
}
else if(lb.equals("修改單詞")){
Navigator.show(new EnWordScreen(this));
}
else if(lb.equals("修改音標")){
Navigator.show(new SelectSymbolScreen(this));
}
else if(lb.equals("標記生詞")){
Navigator.show(new IsSenScreen(this));
}
else if(lb.equals("添加解釋")){
Navigator.show(new AddInfoScreen(this));
}
else if(lb.equals("提交表單")){
Alert message=new Alert("提交單詞表單"," 程序正在執行提交操作,請稍后.....",pr.busyImage,AlertType.CONFIRMATION);
message.setTimeout(60000000);
Navigator.show(message);
Thread init=new Thread(this);
init.start();
}
}
/**
* 執行添加單詞操作
*/
private void addWord(){
//檢查單詞是否填寫
String mess=null;
boolean ok=true;
if(enWord==null||enWord.trim().equals("")){
mess="錯誤:單詞項為空";
ok=false;
}
else{
enWord=enWord.trim();
//檢查頭兩個char是否在z的范圍內
if(enWord.charAt(0)>'z'||(enWord.length()>1&&enWord.charAt(1)>'z')){
ok=false;
mess="錯誤:單詞項非法字符";
}
}
if(ok){
DbTool dt = DbTool.getInstance();
int res = -1;
if(rsId==-1){
res = dt.addWord(this);
mess="單詞添加成功!";
}
else{
res=dt.updateWord(this);
mess="單詞修改成功!";
}
if(res!=-1){
clear();
}else{
mess="失敗:系統錯誤.";
}
}
Alert result=new Alert("表單提交結果",mess,pr.busyImage,AlertType.CONFIRMATION);
result.setTimeout(Alert.FOREVER);
Navigator.display.setCurrent(result,this);
}
/**
* 因為提交表單操作可能時間比較長,所有采用了多線程方式,后臺執行提交操作,
* 前臺顯示"請等待..."頁面
*/
public void run() {
addWord();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -