类似qq上线提示的一个java程序

来源:互联网 发布:医学 教学 仿真软件 编辑:程序博客网 时间:2024/04/28 04:29

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
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);
 
  shell.setBackground(new Color(Display.getCurrent(), 23, 184, 237));
  Text text = new Text(shell, SWT.MULTI | SWT.WRAP);
  text.setBounds(10, 20, 180, 80);
  text.setBackground(shell.getBackground()); 
  text.setForeground(new Color(Display.getCurrent(), 221, 55, 84));
  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(6000);
     display.asyncExec(new Runnable() {
      public void run() {
       shell.dispose();
      }
     });
    }
   } catch (InterruptedException e) {
   e.printStackTrace();
   }
  }
 }

原创粉丝点击