2008-09-05 3 views
3

이이보다 자바에서 윈도우를 플래시 할 수있는 더 좋은 방법인가 사용자의 관심을 사로 잡아합니다 :자바 : 플래시 창이

public static void flashWindow(JFrame frame) throws InterruptedException { 
     int sleepTime = 50; 
     frame.setVisible(false); 
     Thread.sleep(sleepTime); 
     frame.setVisible(true); 
     Thread.sleep(sleepTime); 
     frame.setVisible(false); 
     Thread.sleep(sleepTime); 
     frame.setVisible(true); 
     Thread.sleep(sleepTime); 
     frame.setVisible(false); 
     Thread.sleep(sleepTime); 
     frame.setVisible(true); 
} 

이 코드는 무서운 것을 알고 ...하지만 그것은 확실히 작동합니다. (루프를 구현해야합니다 ...)

답변

5

이 작업을 수행하는 일반적인 두 가지 방법이 있습니다 : 작업 표시 줄의 창에서 긴급 힌트를 설정하고 알림 아이콘/메시지를 작성할 수 JNI를 사용. 나는 두 번째 방법을 선호하는데, 크로스 플랫폼이고 덜 짜증나기 때문입니다.

documentation on the TrayIcon class, 특히 displayMessage() 방법을 참조하십시오.

다음 링크는 관심이있을 수 있습니다

1

음, 몇 가지 사소한 개선이있었습니다. ;)

타이머를 사용하여 호출자가 메서드를 반환 할 때까지 기다리지 않아도됩니다. 또한 주어진 창에서 한 번에 두 개 이상의 깜박이는 작업을 방지하는 것도 좋습니다.

import java.util.Map; 
import java.util.Timer; 
import java.util.TimerTask; 
import java.util.concurrent.ConcurrentHashMap; 
import javax.swing.JFrame; 

public class WindowFlasher { 

    private final Timer timer = new Timer(); 
    private final Map<JFrame, TimerTask> flashing 
           = new ConcurrentHashMap<JFrame, TimerTask>(); 

    public void flashWindow(final JFrame window, 
          final long period, 
          final int blinks) { 
     TimerTask newTask = new TimerTask() { 
      private int remaining = blinks * 2; 

      @Override 
      public void run() { 
       if (remaining-- > 0) 
        window.setVisible(!window.isVisible()); 
       else { 
        window.setVisible(true); 
        cancel(); 
       } 
      } 

      @Override 
      public boolean cancel() { 
       flashing.remove(this); 
       return super.cancel(); 
      } 
     }; 
     TimerTask oldTask = flashing.put(window, newTask); 

     // if the window is already flashing, cancel the old task 
     if (oldTask != null) 
      oldTask.cancel(); 
     timer.schedule(newTask, 0, period); 
    } 
} 
관련 문제