2014-01-20 177 views
2

我將以「這更像是一個答案」作爲序言。將JFrame捕捉到屏幕邊緣

正如在,我自己回答。

我搜索的標籤,我搜索谷歌,我搜索使用標籤和明文(在這個網站上)的組合和我仍然找不到這個起源於該死的問題。作爲答案,有些人粘貼源代碼或鏈接源代碼。

這是(其中一個)原始資料,它遍佈整個網絡。

http://bit.ly/1eYKNXS

它並不完美(code.google.com域,我可以用完整的URL根據需要進行編輯)。 0的偏移似乎並不總是奏效(在我的家用機器上進行測試,這裏使用多顯示器設置並且沒有),而偏移量爲5。它沒有多顯示器支持。捕捉行爲有點令人興奮(它一直在觸發)。

答案就是這個問題的答案。隨時鎖定,存檔等只是試圖幫助:)

回答

1

所以,看哪,改進的代碼。希望它可以幫助人們,而且它不是太冗長或痛苦的閱讀。

package widgets; 

import java.awt.Frame; 
import java.awt.GraphicsConfiguration; 
import java.awt.GraphicsDevice; 
import java.awt.GraphicsEnvironment; 
import java.awt.Window; 
import java.awt.event.ComponentAdapter; 
import java.awt.event.ComponentEvent; 

public class WindowSnapper extends ComponentAdapter { 

    private boolean locked = false; 

    // feel free to modify; set based on my own preferences 
    // incorporate as user option? 
    private int sd = 30; 
    private GraphicsDevice[] screenList = GraphicsEnvironment 
      .getLocalGraphicsEnvironment().getScreenDevices(); 

    // clamping at 5 seems correct, 0 clamps at -5 beyond screen boundary 
    public void componentMoved(ComponentEvent evt) { 
     // gets current display device 
     Window myWindow = new Window((Frame) evt.getComponent()); 
     GraphicsConfiguration config = myWindow.getGraphicsConfiguration(); 
     GraphicsDevice myScreen = config.getDevice(); 
     // matches against active display 
     for(GraphicsDevice gd : getScreenList()) { 
      // this will set the display to a new display if the window is moved to a new display 
      if(gd.equals(myScreen)) { 
       myScreen = gd; 
       break; 
      } 
     } 

     // minimising calls to stack 
     int screenWidth = myScreen.getDefaultConfiguration().getBounds().width; 
     int screenHeight = myScreen.getDefaultConfiguration().getBounds().height; 
     int compWidth = evt.getComponent().getWidth(); 
     int compHeight = evt.getComponent().getHeight(); 
     int nx = evt.getComponent().getX(); 
     int ny = evt.getComponent().getY(); 
     // setting offsets in case of different screen 
     int currentX = myScreen.getDefaultConfiguration().getBounds().x; 
     int currentY = myScreen.getDefaultConfiguration().getBounds().y; 

     // see end of method 
     // OR conditions seem to stabilise movement when close to screen edge 
     if(locked 
       || nx == currentX + 5 
       || ny == currentY + 5 
       || nx == currentX + screenWidth - compWidth - 5 
       || ny == currentY + screenHeight - compHeight - 5) 
      return; 

     // left 
     if(nx < (currentX + sd) && nx > (currentX + 5)) { 
      nx = currentX + 5; 
     } 

     // top 
     if(ny < (currentY + sd) && ny > (currentY + 5)) { 
      ny = currentY + 5; 
     } 

     // right 
     if(nx > currentX + screenWidth - compWidth - sd 
       && nx < currentX + screenWidth - compWidth - 5) { 
      nx = currentX + screenWidth - compWidth - 5; 
     } 

     // bottom 
     if(ny > currentY + screenHeight - compHeight - sd 
       && ny < currentY + screenHeight - compHeight - 5) { 
      ny = currentY + screenHeight - compHeight - 5; 
     } 

     // make sure we don't get into a recursive loop when the 
     // set location generates more events 
     locked = true; 
     evt.getComponent().setLocation(nx, ny); 
     locked = false; 
    } 

    public int returnSD() { 
     return sd; 
    } 

    public void setSD(int sd) { 
     this.sd = sd; 
    } 

    public GraphicsDevice[] getScreenList() { 
     return screenList; 
    } 

    public void setScreenList(GraphicsDevice[] screenList) { 
     this.screenList = screenList; 
    } 

}