2010-03-15 157 views
4

我只是想搞個遊戲,但我之前也有過這個問題。當我指定一個特定的窗口大小(例如1024 x 768)時,生成的窗口只比我指定的大一點。很煩人。是否有一個原因?我如何糾正它,使創建的窗口實際上是我想要的大小,而不是稍大一點?到目前爲止,我總是回去手動調整大小几個像素,直到我得到了我想要的結果,但那已經變老了。如果甚至有一個我可以使用的公式,它會告訴我需要從我的變量中增加/減少多少像素,這非常棒!Java中的JFrame窗口大小如何產生指定的窗口大小?

P.S.我不知道我的操作系統是否會成爲這個因素,但我正在使用W7X64。

private int windowWidth = 1024; 
private int windowHeight = 768; 

public SomeWindow() { 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setSize(windowWidth, windowHeight); 
    this.setResizable(false); 
    this.setLocation(0,0); 
    this.setVisible(true); 
} 

回答

5

我想了Windows 創建要確切大小的總框架添加

this.setUndecorated(true); 

我指定

我不不明白你的問題。發佈您的SSCCE,顯示問題。

如果我運行類似的代碼:

frame.setResizable(false); 
frame.setSize(1024, 768); 
frame.setVisible(true); 
System.out.println(frame.getSize()); 

它顯示java.awt.Dimension中的[寬度= 1024,高度= 768],是你沒有想到什麼?

如果還出現了一個公式,我可以 使用會告訴我,我多少像素 需要添加/從我的我的 變量減去這將是極好的!

也許你指的是標題欄和邊框所佔用的空間?

import java.awt.*; 
import javax.swing.*; 

public class FrameInfo 
{ 
    public static void main(String[] args) 
    { 
     GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
     Rectangle bounds = env.getMaximumWindowBounds(); 
     System.out.println("Screen Bounds: " + bounds); 

     GraphicsDevice screen = env.getDefaultScreenDevice(); 
     GraphicsConfiguration config = screen.getDefaultConfiguration(); 
     System.out.println("Screen Size : " + config.getBounds()); 

     JFrame frame = new JFrame("Frame Info"); 
     System.out.println("Frame Insets : " + frame.getInsets()); 

     frame.setSize(200, 200); 
     System.out.println("Frame Insets : " + frame.getInsets()); 
     frame.setVisible(true); 

     System.out.println("Frame Size : " + frame.getSize()); 
     System.out.println("Frame Insets : " + frame.getInsets()); 
     System.out.println("Content Size : " + frame.getContentPane().getSize()); 
    } 
} 
+0

我使用了這段代碼,它告訴我W7創建的幀的確是我指定的尺寸,但它仍比我的1024 x 768顯示器大幾個像素。我有3臺顯示器(1920 x 1080,1024 x 768兩側),我注意到即使指定了setLocation(0,0),在我的主顯示器中創建的窗口對我來說只是幾個像素剩下。討厭,但我想這是W7的工作原理。我看不到任何選擇,除非處理它或以小增量更改數字,直到獲得理想的結果。 camickr爲勝利...(幫助糾正錯誤)。 – ubiquibacon 2010-03-15 21:02:54

+1

也許你會發現frame.setExtendedState(JFrame.MAXIMIZED_BOTH);一個更好的選擇?那麼你不必擔心手動指定尺寸。 – camickr 2010-03-16 01:46:27

+0

這聽起來也是個好主意。無論如何要使用它,同時指定我不希望框架超過一定的尺寸?例如,如果我在設置時使用了這種方法,則生成的窗口會填充我的主顯示器1920 x 1080,我不希望創建的窗口的大小超過1280 x 1024我將通過API讀取看看我能想出什麼。和我一起Camickr,當我學習編碼時,會有更多的問題。如果我也使用錯誤的語言學,請糾正我。謝謝你的幫助。 – ubiquibacon 2010-03-16 02:57:41

0

當你說獲得的窗口大小不是被問到的窗口大小時,你是在談論窗口,它的裝飾?

事實上,窗口大小的定義沒有OS特定的窗口裝飾。

嘗試之前

this.setVisible(true);