2009-02-08 23 views
2

當我的程序啓動時,主窗口將它自己放在上次關閉時的位置。我想修改這個行爲,所以如果窗口在屏幕外(或部分屏幕外),它會自動移動到屏幕上。Java:在桌面上放置一個窗口時考慮任務欄/菜單欄/等?

我已經完美地完成了這項工作。下面的代碼:

int x = gameConfig.windowX; 
    int y = gameConfig.windowY; 
    int width = gameConfig.windowWidth; 
    int height = gameConfig.windowHeight; 

    if(x < 0) x = 0; 
    if(y < 0) y = 0; 

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 

    if(x + width > screenSize.width) x = screenSize.width - width; 
    if(y + height > screenSize.height) y = screenSize.height - height; 

    if(width > screenSize.width) width = screenSize.width; 
    if(height > screenSize.height) height = screenSize.height; 

    this.setLocation(x, y); 
    this.setSize(width, height); 

    if(gameConfig.screenMaximized) { 
     this.setExtendedState(getExtendedState() | MAXIMIZED_BOTH); 
    } 



可正常工作,但是有一個很大的例外;它不佔任務欄。在窗口上,如果窗口超過了屏幕底部,此代碼將糾正它,但它仍然留下任務欄阻塞的一部分窗口。

我不知道如何做到這一點。有人問java關於系統中的任何任務欄,以及它們的寬度/高度是多少?


非常感謝您的工作。

你知道如何獲得它,所以當我調用getScreenSize()時,Java將反映我的兩個監視器的總屏幕大小?現在,它返回1600x1200,當它真的是3200x1200,橫跨兩臺顯示器。

Java API建議GraphicsConfiguration.getBounds()可以做到這一點,但仍會返回矩形{0,0,1600,1200}。

回答

4

使用getScreenInsets(Java的4+):

static public Insets getScreenInsets(Window wnd) { 
    Insets        si; 

    try { 
     if(wnd==null) { si=Toolkit.getDefaultToolkit().getScreenInsets(new Frame().getGraphicsConfiguration()); } 
     else   { si=wnd.getToolkit()   .getScreenInsets(wnd.getGraphicsConfiguration());   } 
     } catch(NoSuchMethodError thr) { si=new Insets(0,0,0,0); } 
    return si; 
    } 

(這種方法允許多個屏幕,和老版本的JVM的不支持API)。

而且,永遠記住任務欄可能在屏幕的任何邊緣,而不僅僅是底部。

+0

「任務欄可能在屏幕的任何邊緣」 - 這是我正在討論的任務欄(在屏幕的右側)。太多的應用程序*強迫我將它移動到左邊(arghh!),並且仍有一些不能正常工作。 – 2009-02-08 09:22:08

+0

是的 - 雖然Sun並沒有幫助我們,但是一直等到Java 4才能夠確定哪些地方出現了問題。 – 2009-02-08 21:47:54

0

關於多屏幕的問題,我沒有真正做到,但我相信每個屏幕都有它自己的圖形配置。據我所知,你可以枚舉配置以找到你想要的配置 - 然後你使用和我們已經討論過的相同的API。

看起來像GraphicConfiguration的DOCO有細節,你需要:

GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment(); 
GraphicsDevice[] gs=ge.getScreenDevices(); 
for(int j = 0; j<gs.length; j++) { 
    GraphicsDevice gd=gs[j]; 
    GraphicsConfiguration[] gc=gd.getConfigurations(); 
    for (int i=0; i<gc.length; i++) { 
     ... 
     } 
    } 
2

下面的代碼爲我工作,甚至通過工具欄移動到正確的測試。但沒有嘗試多個屏幕。

public class Scene extends JFrame { 
private static final long serialVersionUID = 42L; 

public Scene() { 
    Canvas canvas = new Canvas(); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setContentPane(canvas); 
    this.pack(); 
    this.putFrameInRightCorner(); 
    this.setVisible(true); 
} 

protected void putFrameInRightCorner(){ 
    Rectangle winSize = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds(); 
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
    Dimension frameSize = this.getSize(); 
    int taskBarWidth = screenSize.width - winSize.width; 
    int taskBarHeight = screenSize.height - winSize.height; 
    this.setLocation((screenSize.width - frameSize.width - taskBarWidth), 
         (screenSize.height - frameSize.height - taskBarHeight)); 
} 

public static void main(String[] args) { 
    SwingUtilities.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
      new Scene(); 
     } 
    }); 
} 
} 
1

通過以下帕萬·庫馬爾Kundagol Sundara's後,回答1月9日14:03時,我得到了這個漂亮和完美的工作代碼:

import java.awt.GraphicsEnvironment; 
import java.awt.Rectangle; 

Rectangle janelSOdisponivel = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds(); 
int janelaSOaltura = janelSOdisponivel.height; 
int janelaSOlargura = janelSOdisponivel.width; 

,然後用兩個變量(janelaSOalturajanelaSOlargura)但是我需要。

相關問題