java 仿qq消息提示框

引用包:

swt.jar


主类:

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

public class Z {
	
	public static void main(String[] args) {

		final Display display = new Display();
		final Shell shell = new Shell();
		shell.setText("aaa");
		shell.setSize(250, 150);

		
		shell.addShellListener(new ShellAdapter() {
			   @Override
			   public void shellClosed(ShellEvent e) {
			      MessageBox mb = new MessageBox(shell,SWT.ICON_QUESTION | SWT.OK| SWT.CANCEL);
			                  mb.setText("提示");
			                  mb.setMessage("确定要关闭吗?");
			                 int rc = mb.open();
			                        if (e.doit == (rc == SWT.OK)) {
			                        System.exit(0);
			                   }
			                  else if(e.doit == (rc == SWT.CANCEL)) {
			                    return;
			                   }
			   }
			  });

		
		

		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("更新信息");
				popup.start();
			}
		});
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		display.dispose();
	}
}


方法类:

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(String message) {
		// TODO Auto-generated constructor stub
		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();
			}
		}
	}
}


shell 窗口关闭时出现是否关闭提示:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值