
java中swt如何实现跑马灯的效果
1个回答
展开全部
Java中swt界面设计跑马灯
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;
public class TestMove {
private boolean stop;
private String str;
private int mx,x,width;
private Rectangle clientArea;
private Canvas canvas;
private int step=1;
private int speed = 40;
private Shell shell = null;
private Color color = null;
private int fontSize = 20;
private String fontName = "幼圆";
private Display display = null;
GC gc;
public TestMove(Display display,Shell shell,String soucestr){
this.display = display;
this.shell = shell;
canvas = new Canvas(shell,SWT.NONE);
gc=new GC(canvas);
stop=true;
clientArea=shell.getClientArea();
mx=clientArea.width;
x=mx;
str=soucestr;
width=strlen(soucestr);
color = display.getSystemColor(SWT.COLOR_BLUE);
FontData fd = new FontData(fontName,fontSize,SWT.NORMAL);
Font font = new Font(canvas.getDisplay(),fd);
gc.setForeground(color);
gc.setFont(font);
start(display);
}
public void setFontSize(int size){
fontSize = size;
FontData fd = new FontData(fontName,fontSize,SWT.NORMAL);
Font font = new Font(canvas.getDisplay(),fd);
gc.setFont(font);
}
public void setFontName(String fontName){
this.fontName = fontName;
FontData fd = new FontData(fontName,fontSize,SWT.NORMAL);
Font font = new Font(canvas.getDisplay(),fd);
gc.setFont(font);
}
public void setColor(int c){
if(c>=1&&c<=16){
color = display.getSystemColor(c);
gc.setForeground(color);
}
}
public void setColor(Color c){
this.color = c;
gc.setForeground(color);
}
public void setSpeed(int speed){
this.speed = speed;
}
public void setString(String str){
this.str = str;
width=strlen(str);
}
public void refreshClientArea(){
this.clientArea = shell.getClientArea();
mx = clientArea.width;
x=mx;
}
private int strlen(String str){
int strwidth=0;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
strwidth += gc.getAdvanceWidth(c);
}
return strwidth;
}
public void start(Display display){
new BackgroundThread(display).start();
}
class BackgroundThread extends Thread{
private Display display;
public BackgroundThread(Display display){
this.display=display;
};
public void run(){
while(true){
if(stop){
display.syncExec(new Runnable(){
public void run(){
if(width<=clientArea.width)
{
if(mx>0)
{
gc.drawString(str, mx, 0);
}
else
{
gc.drawString(str, mx, 0);
gc.drawString(str, x, 0);
if(mx==-width)
{
mx=x+step;
x=clientArea.width;
}
else
{
x-=step;
}
}
mx-=step;
}
else
{
gc.drawString(str, mx, 0);
if(mx>-width)
{
mx-=step;
}
else
{
mx=clientArea.width;
}
}
}
});
}
try{
Thread.sleep(speed);
}catch(Exception e){
e.printStackTrace();
}
}
}
}
public void pause(){
stop=false;
}
public void resume(){
stop=true;
}
}
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;
public class TestMove {
private boolean stop;
private String str;
private int mx,x,width;
private Rectangle clientArea;
private Canvas canvas;
private int step=1;
private int speed = 40;
private Shell shell = null;
private Color color = null;
private int fontSize = 20;
private String fontName = "幼圆";
private Display display = null;
GC gc;
public TestMove(Display display,Shell shell,String soucestr){
this.display = display;
this.shell = shell;
canvas = new Canvas(shell,SWT.NONE);
gc=new GC(canvas);
stop=true;
clientArea=shell.getClientArea();
mx=clientArea.width;
x=mx;
str=soucestr;
width=strlen(soucestr);
color = display.getSystemColor(SWT.COLOR_BLUE);
FontData fd = new FontData(fontName,fontSize,SWT.NORMAL);
Font font = new Font(canvas.getDisplay(),fd);
gc.setForeground(color);
gc.setFont(font);
start(display);
}
public void setFontSize(int size){
fontSize = size;
FontData fd = new FontData(fontName,fontSize,SWT.NORMAL);
Font font = new Font(canvas.getDisplay(),fd);
gc.setFont(font);
}
public void setFontName(String fontName){
this.fontName = fontName;
FontData fd = new FontData(fontName,fontSize,SWT.NORMAL);
Font font = new Font(canvas.getDisplay(),fd);
gc.setFont(font);
}
public void setColor(int c){
if(c>=1&&c<=16){
color = display.getSystemColor(c);
gc.setForeground(color);
}
}
public void setColor(Color c){
this.color = c;
gc.setForeground(color);
}
public void setSpeed(int speed){
this.speed = speed;
}
public void setString(String str){
this.str = str;
width=strlen(str);
}
public void refreshClientArea(){
this.clientArea = shell.getClientArea();
mx = clientArea.width;
x=mx;
}
private int strlen(String str){
int strwidth=0;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
strwidth += gc.getAdvanceWidth(c);
}
return strwidth;
}
public void start(Display display){
new BackgroundThread(display).start();
}
class BackgroundThread extends Thread{
private Display display;
public BackgroundThread(Display display){
this.display=display;
};
public void run(){
while(true){
if(stop){
display.syncExec(new Runnable(){
public void run(){
if(width<=clientArea.width)
{
if(mx>0)
{
gc.drawString(str, mx, 0);
}
else
{
gc.drawString(str, mx, 0);
gc.drawString(str, x, 0);
if(mx==-width)
{
mx=x+step;
x=clientArea.width;
}
else
{
x-=step;
}
}
mx-=step;
}
else
{
gc.drawString(str, mx, 0);
if(mx>-width)
{
mx-=step;
}
else
{
mx=clientArea.width;
}
}
}
});
}
try{
Thread.sleep(speed);
}catch(Exception e){
e.printStackTrace();
}
}
}
}
public void pause(){
stop=false;
}
public void resume(){
stop=true;
}
}
追问
有点没看懂啊 能不能给个方法让我运行一下
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询