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

📄 udpscreen.java

📁 手机游戏设计一书的源代码
💻 JAVA
字号:
package net;import  javax.microedition.lcdui.*;import  javax.microedition.io.*;import  java.io.*;/***  MIDlet显示类*/public class UDPScreen extends Form        implements CommandListener {/***构造器*/    public UDPScreen (UDPMIDlet udpMIDlet) {        super("Displayable Title");        this.udpMIDlet = udpMIDlet;        try {            jbInit();        } catch (Exception e) {            e.printStackTrace();        }    }/***  组件初始化*/    private void jbInit () throws Exception {        // 设置当前对象为命令事件监听器        setCommandListener(this);        // 添加退出命令        addCommand(new Command("Exit", Command.EXIT, 1));        addCommand(new Command("Connect", Command.OK, 1));    }    class UDPConnector            implements java.lang.Runnable {        /**         *  线程运行主方法         */        public void run () {            connect();        }    }    /**     *  与UDP服务器通信的连接核心方法     */    public void connect () {        Datagram revData = null;        Datagram sendData = null;        try {            UDPDatagramConnection connection = (UDPDatagramConnection)Connector.open("datagram://jerrylin-w2kcn.sybase.com:5188");            String clientMsg = "Hello Server!";            sendData = connection.newDatagram(clientMsg.getBytes(), clientMsg.length());            connection.send(sendData);            revData = connection.newDatagram(connection.getMaximumLength());            connection.receive(revData);            connection.close();        } catch (IOException ex) {            ex.printStackTrace();        }        TextBox input = new TextBox("Website content:", "", 50000, TextField.ANY);        input.setTicker(new Ticker("connect internet"));        input.setString(new String(revData.getData()));        sendData = null;        revData = null;        Display.getDisplay(udpMIDlet).setCurrent(input);    }/***处理命令事件*/    public void commandAction (Command command, Displayable displayable) {        if (command.getCommandType() == Command.EXIT) {//退出MIDletUDPMIDlet.quitApp();        }        else if (command.getCommandType() == Command.OK) {            new java.lang.Thread(new UDPConnector()).start();        }    }    private UDPMIDlet udpMIDlet = null;}

⌨️ 快捷键说明

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