我有这段代码,它基本上初始化一个新的 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 上:有时我看到它是全屏的,有时会显示两个栏。我错过了什么?
更新
有截图:
最佳答案
确保使用 invokeLater()
在事件分派(dispatch)线程上构建 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();
}
});
}
}
关于java - 全屏边框问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17340114/