?? chatclientmidlet.java
字號:
package example5;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.SocketConnection;
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.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class ChatClientMIDlet extends MIDlet implements CommandListener{
private SocketConnection sc = null;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private TextField tfMsg = new TextField("輸入聊天信息","",255,TextField.ANY);
private Command cmdSend = new Command("發送",Command.SCREEN,1);
private Form frmChat = new Form("聊天界面");
private Display display;
private TextField tfNickName = new TextField("輸入昵稱","",10,TextField.ANY);
private Command cmdLogin = new Command("登陸到服務器",Command.SCREEN,1);
protected void startApp() throws MIDletStateChangeException {
display = Display.getDisplay(this);
display.setCurrent(frmChat);
frmChat.addCommand(cmdLogin);
frmChat.append(tfNickName);
frmChat.setCommandListener(this);
}
public void commandAction(Command c,Displayable d){
if(c==cmdSend){
try{
dos.writeUTF(tfMsg.getString());
}catch(Exception ex){}
}
else if(c==cmdLogin){
try{
frmChat.removeCommand(cmdLogin);
frmChat.addCommand(cmdSend);
frmChat.append(tfMsg);//添加發送聊天信息的文本框
new ChatThread().start();
}catch(Exception ex){
ex.printStackTrace();
}
}
}
class ChatThread extends Thread{
public void run(){
try{
sc = (SocketConnection)Connector.open("socket://127.0.0.1:9999");
dis = sc.openDataInputStream();
dos = sc.openDataOutputStream();
dos.writeUTF(tfNickName.getString());//發送昵稱
this.start();
}catch(Exception ex){
ex.printStackTrace();
}
while(true){
try{
String msg = dis.readUTF();
frmChat.append(msg + "\n");
}catch(Exception ex){}
}
}
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -