📄 guessword_midlet.java
字号:
package ch04;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class GuessWord_MIDlet
extends MIDlet
implements CommandListener {
//声明一个用来控制单词生产的GuessWordManage类实例对象
GuessWordManage mg = new GuessWordManage();
//声明、获取一个Display对象实例对象
Display display = Display.getDisplay(this);
//声明、创建一个滚动条对象用来提示信息
Ticker currentTicker = new Ticker("提示");
//声明、创建窗体From和提示框Alert上使用的按钮
Command confirm1 = new Command("确定", Command.OK, 1);
Command confirm2 = new Command("确定", Command.OK, 1);
Command confirm3 = new Command("确定", Command.OK, 1);
Command confirm4 = new Command("确定", Command.OK, 1);
Command exit = new Command("退出", Command.EXIT, 1);
Command back = new Command("返回菜单", Command.BACK, 1);
Command cancel = new Command("取消", Command.BACK, 1);
Command goOn = new Command("下一题", Command.OK, 1);
//声明一个承载游戏选项列表对象
List start;
//声明猜字窗体中要使用的文本域
TextField quesField, answerField, wordField, meanField;
//声明一个单选组,用于让玩家选择游戏难度
ChoiceGroup rank;
//声明一个控制每个题目答题时间的计时器
Gauge timeGauge;
//声明一个控制线程生死状态的布尔变量
static boolean symbol = true;
/*
1.构造器
*/
public GuessWord_MIDlet() {
display.setCurrent(getWelcomeAlert());
start = startList();
display.setCurrent(start);
}
/*
2.获得一个用于显示欢迎的Alert提示框
*/
public Alert getWelcomeAlert() {
Image image = null;
try {
image = Image.createImage("/icons/guessWord/welcome.png");
}
catch (Exception e) {
System.out.println(e.getMessage());
}
Alert welcome = new Alert("", null, image, AlertType.INFO); //构造一个欢迎界面;
welcome.setTimeout(1000);
return welcome;
}
/*
3.创建游戏功能列表
*/
public List startList() {
String[] stringElements = {
"缺字猜词", "难度设置", "添加词库", "游戏帮助"};
Image image1 = null, image2 = null, image3 = null, image4 = null;
try {
image1 = Image.createImage("/icons/guessWord/icon01.png");
image2 = Image.createImage("/icons/guessWord/icon02.png");
image3 = Image.createImage("/icons/guessWord/icon03.png");
image4 = Image.createImage("/icons/guessWord/icon04.png");
}
catch (Exception e) {
System.out.println(e.getMessage());
}
Image[] imageElements = {
image1, image2, image3, image4};
List start = new List("开始游戏", Choice.IMPLICIT, stringElements,
imageElements);
currentTicker.setString("Welcome to GuessWord");
start.setTicker(currentTicker);
start.addCommand(confirm1);
start.addCommand(exit);
start.setCommandListener(this);
return start;
}
/*
4.创建猜字游戏的主游戏窗体
*/
public Form guessForm() {
Form guessForm = new Form("缺字猜词");
currentTicker.setString("Wish you good Luck!");
guessForm.setTicker(currentTicker);
guessForm.append(timeGauge);
try {
String guessstr = mg.getFinalWord();
String mean = GuessWordManage.tempMean;
quesField = new TextField("题目", guessstr, 40,
TextField.ANY | TextField.UNEDITABLE);
meanField = new TextField("词义", mean, 40,
TextField.ANY | TextField.UNEDITABLE);
answerField = new TextField("回答", "", 40,
TextField.ANY);
guessForm.append(answerField);
guessForm.append(quesField);
guessForm.append(meanField);
}
catch (Exception e) {
System.out.println(e.getMessage());
}
guessForm.addCommand(confirm2);
guessForm.addCommand(back);
guessForm.setCommandListener(this);
return guessForm;
}
/*
5.创建选择游戏难度的窗体
*/
public Form rankForm() {
Form rankForm = new Form("游戏设置");
Image aimage = null, bimage = null, cimage = null;
try {
aimage = Image.createImage("/icons/background/star.png");
bimage = Image.createImage("/icons/background/hand.png");
cimage = Image.createImage("/icons/background/boot.png");
}
catch (Exception e) {
System.out.println(e.getMessage());
}
Image[] imageArray = {
aimage, bimage, cimage};
String[] rankArray = {
"去除一个字母", "去除三个字母(默认)", "去除五个字母"};
rank = new ChoiceGroup("难度设置",
ChoiceGroup.EXCLUSIVE,
rankArray,
imageArray);
ChoiceGroup groups = rank;
rankForm.append(groups);
currentTicker.setString("Game Setting");
rankForm.setTicker(currentTicker);
rankForm.addCommand(cancel);
rankForm.addCommand(confirm4);
rankForm.setCommandListener(this);
return rankForm;
}
/*
6.创建添加单词到字库的窗体
*/
public Form addForm() {
Form addForm = new Form("添加单词");
currentTicker.setString("Add a new word");
addForm.setTicker(currentTicker);
try {
wordField = new TextField("请输入单词", "", 40,
TextField.ANY);
addForm.append(wordField);
meanField = new TextField("请输入词义", "", 40,
TextField.ANY);
addForm.append(meanField);
}
catch (Exception e) {
System.out.println(e.getMessage());
}
addForm.addCommand(confirm3);
addForm.addCommand(back);
addForm.setCommandListener(this);
return addForm;
}
/*
7.创建帮助窗体
*/
public Form helpForm() {
Form helpForm = new Form("游戏帮助");
currentTicker.setString("Game Help");
helpForm.setTicker(currentTicker);
String label1 = "缺字猜词";
String text1 = "\n将文本域中带'-'的字母补全,按“确定”按钮即可;游戏会自动判定答案的正误,给出结果。";
String label2 = "难度说明";
String text2 = "\n玩家可以通过选择游戏中扣去字母的个数,来控制游戏的难度。";
String label3 = "答题时间";
String text3 = "\t答题界面下的计时器会给你提示答题世界。游戏默认的答题时间是每题16秒,在规定时间内没点击确定按钮则认为答题超时。";
StringItem gameinfo1 = new StringItem(label1, text1);
StringItem gameinfo2 = new StringItem(label2, text2);
StringItem gameinfo3 = new StringItem(label3, text3);
helpForm.append(gameinfo1);
helpForm.append(gameinfo2);
helpForm.append(gameinfo3);
helpForm.addCommand(back);
helpForm.setCommandListener(this);
return helpForm;
}
/*
8.创建一个提示用户是否回答正确的提示框
symbol 是一个boolean类型的标识,用户回答正确的时候,返回为true;否则返回为false.
*/
public Alert judgeAlert(boolean symbol) {
Image image = null;
try {
if (symbol == true) {
image = Image.createImage("/icons/guessWord/good.png");
}
if (symbol == false) {
image = Image.createImage("/icons/guessWord/bad.png");
}
}
catch (Exception e) {
System.out.println(e.getMessage());
}
String answer = "单词: " + GuessWordManage.tempWord + " 词义: " +
GuessWordManage.tempMean;
Alert good = new Alert("还要努力哦!", answer, image,
AlertType.INFO);
good.setTimeout(5000);
good.addCommand(goOn); //添加一个继续按钮,返回到答题的主界面
good.addCommand(back);
good.setCommandListener(this); //添加监听器
return good;
}
/*
9.创建一个答题时间期限已过的提示框
每一次作题都要求在一定的时间类完成,这需要对一个进度条进行监控
*/
public Alert timeAlert() {
Image image = null;
try {
image = Image.createImage("/icons/guessWord/time.png");
}
catch (Exception e) {
System.out.println(e.getMessage());
}
Alert time = new Alert("", null, image, AlertType.INFO);
time.setTimeout(2000);
time.addCommand(goOn);
time.setCommandListener(this);
return time;
}
/*
10.创建一个添加单词成功的提示框
*/
public Alert addGoodAlert() {
Image image = null;
try {
image = Image.createImage("/icons/guessWord/add.png");
}
catch (Exception e) {
System.out.println(e.getMessage());
}
Alert addWord = new Alert("", null, image, AlertType.INFO);
addWord.setTimeout(700);
return addWord;
}
/*
11.创建一个添加单词失败的提示框
*/
public Alert addBadAlert() {
Image image = null;
try {
image = Image.createImage("/icons/guessWord/bad2.png");
}
catch (Exception e) {
System.out.println(e.getMessage());
}
Alert addWord = new Alert("", null, image, AlertType.INFO);
addWord.setTimeout(700);
return addWord;
}
/*
12.创建一个控制答题时间的进度条线程对象
*/
class timeGauge
extends Gauge
implements Runnable {
//每次变换的幅度
private int movement = 1;
//每次给计数器设置的值
private int newValue = 0;
/*
启动线程
*/
public timeGauge() {
super("请注意时间!", false, 8, 0);
new Thread(this).start();
}
public void run() {
while (true) {
if (symbol == true) {
setTime();
}
else {
break;
}
}
}
/*
控制进度条的值
*/
public synchronized void setTime() {
try {
Thread.sleep(2000);
}
catch (Exception ex) {
ex.printStackTrace();
}
newValue = getValue() + movement; //当前值加1
if (newValue < 9) {
setValue(newValue); //设置当前值
}
if (newValue == 9) {
display.setCurrent(timeAlert());
symbol = false; //关闭该线程
}
}
}
/*
13.响应按钮事件
*/
public void commandAction(Command c, Displayable s) {
if (c == confirm1) {
if (start.getSelectedIndex() == 0) {
symbol = true;
timeGauge = new timeGauge(); //创建一个新的线程对象
display.setCurrent(guessForm());
}
if (start.getSelectedIndex() == 1) {
display.setCurrent(rankForm());
}
if (start.getSelectedIndex() == 2) {
display.setCurrent(addForm());
}
if (start.getSelectedIndex() == 3) {
display.setCurrent(helpForm());
}
}
//按下答题界面的确定
if (c == confirm2) {
symbol = false;
String str = new String(""); //避免空指针
str = answerField.getString();
//判断用户输入的字符与题目要求的字符是否相同。根据比较的结果来反馈用户相应的信息
if (str.equals(GuessWordManage.tempWord)) {
display.setCurrent(judgeAlert(true));
}
else {
display.setCurrent(judgeAlert(false));
}
}
//按下添加单词的确定
if (c == confirm3) {
String strW = wordField.getString();
String strM = meanField.getString();
//防止用户不输入
if (strW == null || strW.length() == 0 || strM == null ||
strM.length() == 0) {
display.setCurrent(addBadAlert());
}
else {
mg.addWord(strW, strM);
display.setCurrent(addGoodAlert());
}
wordField.setString("");
meanField.setString("");
}
//确定游戏的难度
if (c == confirm4) {
if (rank.getSelectedIndex() == 0) {
GuessWordManage.times = 1; // "去除一个字母"
}
else if (rank.getSelectedIndex() == 1) {
GuessWordManage.times = 3; // "去除三个字母"
}
else if (rank.getSelectedIndex() == 2) {
GuessWordManage.times = 5; // "去除五个字母"
}
currentTicker.setString("Welcome to GuessWord");
display.setCurrent(start);
}
//要求返回开始列表的返回按钮
if (c == cancel) {
currentTicker.setString("Welcome to GuessWord");
display.setCurrent(start);
}
//要求返回 开始列表的返回按钮
if (c == back) {
symbol = false;
currentTicker.setString("Welcome to GuessWord");
display.setCurrent(start);
}
//要求返回答题界面的继续按钮
if (c == goOn) {
symbol = true;
timeGauge = new timeGauge(); //创建一个新的线程对象
display.setCurrent(guessForm());
}
//退出游戏按钮
if (c == exit) {
destroyApp(true);
notifyDestroyed();
}
}
//启动应用程序
public void startApp() {
}
//挂起应用程序
public void pauseApp() {
}
//撤销应用程序
public void destroyApp(boolean conditional) {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -