怎么办。回过去用高级界面?想都不要想。
你打开API手册查阅,希望能找出点什么来。
有了,你眼前一亮,印入眼帘的正是Font类提供的stringWidth函数,该函数能够返回字符串在屏幕上显示时的长度。
有了这个函数,就可以改进getSubsection函数了
其中,strSource是待断行的文字,font是画文字时使用的字体,width是每行的最大宽度,而最后的strSplit是用于分词的,即英文单词中的间隔符号,函数依靠这个参数来分辨单词。
public Vector getSubsection(String strSource,Font font,int width,String strSplit){
Vector vector = new Vector();
String temp=strSource;
int i,j;
int LastLength = 1;
int step = 0;
try{
while (!temp.equals("")) {
i=temp.indexOf("\n");
if(i>0){
if(font.stringWidth(temp.substring(0,i-1)) >= width){
i = -1;
}
}
if(i==-1){
if(LastLength>temp.length()){
i = temp.length();
}else{
i = LastLength;
step = font.stringWidth(temp.substring(0, i)) > width ? -1 : 1;
if(i<temp.length()){
while (! (font.stringWidth(temp.substring(0, i)) <= width
&& font.stringWidth(temp.substring(0, i + 1)) > width)) {
i = i + step;
if (i == temp.length())
break;
}
}
}
if(!strSplit.equals("")){
j = i;
if (i < temp.length()) {
while (strSplit.indexOf(temp.substring(i-1,i))==-1) {
i--;
if (i == 0) {
i = j;
break;
}
}
}
}
}
LastLength = i;
vector.addElement(temp.substring(0, i));
if (i == temp.length()) {
temp = "";
}