而我的揮杆應用程序正在運行我改變屏幕的大小(例如從1024x768到800x600)。如何讓擺動應用程序知道屏幕尺寸的變化?
是否有任何事件可以收聽我的通知?
或者我可以在每秒鐘檢查屏幕大小,但Toolkit.getScreenSize()會告訴我舊值。
如何在更改後獲得真實屏幕尺寸?
環境:Linux的
(在SuSE ES 11和Ubuntu 9.04進行測試),我感謝你的幫助。
馬頓
而我的揮杆應用程序正在運行我改變屏幕的大小(例如從1024x768到800x600)。如何讓擺動應用程序知道屏幕尺寸的變化?
是否有任何事件可以收聽我的通知?
或者我可以在每秒鐘檢查屏幕大小,但Toolkit.getScreenSize()會告訴我舊值。
如何在更改後獲得真實屏幕尺寸?
環境:Linux的
(在SuSE ES 11和Ubuntu 9.04進行測試),我感謝你的幫助。
馬頓
以下爲我工作,但我在Mac上,所以我不能肯定地說,這將在Linux上運行:
System.out.println(GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getDisplayMode().getWidth() + "x" + GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getDisplayMode().getHeight());
//Ignore this block, it was simply to give me a chance to change my resolution
Scanner readUserInput=new Scanner(System.in);
System.out.println("Waiting on input, go change your resolution");
String myName=readUserInput.nextLine();
System.out.println(GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getDisplayMode().getWidth() + "x" + GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getDisplayMode().getHeight());
輸出(廢話實際輸入),爲如下:
1440x900
Waiting on input, go change your resolution
blah
1280x960
顯然,如果你有多個屏幕設備,你將不得不謹慎行事。
爲了計算顯示大小,你可以使用(從GraphicsConfiguration的javadoc拍攝)
Rectangle virtualBounds = new Rectangle();
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++) {
virtualBounds =
virtualBounds.union(gc[i].getBounds());
}
}
然後,您可以查詢width
和virtualBounds
height
。
這也適用於多顯示器設置。
據我所知,沒有JDK支持的方法來檢測更改,所以輪詢是唯一的選擇,或者使用JNA和本機代碼。如果您的頂層窗口最大化,那麼調整顯示大小也會調整任何最大化的窗口大小,因此您還可以在那裏偵聽更改。有關編寫組件偵聽器的示例,請參見此sun tutorial。
如果您沒有頂級最大化窗口,那麼您也可以通過創建隱藏最大化窗口來實現相同的效果。我無法確定這是否可行,但值得嘗試。
Dimension screenSize = Toolkit.getDefaultToolkit()。getScreenSize();
f.setSize(screenSize.width,screenSize.height);
這並不回答這個問題。 – vektor 2016-02-16 07:23:29
感謝您的快速回答。 不幸的是,上面的代碼片段在Ubuntu 9.04上一直顯示舊的大小(1024x768)。 最好的問候, Marton – 2010-06-08 15:21:33
這很奇怪,因爲實質上它使用與接受的答案相同的API。 – mdma 2010-06-08 15:34:24
是的,奇怪的是,GraphicsConfiguration.getBounds()返回不正確的值,而GraphicsDevice.getDisplayMode()返回好的值。 另一方面,GraphicsDevice.getDisplayMode()似乎是重量級的,所以每隔幾秒鐘執行一次可能不是最好的主意。我可能不得不選擇本地電話。 – 2010-06-09 07:36:10