📄 textajust.java
字号:
package demo;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
public class TextAjust {
//功能: 计算需要换行的位置
//str: 需要显示的文字
//font: 文字的字体
//linewd: 每行的宽度限制
static public int ChangLine(String str, Font font, int linewd )
{
int wd = 0;
char ch;
for (int i = 0; i < str.length(); i++)
{
ch = str.charAt(i);
if (ch == '\n')
return i + 1;
wd += font.charWidth(ch);
if (wd > linewd)
return i;
}
return 0;
}
//功能: 分行显示字符串
//g: 当前显示的缓冲
//strText: 显示的字符串
//linewd: 每行的宽度限制
//x,y: 字符串左上角显示的位置
//yDis: 显示文字时每行间隔的距离
static public void AjustDrawString( Graphics g, String strText,
int linewd, int x, int y, int yDis )
{
String subStr;
int nPos; //需要换行的位置
while (true)
{
nPos = ChangLine(strText, g.getFont(), linewd );
if (nPos == 0)
{
g.drawString( strText, x, y, 0);
break;
}
else
{
if (strText.charAt(nPos - 1) == '\n' )
subStr = strText.substring(0, nPos - 1);
else
subStr = strText.substring(0, nPos);
g.drawString( subStr, x, y, 0);
strText = strText.substring(nPos, strText.length());
y = y + yDis;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -