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

📄 lcduiui.java

📁 通过蓝牙实现手机和PC通信的例子。。 。。 。。 。
💻 JAVA
字号:
package example.chat;

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 LcduiUI extends Form implements CommandListener,
		ConnectionProtocol.EventHandler {

	/**
	 * A custom item that draws last messages to screen.
	 */
	class MessageArea extends CustomItem {
		private int negHeight;

		protected MessageArea(String label, int negHeight) {
			super(label);
			if (negHeight > LcduiUI.this.getHeight())
				this.negHeight = LcduiUI.this.getHeight() / 4;
			else
				this.negHeight = negHeight;
		}

		protected int getMinContentWidth() {
			return LcduiUI.this.getWidth();
		}

		protected int getMinContentHeight() {
			return LcduiUI.this.getHeight() - negHeight;
		}

		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); // yellow-white
			g.fillRect(0, 0, w, h);

			Font f = Font.getDefaultFont();
			g.setColor(0); // black
			g.setFont(f);

			// paint last messages in screen.
			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);
				}
			}

		}

		/**
		 * Assumes non empty message.
		 * 
		 * @return used row count.
		 */
		private int drawMessage(Graphics g, Font f, String msg, int rowsLeft) {
			int maxWidth = LcduiUI.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 = LcduiUI.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 ConnectionProtocol protocol;

	private TextField field;

	private MessageArea messageArea;

	private Command send = new Command("Send message", Command.SCREEN, 1);

	private Command log = new Command("Log screen", Command.SCREEN, 2);

	public LcduiUI(MIDlet m) {
		super("Chat!");
		this.midlet = m;

		field = new TextField("Msg:", "Hi!", 128, TextField.ANY);
		messageArea = new MessageArea("Messages", 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 + " is leaving."));
		messageArea.refresh();
	}

	public void chatEnter(String userName) {
		messages.addElement(new Message(null, userName + " has joined."));
		messageArea.refresh();
	}

	public void setCurrent() {
		Display.getDisplay(midlet).setCurrent(this);
		messageArea.refresh();
	}

	public void setProtocol(ConnectionProtocol protocol) {
		this.protocol = protocol;
	}

	public void commandAction(Command cmd, Displayable d) {
		if (cmd == send) {
			protocol.broadcastMessage(field.getString());
		} else if (cmd == log) {
			Log.setCurrent(midlet);
		}

	}

}

⌨️ 快捷键说明

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