2013-06-27 50 views
0

我有這樣的代碼,基本上初始化一個新的JFrame,並將其設置全屏全屏幕邊框的問題

public class FullScreenFrameTest extends JFrame { 

    public FullScreenFrameTest() { 
     super(); 
     initFrame(); 
     setVisible(true); 

     //full screen 
     GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
     GraphicsDevice device = env.getDefaultScreenDevice(); 
     device.setFullScreenWindow(this); 
     //end full screen 
    } 

    public void initFrame() { 
     Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setUndecorated(true); 
     setLocation(0, 0); //tried removing this, still doesn't work 
     setSize(screen.width, screen.height); 
    } 

    public static void main(String[] args) { 
     try { 
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
     } catch (Exception e) { 
     } 
     new FullScreenFrameTest(); 
    } 
} 

的問題是,有時它的工作,有時沒有,尤其是與Ubuntu:有時我會看到全屏,有時會顯示兩個條。我錯過了什麼?

UPDATE

有一個截圖:

Screenshot

+1

[''invokeLater()'](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html)? –

+2

1)應該在EDT上創建和更新Java GUI。有關更多詳細信息,請參見[Swing中的併發](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/)。 2)另請參見['Frame.setExtendedState(INT)'](http://docs.oracle.com/javase/7/docs/api/java/awt/Frame.html#setExtendedState%28int%29)。 –

+1

@CatalinaIsland它的工作,謝謝! – BackSlash

回答

1

變化

setLocation(0, 0); //tried removing this, still doesn't work 
setSize(screen.width, screen.height); 

setExtendedState(java.awt.Frame.MAXIMIZED_BOTH); 
setLocationRelativeTo(null); 

並在SwingUtilities.invokeLater內調用FullScreenFrameTest()構造函數。

UPDATE

這可能是由於在Java運行時環境中的錯誤。以下是報道http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7057287
爲了更多地瞭解這個問題,看看HERE的bug。
UPDATE
截至去年tryI會建議你使用JFrame#setAlwaysOnTop(true)

+0

不起作用,仍然顯示兩個條 – BackSlash

+0

你能顯示屏幕截圖嗎? –

+0

當然,請參閱我的更新 – BackSlash

2

確保與invokeLater()打造的事件調度線程上的GUI。

更新:這裏有一個SSCCE,似乎工作始終。

import java.awt.EventQueue; 
import java.awt.GraphicsDevice; 
import java.awt.GraphicsEnvironment; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 

public class FullScreenFrameTest extends JFrame { 

    public FullScreenFrameTest() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setUndecorated(true); 
     add(new JLabel("Test", JLabel.CENTER)); 
     GraphicsEnvironment env = 
      GraphicsEnvironment.getLocalGraphicsEnvironment(); 
     GraphicsDevice device = env.getDefaultScreenDevice(); 
     device.setFullScreenWindow(this); 
     setVisible(true); 
    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new FullScreenFrameTest(); 
      } 
     }); 
    } 
} 
+0

那麼,它的工作是第一次,現在它不工作 – BackSlash

+0

即使'setExtendedState()'? –

+0

是的,也有'setExtendedState(JFrame。MAXIMIZED_BOTH)' – BackSlash