📄 filebrowserexample.java
字号:
/* * FileBrowserExample.java * * Created on Aug 20, 2007, 10:22:30 AM * * To change this template, choose Tools | Templates * and open the template in the editor. */package filebrowserexample;import java.io.IOException;import java.io.InputStream;import javax.microedition.io.file.FileConnection;import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import org.netbeans.microedition.lcdui.SplashScreen;import org.netbeans.microedition.lcdui.pda.FileBrowser;/** * @author devil */public class FileBrowserExample extends MIDlet implements CommandListener { private boolean midletPaused = false; //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0| private Command backCommand; private Command exitCommand; private TextBox textBox; private FileBrowser fileBrowser; private SplashScreen splashScreen; //</editor-fold>//GEN-END:|fields|0| /** * The FileBrowserExample constructor. */ public FileBrowserExample() { } //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0| //</editor-fold>//GEN-END:|methods|0| //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize /** * Initilizes the application. * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method. */ private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize // write pre-initialize user code here//GEN-LINE:|0-initialize|1|0-postInitialize // write post-initialize user code here }//GEN-BEGIN:|0-initialize|2| //</editor-fold>//GEN-END:|0-initialize|2| //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction /** * Performs an action assigned to the Mobile Device - MIDlet Started point. */ public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction // write pre-action user code here switchDisplayable(null, getSplashScreen());//GEN-LINE:|3-startMIDlet|1|3-postAction // write post-action user code here }//GEN-BEGIN:|3-startMIDlet|2| //</editor-fold>//GEN-END:|3-startMIDlet|2| //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction /** * Performs an action assigned to the Mobile Device - MIDlet Resumed point. */ public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction // write pre-action user code here//GEN-LINE:|4-resumeMIDlet|1|4-postAction // write post-action user code here }//GEN-BEGIN:|4-resumeMIDlet|2| //</editor-fold>//GEN-END:|4-resumeMIDlet|2| //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch /** * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable. * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately * @param nextDisplayable the Displayable to be set */ public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch // write pre-switch user code here Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch if (alert == null) { display.setCurrent(nextDisplayable); } else { display.setCurrent(alert, nextDisplayable); }//GEN-END:|5-switchDisplayable|1|5-postSwitch // write post-switch user code here }//GEN-BEGIN:|5-switchDisplayable|2| //</editor-fold>//GEN-END:|5-switchDisplayable|2| //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand ">//GEN-BEGIN:|23-getter|0|23-preInit /** * Returns an initiliazed instance of backCommand component. * @return the initialized component instance */ public Command getBackCommand() { if (backCommand == null) {//GEN-END:|23-getter|0|23-preInit // write pre-init user code here backCommand = new Command("Back", Command.BACK, 0);//GEN-LINE:|23-getter|1|23-postInit // write post-init user code here }//GEN-BEGIN:|23-getter|2| return backCommand; } //</editor-fold>//GEN-END:|23-getter|2| //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|26-getter|0|26-preInit /** * Returns an initiliazed instance of exitCommand component. * @return the initialized component instance */ public Command getExitCommand() { if (exitCommand == null) {//GEN-END:|26-getter|0|26-preInit // write pre-init user code here exitCommand = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|26-getter|1|26-postInit // write post-init user code here }//GEN-BEGIN:|26-getter|2| return exitCommand; } //</editor-fold>//GEN-END:|26-getter|2| //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction /** * Called by a system to indicated that a command has been invoked on a particular displayable. * @param command the Command that was invoked * @param displayable the Displayable where the command was invoked */ public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction // write pre-action user code here if (displayable == fileBrowser) {//GEN-BEGIN:|7-commandAction|1|19-preAction if (command == FileBrowser.SELECT_FILE_COMMAND) {//GEN-END:|7-commandAction|1|19-preAction readFile(); switchDisplayable(null, getTextBox());//GEN-LINE:|7-commandAction|2|19-postAction // write post-action user code here } else if (command == exitCommand) {//GEN-LINE:|7-commandAction|3|27-preAction // write pre-action user code here exitMIDlet();//GEN-LINE:|7-commandAction|4|27-postAction // write post-action user code here }//GEN-BEGIN:|7-commandAction|5|15-preAction } else if (displayable == splashScreen) { if (command == SplashScreen.DISMISS_COMMAND) {//GEN-END:|7-commandAction|5|15-preAction // write pre-action user code here switchDisplayable(null, getFileBrowser());//GEN-LINE:|7-commandAction|6|15-postAction // write post-action user code here }//GEN-BEGIN:|7-commandAction|7|24-preAction } else if (displayable == textBox) { if (command == backCommand) {//GEN-END:|7-commandAction|7|24-preAction // write pre-action user code here switchDisplayable(null, getFileBrowser());//GEN-LINE:|7-commandAction|8|24-postAction // write post-action user code here }//GEN-BEGIN:|7-commandAction|9|7-postCommandAction }//GEN-END:|7-commandAction|9|7-postCommandAction // write post-action user code here }//GEN-BEGIN:|7-commandAction|10| //</editor-fold>//GEN-END:|7-commandAction|10| //<editor-fold defaultstate="collapsed" desc=" Generated Getter: splashScreen ">//GEN-BEGIN:|13-getter|0|13-preInit /** * Returns an initiliazed instance of splashScreen component. * @return the initialized component instance */ public SplashScreen getSplashScreen() { if (splashScreen == null) {//GEN-END:|13-getter|0|13-preInit // write pre-init user code here splashScreen = new SplashScreen(getDisplay());//GEN-BEGIN:|13-getter|1|13-postInit splashScreen.setTitle("splashScreen"); splashScreen.setCommandListener(this); splashScreen.setText("File Browser Example");//GEN-END:|13-getter|1|13-postInit // write post-init user code here }//GEN-BEGIN:|13-getter|2| return splashScreen; } //</editor-fold>//GEN-END:|13-getter|2| //<editor-fold defaultstate="collapsed" desc=" Generated Getter: fileBrowser ">//GEN-BEGIN:|17-getter|0|17-preInit /** * Returns an initiliazed instance of fileBrowser component. * @return the initialized component instance */ public FileBrowser getFileBrowser() { if (fileBrowser == null) {//GEN-END:|17-getter|0|17-preInit // write pre-init user code here fileBrowser = new FileBrowser(getDisplay());//GEN-BEGIN:|17-getter|1|17-postInit fileBrowser.setTitle("fileBrowser"); fileBrowser.setCommandListener(this); fileBrowser.addCommand(FileBrowser.SELECT_FILE_COMMAND); fileBrowser.addCommand(getExitCommand());//GEN-END:|17-getter|1|17-postInit // write post-init user code here }//GEN-BEGIN:|17-getter|2| return fileBrowser; } //</editor-fold>//GEN-END:|17-getter|2| //<editor-fold defaultstate="collapsed" desc=" Generated Getter: textBox ">//GEN-BEGIN:|21-getter|0|21-preInit /** * Returns an initiliazed instance of textBox component. * @return the initialized component instance */ public TextBox getTextBox() { if (textBox == null) {//GEN-END:|21-getter|0|21-preInit // write pre-init user code here textBox = new TextBox("textBox", null, 100, TextField.ANY);//GEN-BEGIN:|21-getter|1|21-postInit textBox.addCommand(getBackCommand()); textBox.setCommandListener(this);//GEN-END:|21-getter|1|21-postInit // write post-init user code here }//GEN-BEGIN:|21-getter|2| return textBox; } //</editor-fold>//GEN-END:|21-getter|2| /** * Loads text file */ private void readFile() { try { FileConnection textFile = fileBrowser.getSelectedFile(); getTextBox().setString(""); InputStream fis = textFile.openInputStream(); byte[] b = new byte[1024]; int length = fis.read(b, 0, 1024); fis.close(); if (length > 0) { textBox.setString(new String(b, 0, length)); } } catch (IOException ex) { ex.printStackTrace(); } } /** * Returns a display instance. * @return the display instance. */ public Display getDisplay () { return Display.getDisplay(this); } /** * Exits MIDlet. */ public void exitMIDlet() { switchDisplayable (null, null); destroyApp(true); notifyDestroyed(); } /** * Called when MIDlet is started. * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet. */ public void startApp() { if (midletPaused) { resumeMIDlet (); } else { initialize (); startMIDlet (); } midletPaused = false; } /** * Called when MIDlet is paused. */ public void pauseApp() { midletPaused = true; } /** * Called to signal the MIDlet to terminate. * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released. */ public void destroyApp(boolean unconditional) { }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -