2014-02-20 70 views
0

我試圖在Windows中使用5個按鈕(North,East,South,West和Center)移動JFrame此刻所有當前代碼都已就位,在使用時工作;使用setLocation圍繞Windows移動JFrame,Java

public void actionPerformed(ActionEvent e) 
{ 
    if(e.getSource()==northButton) 
    { 
     setLocation(500,500); 
    } 

} //works 

public void actionPerformed(ActionEvent e) 
{ 
    if(e.getSource()== northButton) 
    { 
     setLocation(north); 
    } 

} //doesn't work 

然而,隨着任務的一部分,我需要使用Java工具包來getScreenSize的寬度和高度,並用計算來制定出屏幕的邊界,併發送「北」到setLocation()(如上)。但是,使用這種方法會引發錯誤"No suitable method found"我不確定如何解決此問題。計算代碼目前僅在北方。

int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width; 
int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height; 

int width = this.getWidth(); 
int height = this.getHeight(); 

int north = ((screenWidth - width)/2); 

任何幫助將不勝感激。 謝謝!

回答

4

這裏你傳遞兩個參數,X和Y:

setLocation(500,500); 

這裏你逝去的一個,但它是什麼呢? X或Y?如果X,其中的Y ?:

int north = ((screenWidth - width)/2); 
setLocation(north); 

編譯器告訴你它沒有一個setLocation()方法,它有一個參數。它希望在二維空間中的位置:這是一個X Y.也許你想要的是:

setLocation(north, 0); 
+0

固定,歡呼隊友。應該知道,雖然我自己。 –

2

Toolkit.getDefaultToolkit().getScreenSize()返回默認屏幕的全尺寸,它沒有考慮到像任務考慮的事情酒吧或其他可能佔用桌面空間的元素,這些窗口應該避免放置在下面/上面。

一個更好的解決辦法是使用Toolkit.getDefaultToolkit().getScreenInsets(GraphicsConfiguration)GraphicsConfiguration#getBounds

public static Rectangle getScreenViewableBounds(Window window) { 
    return getScreenViewableBounds((Component) window); 
} 

public static Rectangle getScreenViewableBounds(Component comp) { 
    return getScreenViewableBounds(getGraphicsDevice(comp)); 
} 

public static Rectangle getScreenViewableBounds(GraphicsDevice gd) { 
    Rectangle bounds = new Rectangle(0, 0, 0, 0); 
    if (gd == null) { 
     GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
     gd = ge.getDefaultScreenDevice(); 
    } 

    if (gd != null) { 
     GraphicsConfiguration gc = gd.getDefaultConfiguration(); 
     bounds = gc.getBounds(); 

     Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc); 
     bounds.x += insets.left; 
     bounds.y += insets.top; 
     bounds.width -= (insets.left + insets.right); 
     bounds.height -= (insets.top + insets.bottom); 
    } 

    return bounds; 
} 

/** 
* Attempts to locate the graphics device that the component most likely is 
* on. 
* 
* This calculates the area that the window occupies on each screen deivce and 
* returns the one which it occupies the most. 
* 
* @param comp 
* @return 
*/ 
public static GraphicsDevice getGraphicsDevice(Component comp) { 

    GraphicsDevice device = null; 

    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
    GraphicsDevice lstGDs[] = ge.getScreenDevices(); 

    ArrayList<GraphicsDevice> lstDevices = new ArrayList<GraphicsDevice>(lstGDs.length); 

    if (comp != null && comp.isVisible()) { 

     Rectangle parentBounds = comp.getBounds(); 

     /* 
     * If the component is not a window, we need to find its location on the 
     * screen... 
     */ 
     if (!(comp instanceof Window)) { 

      Point p = new Point(0, 0); 

      SwingUtilities.convertPointToScreen(p, comp); 
      parentBounds.setLocation(p); 

     } 

     for (GraphicsDevice gd : lstGDs) { 

      GraphicsConfiguration gc = gd.getDefaultConfiguration(); 
      Rectangle screenBounds = gc.getBounds(); 

      if (screenBounds.intersects(parentBounds)) { 

       lstDevices.add(gd); 

      } 

     } 

     if (lstDevices.size() == 1) { 

      device = lstDevices.get(0); 

     } else { 

      GraphicsDevice gdMost = null; 
      float maxArea = 0; 

      for (GraphicsDevice gd : lstDevices) { 

       int width = 0; 
       int height = 0; 

       GraphicsConfiguration gc = gd.getDefaultConfiguration(); 
       Rectangle bounds = gc.getBounds(); 

       Rectangle2D intBounds = bounds.createIntersection(parentBounds); 

       float perArea = (float) ((intBounds.getWidth() * intBounds.getHeight())/(parentBounds.width * parentBounds.height)); 

       if (perArea > maxArea) { 

        maxArea = perArea; 
        gdMost = gd; 

       } 

      } 

      if (gdMost != null) { 

       device = gdMost; 

      } 

     } 

    } 

    return device; 

} 

您遇到的主要問題是,有沒有這樣的方法setLocation(int) ...會是什麼值int代表什麼辦法? x或y位置?

您需要將x和y位置都傳遞到setLocation才能使其工作。

Rectangle bounds = getScreenViewableBounds(this); // where this is a reference to your window 
int x = bounds.x + ((bounds.width - getWidth())/2; 
int y = bounds.y; 

setLocation(x, y); 

例如...

+0

我認爲這可能會有所幫助,但getGraphicsDevice(comp)不存在。 –

+0

不,你需要寫一個方法 – MadProgrammer

+0

@StanTowianski這就是你得到的複製和粘貼庫代碼:P - 已更新 – MadProgrammer