⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stockmidlet.java

📁 j2me Demos for example
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/* * * Copyright (c) 2007, Sun Microsystems, Inc. * * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * *  * Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. *  * Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in the *    documentation and/or other materials provided with the distribution. *  * Neither the name of Sun Microsystems nor the names of its contributors *    may be used to endorse or promote products derived from this software *    without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */package example.stock;import java.io.*;import java.util.Timer;import java.util.TimerTask;import javax.microedition.io.*;import javax.microedition.lcdui.*;import javax.microedition.midlet.*;import javax.microedition.rms.*;/** * <p>The MIDlet application class that we'll run for the Stock Demo.</p> * */public class StockMIDlet extends MIDlet {    /**     * Since there is no support in MIDP/CLDC for floating point numbers,     * and all of the stock data comes in the form ##.### (usually) then we     * have to find a way to store the decimals as well as the integer part     * of the quote data.  Multiplying by the OFFSET will shift the decimal     * place far enough over to make the number an integer which we can     * store.  Every time we retrieve quote data from our     * <code>RecordStore</code> we     * must make sure that we divide by the OFFSET to correctly display and     * use the value we actually want.     */    private static int OFFSET = 10000; // each 0 is one decimal place    /**     * Back <code>Command</code>     */    private static final Command BACK_COMMAND = new Command("Back", Command.BACK, 0);    /**     * Main Menu <code>Command</code>     */    private static final Command MAIN_MENU_COMMAND = new Command("Main", Command.SCREEN, 1);    /**     * Done <code>Command</code>     */    private static final Command DONE_COMMAND = new Command("Done", Command.OK, 2);    /**     * Set <code>Command</code>     */    private static final Command SET_COMMAND = new Command("Set", Command.OK, 4);    /**     * Exit <code>Command</code>     */    private static final Command EXIT_COMMAND = new Command("Exit", Command.STOP, 5);    /**     * Calc <code>Command</code>     */    private static final Command CALC_COMMAND = new Command("Calc", Command.OK, 6);    /**     * The <code>MIDlet</code>'s display object     */    Display display = null;    /**     * The <code>Ticker</code> that scrolls along the top of the screen     */    private Ticker stockTicker = null;    /**     * A <code>List</code> of stocks     */    private List choose = null;    /**     * The Stock Tracker menu     */    private List view = null;    /**     * The Main menu     */    private List menu = null;    /**     * The Alert menu     */    private List alertList = null;    /**     * The Settings menu     */    private List settingsList = null;    /**     * The 'What If?' <code>Form</code> upon which we enter our query data     */    private Form whatif = null;    /**     * The <code>Form</code> to display the different update intervals settings     */    private Form updatesForm = null;    /**     * Used to input a stock symbol     */    private TextBox stockSymbolBox = null;    /**     * Used to enter the price at which the user wishes to be alerted to the     * stocks's value     */    private TextBox alertPriceBox = null;    /**     * The original price the user purchased the stock at, used on the What If?     * <code>Form</code>     */    private TextField origPurchPriceField = null;    /**     * The number of shares the users wishes to sell, used on the What If?     * <code>Form</code>     */    private TextField numSharesField = null;    /**     * The radio buttons for the update interval time     */    private ChoiceGroup updatesChoices = null;    /**     * A textual reference to the current menu that is displayed     * onscreen to     * allow the <code>StockCommandListener</code> to decide what     * action to perform upon     * execution of a command     */    private String currentMenu = null;    /**     * A reference to the stock that has been chosen from a list of stocks     * onscreen.  Using this we can extract the correct stock's data from     * the <code>StockDatabase</code>     */    private String stockSymbol = null;    /**     * The reference to the <code>StockDatabase</code> that stores     * the stock data     */    private StockDatabase stocks = null;    /**     * The reference to the <code>AlertDatabase</code> that stores the alerts     */    private AlertDatabase alerts = null;    /**     * The server from which the quotes are downloaded     *     * NOTE: Currently, only the quote.yahoo.com server is supported     */    private String quoteServerURL = "http://quote.yahoo.com/d/quotes.csv?s=";    /**     * The format parameter for the quote server to retrieve stock data     *     * NOTE: Currently, only this format is supported     */    private String quoteFormat = "&f=slc1wop";    /**     * The proxy server that must be negotiated     *     * NOTE: Proxy server is optional and a blank string indicates that     *       no proxy should be used     */    // Initial default but read from settings file on subsequent runs    private String proxyURL = null;    /**     * The <code>Timer</code> object that refreshes the stock     * quotes periodically     */    private Timer stockRefresh = null;    /**     * The <code>TimerTask</code> that the <code>Timer</code>     * performs periodically.     * It refreshes the stock quotes     */    private StockRefreshTask stockRefreshTask = null;    /**     * How often are the stocks' data updated off of the server?     */    // Initial default but read from settings file on subsequent runs    private int refresh_interval = 900000; // 1000 = 1 second    private boolean firstTime;    /**     * <p>Default constructor that is called by the application     * manager to create a new instance of this <code>MIDlet</code> after     * which the <code>MIDlet</code> enters the <code>Paused</code> state.</p>     */    public StockMIDlet() throws MIDletStateChangeException {        synchronized (this) {            // Open the Stocks file            stocks = new StockDatabase();            try {                stocks.open("Stocks");            } catch (Exception e) {                try {                    stocks.cleanUp("Stocks");                } catch (Exception e2) {                }            }            // Open the Alerts file            alerts = new AlertDatabase();            try {                alerts.open("Alerts");            } catch (Exception e) {                try {                    alerts.cleanUp("Alerts");                } catch (Exception e2) {                }            }            // Create all the menus and forms            origPurchPriceField = new TextField("Original Purchase Price:", "", 5, TextField.NUMERIC);            numSharesField = new TextField("Number Of Shares:", "", 9, TextField.NUMERIC);            menu = new List("Stock Menu", Choice.IMPLICIT);            menu.append("Stock Tracker", null);            menu.append("What If?", null);            menu.append("Alerts", null);            menu.append("Settings", null);            menu.addCommand(EXIT_COMMAND);            menu.setCommandListener(new StockCommandListener());            //menu.setTicker(stockTicker);            whatif = new Form("What If?");            //whatif.setTicker(stockTicker);            whatif.append(origPurchPriceField);            whatif.append(numSharesField);            whatif.addCommand(BACK_COMMAND);            whatif.addCommand(CALC_COMMAND);            whatif.setCommandListener(new StockCommandListener());            alertList = new List("Alert Menu", Choice.IMPLICIT);            //alertList.setTicker(stockTicker);            alertList.append("Add", null);            alertList.append("Remove", null);            alertList.addCommand(BACK_COMMAND);            alertList.setCommandListener(new StockCommandListener());            settingsList = new List("Settings", Choice.IMPLICIT);            //settingsList.setTicker(stockTicker);            settingsList.append("Updates", null);            settingsList.append("Add Stock", null);            settingsList.append("Remove Stock", null);            settingsList.addCommand(BACK_COMMAND);            settingsList.setCommandListener(new StockCommandListener());            alertPriceBox = new TextBox("Alert me when stock reaches:", "", 9, TextField.NUMERIC);            //alertPriceBox.setTicker(stockTicker);            alertPriceBox.addCommand(DONE_COMMAND);            alertPriceBox.addCommand(BACK_COMMAND);            alertPriceBox.setCommandListener(new StockCommandListener());            updatesForm = new Form("Updates");            updatesChoices = new ChoiceGroup("Update Interval:", Choice.EXCLUSIVE);            updatesChoices.append("Continuous", null); // will be 30 seconds            updatesChoices.append("15 minutes", null); // default for JavaONE            updatesChoices.append("30 minutes", null);            updatesChoices.append("1 hour", null);            updatesChoices.append("3 hours", null);            switch (refresh_interval) {            case 30000:                updatesChoices.setSelectedIndex(0, true);                break;            case 1800000:                updatesChoices.setSelectedIndex(2, true);                break;            case 3600000:                updatesChoices.setSelectedIndex(3, true);                break;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -