用java来实现滚动消息
2010-08-08 14:38
232 查看
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
class Test extends Canvas implements Runnable{
// TODO Auto-generated method stub
String msg;
int msgLength;
int x_character=0;
Font wordFont;
int width,height;
Image offImg;
Graphics offG;
Thread runThread;
static boolean stop;
int delay;
public Test(int width,int height){
this.width=width;
this.height=height;
first();
}
public void first(){
wordFont=new Font("TimesRoman",Font.PLAIN+Font.BOLD,15);
msg="小清要加油";
msgLength=msg.length();
delay=600;
stop=false;
runThread=new Thread(this);
runThread.start();
}
public void run(){
while(!stop){
if(x_character++>=msgLength){
x_character=0;
offG=null;
}
repaint();
try{
Thread.sleep(delay);
}catch(InterruptedException e){
}
}
}
public void paint(Graphics g){
g.setFont(wordFont);
g.setColor(Color.red);
g.drawString(msg.substring(0,x_character),10, height/2);
}
public void update(Graphics g){
if(offG!=null){
paint(offG);
g.drawImage(offImg,0,0,this);
}else{
paint(g);
offImg=createImage(width,height);
offG=offImg.getGraphics();
}
}
public static void stop(){
stop=true;
}
public static void main(String[] args){
int width=200;
int height=100;
Test test=new Test(width,height);
JFrame frame=new JFrame("滚动的消息");
frame.setLocation(0,0);
frame.setSize(width,height);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
Test.stop();
System.exit(0);
}
});
frame.getContentPane().add(test);
frame.setVisible(true);
}
}
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
class Test extends Canvas implements Runnable{
// TODO Auto-generated method stub
String msg;
int msgLength;
int x_character=0;
Font wordFont;
int width,height;
Image offImg;
Graphics offG;
Thread runThread;
static boolean stop;
int delay;
public Test(int width,int height){
this.width=width;
this.height=height;
first();
}
public void first(){
wordFont=new Font("TimesRoman",Font.PLAIN+Font.BOLD,15);
msg="小清要加油";
msgLength=msg.length();
delay=600;
stop=false;
runThread=new Thread(this);
runThread.start();
}
public void run(){
while(!stop){
if(x_character++>=msgLength){
x_character=0;
offG=null;
}
repaint();
try{
Thread.sleep(delay);
}catch(InterruptedException e){
}
}
}
public void paint(Graphics g){
g.setFont(wordFont);
g.setColor(Color.red);
g.drawString(msg.substring(0,x_character),10, height/2);
}
public void update(Graphics g){
if(offG!=null){
paint(offG);
g.drawImage(offImg,0,0,this);
}else{
paint(g);
offImg=createImage(width,height);
offG=offImg.getGraphics();
}
}
public static void stop(){
stop=true;
}
public static void main(String[] args){
int width=200;
int height=100;
Test test=new Test(width,height);
JFrame frame=new JFrame("滚动的消息");
frame.setLocation(0,0);
frame.setSize(width,height);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
Test.stop();
System.exit(0);
}
});
frame.getContentPane().add(test);
frame.setVisible(true);
}
}
相关文章推荐
- java翻译到mono C#实现系列(2) mono实现GridView 横向滚动
- iOS 设备通过 java-apns 组件实现苹果 APNs 消息推送实现
- java WebSocket实现聊天消息推送功能
- [分布式java]基于JavaAPI实现消息方式的系统间通信:UDP/IP+BIO
- IOS 基于APNS消息推送原理与实现(JAVA后台)--转
- java实现客户端一键群发消息完整代码
- (转)java实现动态切换上网IP (ADSL拨号上网) java 断网重连 (原)读取cmd消息 乱码解决
- 东方通消息中间件(TongLINK/Q)开发实例和java客户端实现
- JAVA实现 springMVC方式的微信接入、实现消息自动回复
- Java Swing实现的仿QQ气泡消息聊天窗口效果
- Java实现app接口和Socket消息传递(9)navicat创建user表
- java编写简单消息队列,实现高德坐标变形服务
- 聊天功能,实现:每增加一条消息整体往上移动,滚动查看聊天记录
- (10)java代码实现回复music类型的消息
- 实现类似天猫列表消息自动垂直滚动效果
- java实现用户分页消息springmvc+mysql精装版本
- Java实现Redis的消息订阅和发布
- java中建立0-10m的消息(字符串)实现方法
- UGUI实现直播间聊天消息滚动功能
- Java中websocket消息推送的实现代码