相信大家都使用过MSN,QQ这样的即时聊天类软件,对于它们的好友上线提示功能并不陌生吧?从屏幕右下角弹出一个小界面,慢慢上升,最后消失。我们能不能在自已的程序中也做出相同的功能呢?能!笔者现用JAVA和eclipse的SWT用户界面组件实现这个功能。
什么是SWT呢? SWT原来是eclipse项目组为开发eclipse IDE所编写的图形界面API,运行时,其先判断本机是否有相同的界面元素,如果有则直接调用显示,如没有才进行模拟显示。其运行机制使速度比AWT,SWING快很多。
了解更多请看:http://www.eclipse.org/swt
编写思路
先取得用户屏幕大小,用屏幕高度减去popup界面的高度计算出popup界面在屏幕显示的最高位置(当界面移动到此位置时就停止移动)。
Rectangle area = Display.getDefault().getClientArea(); int upPosition = area.height - 100; |
用屏幕高度加上popup界面的高度就计算出popup界面的初始位置(初始化时不可见,然后慢慢上移到upPosition点后停止移动,再显示若干秒后消失)。
int downPosition = area.height + 100; |
移动位置我们用线程实现,当初始化界面后,调用start()方法运行此线程,在线程中循环判断downPosition的大小是否小于upPosition,如果小于的话说明还未到停止的时候,设置popup界面的边框为downPosition,并暂停10毫秒,如果downPosition大于upPosition的,说明popup界面已移动到了最高位置。调用sleep()暂停5秒钟后关闭界面并退出程序。就这么简单,ok, Let's go! 下面给出整个程序代码:
这是运行后的效果
在这之前要像我下面一样把swt包导进来!
// Test.java

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;


public class Test
{


public static void main(String[] args)
{
final Display display = new Display();
Shell shell = new Shell();
shell.setText("aaa");
shell.setSize(250, 150);
final Button button = new Button(shell, SWT.NONE);
button.setBounds(50, 20, 100, 25);
button.setText("button");

button.addSelectionListener(new SelectionAdapter()
{

public void widgetSelected(SelectionEvent e)
{
System.out.println("click");
Popup popup = new Popup("您的好友xxx上线了。");
popup.start();
}
});
shell.open();

while (!shell.isDisposed())
{

if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
}


//Popup.java

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;


public class Popup extends Thread
{

Shell shell;

protected int moveStep = 2;
protected int upPosition;
protected int downPosition;
protected int leftPosition;


public Popup(final String message)
{

shell = new Shell(SWT.ON_TOP);
Text text = new Text(shell, SWT.MULTI | SWT.WRAP);
text.setBounds(10, 20, 180, 80);
text.setBackground(shell.getBackground());
text.setText(message);
Rectangle area = Display.getDefault().getClientArea();

upPosition = area.height - 100;
downPosition = area.height + 100;
leftPosition = area.width - 180;

shell.setSize(180, 100);
shell.setLocation(leftPosition, downPosition);

shell.open();

}


public void run()
{

Display display = shell.getDisplay();

while (true)
{

try
{
Thread.sleep(10);

if ((downPosition - moveStep) > upPosition)
{

display.asyncExec(new Runnable()
{

public void run()
{
shell.setLocation(
leftPosition,
downPosition - moveStep);
downPosition -= moveStep;
}
});

} else
{
Thread.sleep(5000);

display.asyncExec(new Runnable()
{

public void run()
{
shell.dispose();
}
});
}

} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
posted on 2007-02-05 23:05
EricWong 阅读(492)
评论(0) 编辑 收藏 所属分类:
Java