📄 i18ndemo.java
字号:
/**
* I18NDemo.java
*
* Oct. 15, 2004
*
* (c) Copyright. 2004. Motorola , Inc. ALL RIGHTS RESERVED.
* This notice does not imply publication.
*
*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
/**
* Internationalization Demo. User can switch Language between English,
* Simplified Chinese, Traditional Chinese or default mode.
*
* The default mode means that application get locale information from
* setting "Default-Language" in jar, if it is null, try to get system locale.
*
* @see MIDlet
*/
public class I18NDemo extends MIDlet implements CommandListener, LocaleChangeListener {
static Form mainUI;
static Display myDisplay;
private Form langChooser;
private Command exit = null;
private Command switchLang = null;
ResourceBundle i18nBundle ;
static String jarLocale = null;
private String currentLocale = null;
static String locale = System.getProperty ("microedition.locale");
public I18NDemo () {
super ();
myDisplay = Display.getDisplay(this);
mainUI = new Form("title");
jarLocale = getAppProperty ("Default-Language");
}
/**
* Valid return values: "en", "sc" or "tc" * ,
* which stand for English, Simplified Chinese and Traditional Chinese ,
* respectively.
*/
static public String getDefaultLocale () {
if (jarLocale != null)
return jarLocale;
String locale = System.getProperty ("microedition.locale");
if (locale.startsWith ("en"))
return "en";
else if (locale.startsWith("zh")) {
if (locale.indexOf("CN") > 0)
return "sc";
else
return "tc";
}
return null;
}
/**
* Construct UI with specified locale
*/
private void constructUI (String locale) throws IOException {
currentLocale = locale;
for (int i = 0; i < mainUI.size(); i++) {
mainUI.delete (i);
}
//mainUI.deleteAll();
i18nBundle = ResourceBundle.getResourceBundle ("I18NDemo",locale);
mainUI.setTitle (i18nBundle.getString("title"));
mainUI.append (i18nBundle.getString("welcome"));
if (exit != null)
mainUI.removeCommand (exit);
if (switchLang != null)
mainUI.removeCommand (switchLang);
exit = new Command(i18nBundle.getString("exit"), Command.EXIT, 3);
switchLang = new Command(i18nBundle.getString("switchlang"), Command.ITEM, 1);
mainUI.addCommand (exit);
mainUI.addCommand (switchLang);
mainUI.setCommandListener (this);
}
boolean initialized = false;
/**
* Midlet lifecycle management
*/
public void startApp () {
if (!initialized) {
String locale = getDefaultLocale ();
try {
constructUI (locale);
initialized = true;
} catch (Exception e) {
Alert alert = new Alert("Error",
"Exception in UI:" + e.getMessage(),
null, AlertType.ERROR);
alert.setTimeout (Alert.FOREVER);
myDisplay.setCurrent(alert);
}
}
myDisplay.setCurrent(mainUI);
}
public void pauseApp () {
}
public void destroyApp (boolean unconditional) {
notifyDestroyed();
}
public void commandAction (Command c, Displayable d) {
if (c == exit) {
destroyApp(false);
} else if (c == switchLang) {
LanguageChooser chooser = LanguageChooser.getLanguageChooser(currentLocale);
chooser.addLocaleChangeListener (this);
myDisplay.setCurrent (chooser);
}
}
public void localeChanged (String newLocale) {
try {
constructUI (newLocale);
} catch (IOException ioe) {
Alert alert = new Alert("Error",
"Exception when switch locale :" + ioe.getMessage(),
null, AlertType.ERROR);
alert.setTimeout (Alert.FOREVER);
myDisplay.setCurrent(alert);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -