原文地址http://www.cnblogs.com/bjzhanghao/archive/2007/03/30/694268.html
用Java画动画很简单,让一个线程自己定时调用自己即可,记得要设置一个退出(结束)条件。
import
org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class Animation {
final static int DELAY = 500 ;
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout( new FillLayout());
final Text text = new Text(shell, SWT.BORDER);
text.setText( " 0 " );
new Runnable() {
public void run() {
if (shell.isDisposed())
return ;
text.setText( "" + (Integer.parseInt(text.getText()) + 1 ));
Display.getDefault().timerExec(DELAY, this );
}
}.run();
shell.pack();
shell.open();
while ( ! shell.isDisposed()) {
if ( ! display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class Animation {
final static int DELAY = 500 ;
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout( new FillLayout());
final Text text = new Text(shell, SWT.BORDER);
text.setText( " 0 " );
new Runnable() {
public void run() {
if (shell.isDisposed())
return ;
text.setText( "" + (Integer.parseInt(text.getText()) + 1 ));
Display.getDefault().timerExec(DELAY, this );
}
}.run();
shell.pack();
shell.open();
while ( ! shell.isDisposed()) {
if ( ! display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
运行结果:

数字不停增长
本文介绍了一个简单的Java动画实现方法,通过一个不断自增并显示数字的例子展示了如何利用SWT库创建基本的动画效果。此示例使用了定时器来更新界面上显示的数字。
6609

被折叠的 条评论
为什么被折叠?



