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

📄 chatform.java

📁 J2ME开发的手机socket 聊天室源码。
💻 JAVA
字号:
package client;

//import com.nec.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;

import server.Sender;

import java.io.*;

public class ChatForm extends Form implements CommandListener, Runnable {
	public Form mainForm = new Form("聊天室");

	public Command enter = new Command("进入", Command.OK, 0);

	public Command send = new Command("发送", Command.OK, 1);

	public Command exit = new Command("退出", Command.EXIT, 0);

	public DataInputStream dis;

	public DataOutputStream dos;

	public SocketConnection sc;

	public Sender sender;

	public boolean stop;

	public TextField textField = new TextField("请输入昵称:", null, 10,
			TextField.ANY);

	public TextField info = new TextField("请输入消息:", null, 255, TextField.ANY);

	public ChoiceGroup choiceGroup = new ChoiceGroup(null,
			Choice.EXCLUSIVE);

	public String MyName = "游客";

	public boolean isCurrent = false;;

	public ChatForm() {
		super("我的聊天室");
		append(textField);
		addCommand(enter);
		mainForm.append(info);
		mainForm.append(choiceGroup);
		choiceGroup.append("www.modia.cn", null);
		setCommandListener(this);
		mainForm.addCommand(send);
		mainForm.addCommand(exit);
		mainForm.setCommandListener(this);
	}

	public void commandAction(Command c, Displayable dis) {
		if (c == enter) {
			if (textField.getString().length() == 0) {
				Alert alert = new Alert("警告", "昵称不能为空!", null,
						AlertType.WARNING);
				alert.setTimeout(3000);
				ChatClientMIDlet.setCurrent(alert);
				return;
			} else {
				MyName = textField.getString();
				append("正在进入......");
			}
			Thread t = new Thread(this);
			t.start();
		} else if (c == send) {
			if (info.getString().length() == 0) {
				Alert alert = new Alert("警告", "消息内容不能为空!", null,
						AlertType.WARNING);
				alert.setTimeout(3000);
				ChatClientMIDlet.setCurrent(alert);
				return;
			}
			sender.send(MyName + "说:" + info.getString());
			info.setString("");
		} else {
			stop();
			ChatClientMIDlet.quitApp();
		}
	}

	public void run() {
		try {
			sc = (SocketConnection) Connector.open("socket://127.0.0.1:5000");
			sc.setSocketOption(SocketConnection.LINGER, 5);
			dis = new DataInputStream(sc.openInputStream());
			dos = new DataOutputStream(sc.openOutputStream());
			sender = new Sender(dos);
			sender.send("欢迎" + MyName + "进入房间");
			while (true) {
				if (stop)
					break;
				StringBuffer sb = new StringBuffer();
				char c;
				while ((c = dis.readChar()) != '\n' && c != -1)
					sb.append(c);
				if (c == -1)
					break;
				if (!isCurrent) {
					ChatClientMIDlet.setCurrent(mainForm);
					isCurrent = true;
				}
				String msg = sb.toString();
				msg = msg.substring(0, msg.length() - 2);
				choiceGroup.insert(0, msg, null);
				choiceGroup.setSelectedIndex(0, true);
			}
			stop();
		} catch (ConnectionNotFoundException cnfe) {
		} catch (IOException ioe) {
			if (!stop)
				ioe.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void stop() {
		try {
			stop = true;
			if (sender != null)
				sender.stop1();

			if (dis != null)
				dis.close();

			if (dos != null)
				dos.close();

			if (sc != null)
				sc.close();
		} catch (IOException ioe) {
		}
	}

}

⌨️ 快捷键说明

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