JavaMe开发中怎么绘制可自动换行文本
发表于:2024-10-02 作者:千家信息网编辑
千家信息网最后更新 2024年10月02日,这期内容当中小编将会给大家带来有关JavaMe开发中怎么绘制可自动换行文本,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。【问题描述】JavaMe Graphics类
千家信息网最后更新 2024年10月02日JavaMe开发中怎么绘制可自动换行文本
这期内容当中小编将会给大家带来有关JavaMe开发中怎么绘制可自动换行文本,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
【问题描述】
JavaMe Graphics类中的drawString不支持文本换行,这样绘制比较长的字符串时,文本被绘制在同一行,超过屏幕部分的字符串被截断了。如何使绘制的文本能自动换行呢?
【分析】
drawString无法实现自动换行,但可以实现文本绘制的定位。因此可考虑,将文本拆分为多个子串,再对子串进行绘制。拆分的策略如下:
1 遇到换行符,进行拆分;
2 当字符串长度大于设定的长度(一般为屏幕的宽度),进行拆分。
【步骤】
1 定义一个String和String []对象;
private String info; private String info_wrap[];
2 实现字符串自动换行拆分函数
StringDealMethod.java
package com.token.util; import java.util.Vector; import javax.microedition.lcdui.Font; public class StringDealMethod { public StringDealMethod() { } // 字符串切割,实现字符串自动换行 public static String[] format(String text, int maxWidth, Font ft) { String[] result = null; Vector tempR = new Vector(); int lines = 0; int len = text.length(); int index0 = 0; int index1 = 0; boolean wrap; while (true) { int widthes = 0; wrap = false; for (index0 = index1; index1 < len; index1++) { if (text.charAt(index1) == '\n') { index1++; wrap = true; break; } widthes = ft.charWidth(text.charAt(index1)) + widthes; if (widthes > maxWidth) { break; } } lines++; if (wrap) { tempR.addElement(text.substring(index0, index1 - 1)); } else { tempR.addElement(text.substring(index0, index1)); } if (index1 >= len) { break; } } result = new String[lines]; tempR.copyInto(result); return result; } public static String[] split(String original, String separator) { Vector nodes = new Vector(); //System.out.println("split start..................."); //Parse nodes into vector int index = original.indexOf(separator); while(index>=0) { nodes.addElement( original.substring(0, index) ); original = original.substring(index+separator.length()); index = original.indexOf(separator); } // Get the last node nodes.addElement( original ); // Create splitted string array String[] result = new String[ nodes.size() ]; if( nodes.size()>0 ) { for(int loop=0; loop3 调用拆分函数,实现字符串的拆分
int width = getWidth(); Font ft = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_BOLD,Font.SIZE_LARGE); info = "欢迎使用!\n" +"1 MVC测试;\n" +"2 自动换行测试,绘制可自动识别换行的字符串。\n"; info_wrap = StringDealMethod.format(info, width-10, ft);4 绘制字符串
graphics.setColor(Color.text); graphics.setFont(ft); for(int i=0; i绘制的效果如图1所示:
图1 自动换行字符串绘制效果上述就是小编为大家分享的JavaMe开发中怎么绘制可自动换行文本了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注行业资讯频道。
字符
字符串
文本
分析
开发
内容
函数
屏幕
效果
长度
测试
一行
专业
中小
内容丰富
多个
宽度
对子
对象
就是
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
香肠派对怎么申请服务器
数据库数据为空要求显示暂无
计算机网络安全传输层
航空安全数据库有哪些
怎么看服务器是ssd硬盘
宜昌极客网络技术
丹络康网络技术
湖南怎么考软件开发证书
怎么比较两个数据库的一致性
信息网络安全监察部门
苏州易汇宝网络技术
一个网站服务器开多少个端
使用jdbc删除数据库
断桥残雪服务器
数据库网络维护工资
东莞手机软件开发中心
数据库中order语句的使用
港湾网络技术骨干
软件开发前段后端
公安网络安全计划
中文科技期刊数据库是一般期刊吗
数据库undo list
郑州大学网络安全和信息化
怎么看服务器的型号
sql如何显示数据库前三条记录
lol 训练模式服务器
网络技术追款
计算机软件开发就业前景分析
互联网加实体科技加盟商
船上网络安全演习