相信大家都使用过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! 下面给出整个程序代码:
描述:
(test为主界面,点击上面的button后,调用popup在右下角显示像msn和qq一样的popup界面。)
图一为源代码中的实现,图二为修改过后的界面(和qq的有点像吧。)


图一 图二
源代码:
// test.java
//主界面,其中只有一个button,当点击时调用popup在右下角显示像msn和qq一样的popup界面。
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的事件,当用户点击时调用popup类显示popup界面。
button.addselectionlistener(new selectionadapter() {
public void widgetselected(selectionevent e) {
//实例化popup类,构造函数为popup界面中出现的提示信息。
popup popup = new popup("您的好友xxx上线了。");
popup.start();
}
});
shell.open();
while (!shell.isdisposed()) {
if (!display.readanddispatch()) {
display.sleep();
}
}
display.dispose();
}
}
// popup.java
//实现像msn,qq一样的好友上线通知popup
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; //每次移动的pixel
protected int upposition; //能移动到的最上面坐标
protected int downposition; //当前popup的边框坐标
protected int leftposition; //popup左边边框坐标 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;//计算出popup界面在屏幕显示的最高位置
downposition = area.height + 100;//计算出popup界面的初始位置
leftposition = area.width - 180;
shell.setsize(180, 100);
//初始化popup位置
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;
}
});
//此时已经移动到了最高位置,显示5秒钟后,关闭窗口并退出。
} else {
thread.sleep(5000);
display.asyncexec(new runnable() {
public void run() {
shell.dispose();
}
});
}
} catch (interruptedexception e) {
e.printstacktrace();
}
}
}
}上面的源程序就完成了图一中的功能,读者可以自行修改,让其界面,功能更强大。
闽公网安备 35060202000074号