📄 weathermidlet.java
字号:
package com.j2medev.chapter4;
import java.io.IOException;
import java.util.Vector;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class WeatherMIDlet extends MIDlet implements CommandListener{
private Display display = null;
private Form form = null;
private Image logo = null;
private TextField location = new TextField("Location","",20,TextField.ANY);
private ChoiceGroup style = new ChoiceGroup("style",Choice.EXCLUSIVE,new String[]{"Fahrenheit","Celsius"},null);
public static final Command okCommand = new Command("query",Command.OK,1);
public static final Command backCommand = new Command("back",Command.BACK,2);
public static final Command exitCommand = new Command("exit",Command.EXIT,3);
public void startApp() {
if(display == null){
display = Display.getDisplay(this);
try {
logo = Image.createImage("/logo.png");
} catch (IOException ex) {
ex.printStackTrace();
}
ImageItem imglogo = new ImageItem("",logo,Item.LAYOUT_CENTER|Item.LAYOUT_NEWLINE_AFTER,"yahoo weather");
form = new Form("Yahoo! weather");
form.append(imglogo);
form.append(location);
form.append(style);
form.addCommand(okCommand);
form.addCommand(exitCommand);
form.setCommandListener(this);
}
display.setCurrent(form);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void setCurrent(Displayable displayable){
display.setCurrent(displayable);
}
public void displayWeather(Vector forecasts,Location location){
Form result = new Form("Yahoo! Weather");
result.append(location.toString());
//这里显示两天的天气信息
for(int i = 0;i<forecasts.size();i++){
result.append(((Forecast)forecasts.elementAt(i)).toString());
}
result.addCommand(backCommand);
result.setCommandListener(this);
display.setCurrent(result);
}
public void commandAction(Command cmd,Displayable displayable){
if(cmd == okCommand){
//位置和单位
String p = location.getString();
int index = style.getSelectedIndex();
String s = index > 0?"c":"f";
String param = "p="+p+"&u="+s;
//查询天气预报
new Thread(new WeatherReader(this,param)).start();
}else if(cmd == backCommand){
display.setCurrent(form);
}else if(cmd == exitCommand){
destroyApp(false);
notifyDestroyed();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -