Topic: 模仿MSN用户上线popup的例子

  Print this page

1.模仿MSN用户上线popup的例子 Copy to clipboard
Posted by: elliott
Posted on: 2005-01-26 04:42

写得很烂,只是简单的实现了而已.在这里学到了很多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("elliott 对 你dddddddddddddd 说:this is test message");
        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();
      }
    }
  }
}

2.Re:模仿MSN用户上线popup的例子 [Re: elliott] Copy to clipboard
Posted by: eva001
Posted on: 2005-01-27 10:55

非常有趣,是个很好的例子哦
谢谢

3.Re:模仿MSN用户上线popup的例子 [Re: elliott] Copy to clipboard
Posted by: rocexwang
Posted on: 2005-02-01 14:48

好,赞一个。

4.Re:模仿MSN用户上线popup的例子 [Re: elliott] Copy to clipboard
Posted by: ildg
Posted on: 2005-02-09 10:35

楼主的这个popup类写得很好,赞一个。
关于关闭的时候会发生异常,
需要添加几句检验shell或者display是否被释放了:
public void run()
{

Display display = shell.getDisplay();
while (true)
{
try
{
Thread.sleep(10);
if ((downPosition - moveStep) > upPosition)
{
if (!display.isDisposed()) //添加此句
display.syncExec(new Runnable()
{
public void run()
{
if (!shell.isDisposed()) //此处添加
shell.setLocation(leftPosition,
downPosition - moveStep);
downPosition -= moveStep;
}
});
} else
{
Thread.sleep(5000);
if (!display.isDisposed()) //此处添加
display.syncExec(new Runnable()
{
public void run()
{
if (!shell.isDisposed()) //此处添加
shell.dispose();
}
});
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}

5.Re:模仿MSN用户上线popup的例子 [Re: elliott] Copy to clipboard
Posted by: huangry
Posted on: 2005-02-22 10:22

好帖 帮你顶起来

我觉得这样的应该加精阿~


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923