socketmidlet.java

来自「J2ME socket通信示例代码」· Java 代码 · 共 67 行

JAVA
67
字号
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;


public class SocketMidlet extends MIDlet implements CommandListener{

	private static final String SERVER="服务器";
	private static final String CLIENT="客户端";
	private static final String[] names={SERVER,CLIENT};
	private static Display display;
	private Form f;
	private ChoiceGroup cg;
	private boolean isPaused;
	private Server server;
	private Client client;
	private Command exitCommand=new Command("退出",Command.EXIT,1);
	private Command startCommand=new Command("启动",Command.ITEM,1);
	
	public SocketMidlet() {
		display=Display.getDisplay(this);
		f=new Form("Socket连接示例");
		cg=new ChoiceGroup("选择端点类型",Choice.EXCLUSIVE,names,null);
		f.append(cg);
		f.addCommand(exitCommand);
		f.addCommand(startCommand);
		f.setCommandListener(this);
		display.setCurrent(f);
	}

	public boolean isPaused(){
		return isPaused;
	}
	protected void destroyApp(boolean unconditional){
		if(server!=null){
			server.stop();
		}
		if(client!=null){
			client.stop();
		}
	}

	protected void pauseApp() {
		isPaused=true;
	}

	protected void startApp() throws MIDletStateChangeException {
		isPaused=false;
	}

	public void commandAction(Command c, Displayable d) {
		if(c==exitCommand){
			destroyApp(true);
			notifyDestroyed();
		}else if(c==startCommand){
			String name=cg.getString(cg.getSelectedIndex());
			if(name.equals(SERVER)){
				server=new Server(this);
				server.start();
			}else{
				client=new Client(this);
				client.start();
			}
		}
	}

}

⌨️ 快捷键说明

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