用swt寫的彈出式對話方塊

來源:互聯網
上載者:User

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(2000);
     display.asyncExec(new Runnable() {
      public void run() {
       shell.dispose();
      }
     });
    }
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.