📄 textblock.java
字号:
package com.ismyway.fairyui;import java.util.Vector;import javax.microedition.lcdui.Graphics;import com.ismyway.anyview.others.Settings;import com.ismyway.util.Theme;public class TextBlock extends DisplayableBlock { protected String text = ""; protected Vector lines = new Vector(); protected boolean wrap = false; //是否换行 private boolean forcewrap = false; public TextBlock() { } public TextBlock(String text) { this.text = text; } public TextBlock(String text, boolean forcewrap) { this.text = text; this.forcewrap = forcewrap; } public void paint(Graphics g, int adjustx, int adjusty) { //System.out.println(getInnerLeft() + ", " + getInnerTop() + ", " + getLeft() + ", " + getTop()); int fontHeight = getFont().getHeight(); g.setFont(getFont()); int top = getTop() - adjusty; int panelHeight = MainCanvas.getInstance().getHeight(); if (!isSelected()) { if (lines.size() > 0) { g.setColor(Theme.TextColor); g.drawString((String) lines.elementAt(0), getLeft() - adjustx, top, ANCHOR); for (int i = 1; i < lines.size(); i++) { if (top > panelHeight) { return; } top += getFont().getHeight() + HORPADDING; if (top < 0) { continue; } g.drawString((String) lines.elementAt(i), VECPADDING - adjustx, top, ANCHOR); } } } else { if (lines.size() > 0) { g.setColor(Theme.linkColor); g.fillRect(getLeft() - adjustx, top, getFont().stringWidth((String) lines.elementAt(0)), fontHeight); g.setColor(Theme.linkBackground); g.drawString((String) lines.elementAt(0), getLeft() - adjustx, top, ANCHOR); for (int i = 1; i < lines.size(); i++) { if (top > panelHeight) { //System.out.println("tb返回:" + i); return; } top += getFont().getHeight() + HORPADDING; if (top < 0) { continue; } g.setColor(Theme.linkColor); g.fillRect(VECPADDING - adjustx, top, getFont().stringWidth((String) lines.elementAt(i)), fontHeight); g.setColor(Theme.linkBackground); g.drawString((String) lines.elementAt(i), VECPADDING - adjustx, top, ANCHOR); } } } } public boolean isPointerIn(int x, int y) { if (!wrap) { //未折行 if (x > getLeft() && x < getLeft() + getFont().stringWidth(text) && y > getTop() && y < getTop() + getFont().getHeight()) { return true; } } else { String str = (String) lines.elementAt(0); if (x > getLeft() && x < getLeft() + getFont().stringWidth(str) && y > getTop() && y < getTop() + getFont().getHeight()) { return true; } int top = getTop() + Settings.HORPADDING + getFont().getHeight(); for (int i = 1; i < lines.size(); i++) { str = (String) lines.elementAt(i); if (x > Settings.VECPADDING && x < Settings.VECPADDING + getFont().stringWidth(str) && y > top && y < top + getFont().getHeight()) { return true; } top += Settings.HORPADDING + getFont().getHeight(); } } return false; } public String getText() { return text; } public void setText(String text) { this.text = text; if (!wrap && lines.size() == 1) { lines.removeElementAt(0); lines.addElement(text); } } public void putInSequence(final int[] rect, int width) { lines.removeAllElements(); wrap = false; setInnerLeft(rect[0]); if (rect[3] > getFont().getHeight()) { setInnerTop(rect[1] + rect[3] - getFont().getHeight() - HORPADDING); } else { setInnerTop(rect[1]); } int rest = rect[2]; int index = 0; int top = getInnerTop(); int chinesewidth = getFont().charWidth('汉'); StringBuffer sb = new StringBuffer(); while (true) { if (index > text.length() - 1) { break; } char ch = text.charAt(index++); int charwidth = chinesewidth; if (ch < 127) { //英文 charwidth = getFont().charWidth(ch); if (ch == '') { lines.addElement(sb.toString()); sb = new StringBuffer(); rest = width; top += getFont().getHeight() + HORPADDING; wrap = true; continue; } } if (rest > charwidth) { sb.append(ch); rest -= charwidth; } else { lines.addElement(sb.toString()); sb = new StringBuffer(); sb.append(ch); rest = width - charwidth; top += getFont().getHeight() + HORPADDING; wrap = true; } } int r0 = rect[0]; int r1 = rect[1]; int r2 = rect[2]; //缓冲区中仍然有字符 lines.addElement(sb.toString()); if (forcewrap) { rect[0] = 0; rect[1] = top + getFont().getHeight() + HORPADDING; rect[2] = width + VECPADDING; rect[3] = 1; } else { if (wrap) { setWidth(width - rest); rect[0] = width - rest + VECPADDING; rect[1] = top; rect[2] = rest; rect[3] = getFont().getHeight() + HORPADDING; } else { setWidth(r2 - rest); rect[0] = r0 + r2 - rest; rect[1] = top; rect[2] = rest; rect[3] = getFont().getHeight() + HORPADDING; } } if (!wrap) { setHeight(getFont().getHeight()); } else { setHeight(top - r1 + getFont().getHeight()); } return; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -