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

📄 chatscreen.java

📁 J2ME MIDP_Example_Applications
💻 JAVA
字号:
//// Copyright 2002 Nokia Corporation.//// THIS SOURCE CODE IS PROVIDED 'AS IS', WITH NO WARRANTIES WHATSOEVER,// EXPRESS OR IMPLIED, INCLUDING ANY WARRANTY OF MERCHANTABILITY, FITNESS// FOR ANY PARTICULAR PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE// OR TRADE PRACTICE, RELATING TO THE SOURCE CODE OR ANY WARRANTY OTHERWISE// ARISING OUT OF ANY PROPOSAL, SPECIFICATION, OR SAMPLE AND WITH NO// OBLIGATION OF NOKIA TO PROVIDE THE LICENSEE WITH ANY MAINTENANCE OR// SUPPORT. FURTHERMORE, NOKIA MAKES NO WARRANTY THAT EXERCISE OF THE// RIGHTS GRANTED HEREUNDER DOES NOT INFRINGE OR MAY NOT CAUSE INFRINGEMENT// OF ANY PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OWNED OR CONTROLLED// BY THIRD PARTIES//// Furthermore, information provided in this source code is preliminary,// and may be changed substantially prior to final release. Nokia Corporation// retains the right to make changes to this source code at any time,// without notice. This source code is provided for informational// purposes only.////// Nokia and Nokia Connecting People are registered trademarks of Nokia// Corporation. Java and all Java-based marks are trademarks or registered// trademarks of Sun Microsystems, Inc. Other product and company names// mentioned herein may be trademarks or trade names of their respective owners.//// A non-exclusive, non-transferable, worldwide, limited license is hereby// granted to the Licensee to download, print, reproduce and modify the// source code. The licensee has the right to market, sell, distribute and// make available the source code in original or modified form only when// incorporated into the programs developed by the Licensee. No other// license, express or implied, by estoppel or otherwise, to any other// intellectual property rights is granted herein.//import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import java.util.*;class ChatScreen    extends Canvas    implements CommandListener{    private final ChatMIDlet parent;    private final Vector chatText = new Vector();    private final Font font;    private final int lineHeight, lineWidth, maxLines;    private final Command toCommand, quitCommand;    private final String prompt;    private Command writeCommand = null;    private int topLine = 0;    ChatScreen(ChatMIDlet parent, String prompt)    {        this.parent = parent;        this.prompt = prompt;        font = Font.getFont(Font.FACE_PROPORTIONAL,                            Font.STYLE_PLAIN,                            Font.SIZE_SMALL);        lineWidth = getWidth() - 2;        lineHeight = font.getHeight() + 1;        maxLines = (getHeight() - 2) / lineHeight;        toCommand = new Command("To", Command.SCREEN, 1);        quitCommand = new Command("Quit", Command.EXIT, 2);        addCommand(toCommand);        addCommand(quitCommand);        setCommandListener(this);    }    void update(String text)    {        if (font.stringWidth(text) > lineWidth)        {            // seperate message text into multiple lines and            // add each to chatText            int offset = 0;            int len = 1;            do            {                // calculate the max lenth of substring fitting one screen line                while (((offset + len) <= text.length()) &&                       (font.substringWidth(text, offset, len) <= lineWidth))                {                    len++;                }                chatText.addElement(text.substring(offset, offset + len - 1));                offset += len - 1;                len = 1;            } while (offset + len <= text.length());        }        else        {            chatText.addElement(text);        }        if (chatText.size() > maxLines)        {            topLine = chatText.size() - maxLines;        }        repaint();    }    public void paint(Graphics g)    {        g.setColor(0xFFFFFF);        g.fillRect(0, 0, getWidth(), getHeight());        g.setColor(0);        g.setFont(font);        if (chatText.size() == 0)        {            g.drawString(prompt, 1, 1, (Graphics.TOP | Graphics.LEFT));        }        else        {            for (int i = 0; i < maxLines && i < chatText.size(); i++)            {                g.drawString((String) chatText.elementAt(topLine + i),                             1,                             (lineHeight * i + 1),                             (Graphics.TOP | Graphics.LEFT));            }        }    }    protected void keyReleased(int keyCode)    {        switch (getGameAction(keyCode))        {            case Canvas.UP:                if (topLine != 0)                {                    topLine--;                    repaint();                }                break;            case Canvas.DOWN:                if ((topLine + maxLines) < chatText.size())                {                    topLine++;                    repaint();                }                break;            default:                break;        }    }    public void commandAction(Command c, Displayable d)    {        if (c == toCommand)        {            parent.chatScreenChoosePeer();        }        else if (c == writeCommand)        {            parent.chatScreenWriteMessage();        }        else if (c == quitCommand)        {            parent.chatScreenQuit();        }    }    void enableWriteCommand()    {        if (writeCommand == null)        {            writeCommand = new Command("Write", Command.SCREEN, 1);            addCommand(writeCommand);        }    }}

⌨️ 快捷键说明

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