首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Java 實作類 MSN/QQ 好友上線彈窗通知:基於 SWT 的動畫彈出效果

     編輯:Java 相關知識

MSN、QQ 等即時通訊軟體的「好友上線」右下角彈窗——從螢幕邊緣滑入、停留數秒後淡出——體驗極佳。本文示範如何使用 Java 與 Eclipse SWT 組件復刻此功能。

為什麼選擇 SWT?

SWT 最初為 Eclipse IDE 開發的圖形 API,運行時優先調用作業系統原生組件,無原生對應才模擬繪製。此機制使其速度遠超 AWT/Swing。詳細資訊請見 eclipse.org/swt。

核心邏輯:座標計算與執行緒動畫

  1. 取得螢幕尺寸:Display.getDefault().getClientArea()
  2. 計算關鍵座標:
    • 最高顯示位置 upPosition = 螢幕高度 - 彈窗高度
    • 初始隱藏位置 downPosition = 螢幕高度 + 彈窗高度(螢幕外下方)
    • 水平靠右 leftPosition = 螢幕寬度 - 彈窗寬度
  3. 執行緒驅動動畫:循環每 10ms 向上移動 moveStep 像素,直到達到 upPosition;停留 5 秒後關閉銷毀。

完整原始碼

主程式: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("Main");
 shell.setSize(250, 150);
 
 final Button button = new Button(shell, SWT.NONE);
 button.setBounds(50, 20, 100, 25);
 button.setText("彈出通知");
 
 button.addSelectionListener(new SelectionAdapter() {
 public void widgetSelected(SelectionEvent e) {
 new Popup("您的好友 xxx 上線了。").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;
 private final int moveStep = 2;
 private int upPosition, downPosition, leftPosition;

 public Popup(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();
 }

 @Override
 public void run() {
 Display display = shell.getDisplay();
 while (true) {
 try {
 Thread.sleep(10);
 if ((downPosition - moveStep) > upPosition) {
 display.asyncExec(() -> {
 shell.setLocation(leftPosition, downPosition - moveStep);
 downPosition -= moveStep;
 });
 } else {
 Thread.sleep(5000);
 display.asyncExec(() -> shell.dispose());
 break;
 }
 } catch (InterruptedException e) {
 e.printStackTrace();
 }
 }
 }
}

效果展示

原始實現效果 修改後界面(類 QQ 風格)

擴展建議

  • 自定義繪製背景、圓角、陰影,提升視覺質感
  • 支援點擊穿透、自動避開任務欄
  • 整合系統通知 API(Windows 10+ Toast、macOS Notification Center)
  • 加入聲音、圖標、超連結等豐富交互

上述代碼已實現核心滑入/停留/退出流程,讀者可據此構建生產級通知組件。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved