📄 mainui.java
字号:
import java.util.Date;
import java.util.Vector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.CustomItem;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
public class MainUI extends Form implements CommandListener,DeviceConnection.EventHandler {
//通过继承列表类构造文本域用作公共聊天板
class MessageArea extends CustomItem {
private int inHeight;
protected MessageArea(String label, int inHeight) {
super(label);
if (inHeight > MainUI.this.getHeight())
this.inHeight = MainUI.this.getHeight() / 4;
else
this.inHeight = inHeight;
}
protected int getMinContentWidth() {
return MainUI.this.getWidth();
}
protected int getMinContentHeight() {
return MainUI.this.getHeight() - inHeight;
}
protected int getPrefContentWidth(int arg0) {
return getMinContentWidth();
}
protected int getPrefContentHeight(int arg0) {
return getMinContentHeight();
}
protected void paint(Graphics g, int w, int h) {
g.setColor(0xFFFFA0); // 背景颜色
g.fillRect(0, 0, w, h);
Font f = Font.getDefaultFont();
g.setColor(0); // 字体颜色
g.setFont(f);
synchronized (messages) {
int rowsLeft = getMinimumHeight() / f.getHeight();
if ((getMinimumHeight() % f.getHeight()) > 0)
rowsLeft--;
for (int msgIdx = messages.size() - 1; rowsLeft >= 0&& msgIdx >= 0; msgIdx--)
{
rowsLeft -= drawMessage(g, f, messages.elementAt(msgIdx).toString(), rowsLeft);
}
}
}
//画聊天区
private int drawMessage(Graphics g, Font f, String msg, int rowsLeft) {
int maxWidth = MainUI.this.getWidth();
int begin = 0, end = 0;
int rows = getApproximationOfRowCount(f, msg);
for (int rowIdx = 0; rowIdx < rows; rowIdx++) {
for (; end <= msg.length()&& f.substringWidth(msg, begin, end - begin) < maxWidth; end++)
{
}
end--;
g.drawSubstring(msg, begin, end - begin, 0,
(rowsLeft - rows + rowIdx) * f.getHeight(),
Graphics.TOP | Graphics.LEFT);
begin = end;
}
return rows;
}
private int getApproximationOfRowCount(Font f, String msg) {
int maxWidth = MainUI.this.getWidth();
int strWidth = f.stringWidth(msg);
int rows = strWidth / maxWidth;
if ((strWidth % maxWidth) > 0)
rows++;
return rows;
}
public void refresh() {
repaint();
}
}
class Message {
Date date;
String userName;
String msg;
Message(String userName, String msg) {
this.date = new Date();
this.userName = userName;
this.msg = msg;
}
public String toString() {
if (userName == null)
return msg;
StringBuffer buf = new StringBuffer();
buf.append(userName);
buf.append(": ");
buf.append(msg);
return buf.toString();
}
}
private MIDlet midlet;
private Vector messages = new Vector();
private DeviceConnection protocol;
private TextField field;
private MessageArea messageArea;
private Command send = new Command("发送消息", Command.SCREEN, 1);
//private Command log = new Command("日志", Command.SCREEN, 2);
public MainUI(MIDlet m) {
super("聊天室");
this.midlet = m;
field = new TextField("信息发送栏:", "", 128, TextField.ANY);
messageArea = new MessageArea("信息接收区:", field.getMinimumHeight());
append(messageArea);
append(field);
addCommand(send);
//addCommand(log);
setCommandListener(this);
}
public void chatMessage(String userName, String msg) {
messages.addElement(new Message(userName, msg));
messageArea.refresh();
}
public void chatLeave(String userName) {
messages.addElement(new Message(null, userName + " 已经离开"));
messageArea.refresh();
}
public void chatEnter(String userName) {
messages.addElement(new Message(null, userName + " 已经加入"));
messageArea.refresh();
}
public void setCurrent() {
Display.getDisplay(midlet).setCurrent(this);
messageArea.refresh();
}
public void setProtocol(DeviceConnection protocol) {
this.protocol = protocol;
}
public void commandAction(Command cmd, Displayable d) {
if (cmd == send) {
protocol.broadcastMessage(field.getString());
field.setString("");
} /*else if (cmd == log) {
Log.setCurrent(midlet);
}*/
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -