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

📄 datagramdemo.java

📁 j2me学习 简单例子
💻 JAVA
字号:
package example.demowireless.datagram;

import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import example.demowireless.datagram.Client;
import example.demowireless.datagram.Server;


/**
 * <p>J2ME Datagram Demo</p>
 * 
 * @author 刘光辉
 * @version 2009.02.04
 */
public class DatagramDemo extends MIDlet implements CommandListener {
	
	private static final String SERVER = "Server";
	private static final String CLIENT = "Client";
	private static final String[] names = {SERVER,CLIENT};
	private static Display display;
	private Form form;
	private ChoiceGroup cg;
	private boolean isPaused;
	private Server server;
	private Client client;
	private Command CMD_EXIT = new Command("Exit", Command.EXIT, 1);
	private Command CMD_START = new Command("Start", Command.ITEM, 1);
	
	
	/**
	 * <p>DatagramDemo的构造函数</p>
	 */
	public DatagramDemo() {
		display = Display.getDisplay(this);
		form = new Form("Datagram Demo");
		cg = new ChoiceGroup("Please select peer", Choice.EXCLUSIVE, names,
		null);
		form.append(cg);
		form.addCommand(CMD_EXIT);
		form.addCommand(CMD_START);
		form.setCommandListener(this);
		display.setCurrent(form);
	}
	public static Display getDisplay(){
		return display;
	}
	
	public boolean isPaused() {
		return isPaused;
	}
	
	/**
	 * <p>启动程序,使MIDlet处于active状态</p>
	 * <p></p>
	 * @see javax.microedition.midlet.MIDlet#startApp()
	 */
	protected void startApp() throws MIDletStateChangeException {
		isPaused = false;
	}
	
	/**
	 * <p>暂停程序,使MIDlet处于pause状态</p>
	 * @see javax.microedition.midlet.MIDlet#pauseApp()
	 */
	protected void pauseApp() {
		isPaused = true;
	}

	/**
	 * <p>销毁程序,使MIDlet处于destroyed状态</p>
	 * 
	 * @param boolean unconditional 
	 * <ul>true 释放资源保存数据进入destroyed状态,false 抛出异常保持当前状态</ul>
	 * @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
	 */
	protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
		if (server != null) {
			server.stop();
		}
		if (client != null) {
			client.stop();
		}
	}

	/**
	 * <p>命令处理函数,实现CommandListener接口</p>
	 * @param Command c 命令对象
	 * @param Displayable d 被放置到显示屏显示的对象
	 */
	public void commandAction(Command c, Displayable d) {
		if (c == CMD_EXIT) {
			try {
				destroyApp(true);
			} catch (MIDletStateChangeException e) {
				e.printStackTrace();
			}
			notifyDestroyed();
		} else if (c == CMD_START) {
			String name = cg.getString(cg.getSelectedIndex());
			if (name.equals(SERVER)) {
				Server server = new Server(this);
				server.start();
			} else {
				Client client = new Client(this);
				client.start();
			}
		}
	}
}

⌨️ 快捷键说明

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