如何使用 J2ME 在 Blackberry 中显示类似 marque 的滚动文本?

编程入门 行业动态 更新时间:2024-10-27 15:24:40
本文介绍了如何使用 J2ME 在 Blackberry 中显示类似 marque 的滚动文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何使用 J2ME 在 Blackberry 中显示滚动条,如选取框文本?那是从左向右移动还是垂直移动?任何帮助将不胜感激.

How can I display scroll like marquee text in Blackberry using J2ME? That moves from left to right or vertically? Any help will be very much appreciated.

推荐答案

它真的很容易显示.检查下面的代码.

its really easy to display. check the code below .

import java.util.Timer; import java.util.TimerTask; import net.rim.device.api.ui.DrawStyle; import net.rim.device.api.ui.Field; import net.rim.device.api.ui.Font; import net.rim.device.api.ui.Graphics; import net.rim.device.api.ui.UiApplication; import net.rim.device.api.uiponent.LabelField; import net.rim.device.api.ui.container.MainScreen; public class MarqueeApp extends UiApplication { public MarqueeApp() { pushScreen(new MarqueeScreen()); } public static void main(String[] args) { (new MarqueeApp()).enterEventDispatcher(); } } class MarqueeScreen extends MainScreen { public MarqueeScreen() { setTitle(new LabelField("hi")); MarqueeLabel testLabel1 = new MarqueeLabel("This is a marquee", Field.FOCUSABLE); add(testLabel1); MarqueeLabel testLabel2 = new MarqueeLabel("This is a long long " + "long long long long long long long long long long long " + "long long marquee", Field.FOCUSABLE); add(testLabel2); } } class MarqueeLabel extends LabelField { int currentChar = 0; String currentText = null; Font ourFont; private Timer _scrollTimer; private TimerTask _scrollTimerTask; public MarqueeLabel(String text, long style) { super(text, style); } public void paint(Graphics graphics) { currentText = this.getText(); if (currentChar < currentText.length()) { currentText = currentText.substring(currentChar); } graphics.drawText(currentText, 0, 0, DrawStyle.ELLIPSIS, 200); super.paint(graphics); } public void layout(int width, int height) { ourFont = this.getFont(); setExtent(500, ourFont.getHeight()); } protected void onDisplay() { startScroll(); } protected void onUnfocus() { startScroll(); } private void startScroll() { // Start scrolling final String fullText = this.getText(); if (_scrollTimer == null) { _scrollTimer = new Timer(); _scrollTimerTask = new TimerTask() { public void run() { currentChar = currentChar + 4; if (currentChar > fullText.length()) { currentChar = 0; } invalidate(); } }; _scrollTimer.scheduleAtFixedRate(_scrollTimerTask, 0, 300); } } protected void onFocus(int direction) { if (_scrollTimer != null) { _scrollTimerTask.cancel(); _scrollTimer.cancel(); _scrollTimer = null; _scrollTimerTask = null; } } protected boolean navigationMovement(int dx, int dy, int status, int time) { currentText = this.getText(); int oldCurrentChar = currentChar; if (Math.abs(dx) > Math.abs(dy)) { // horizontal scroll if (dx > 0) { currentChar = Math.min(currentText.length() - 1, currentChar + 1); } else if (dx < 0) { currentChar = Math.max(0, currentChar - 1); } if (oldCurrentChar != currentChar) { this.invalidate(); } return true; } else { return super.navigationMovement(dx, dy, status, time); } } }

更多推荐

如何使用 J2ME 在 Blackberry 中显示类似 marque 的滚动文本?

本文发布于:2023-11-27 23:49:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1640103.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   文本   类似   Blackberry   J2ME

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!