Java - Screen turns black, when setting a JFrame to Fullscreen(Java - 将 JFrame 设置为全屏时,屏幕变黑)
问题描述
我正在尝试在 Canvas 上绘制一些东西,将其添加到 JFrame,然后将此 JFrame 设置为全屏.我的问题是:在全屏模式下我只看到黑屏.在屏幕变黑之前,我很快就可以看到画布的粉红色背景.
I'm trying to draw something on a Canvas, add it to a JFrame and then set this JFrame to Fullscreen. My problem is: in fullscreenmode I only see a black screen. Before the screen turns black I shortly can see the pink background of the canvas.
直接在 JFrame 上绘图,然后将其设置为全屏效果非常好,我可以看到测试文本.我认为正确显示 Canvas 存在问题.
Drawing directly on a JFrame and then setting it to fullscreen works perfectly fine and I can see the testtext. I assume there is a problem with displaying the Canvas properly.
这是我的代码:
public class FullscreenTest extends Canvas {
private JFrame mainFrame;
public FullscreenTest(){
this.mainFrame = new JFrame();
JPanel contentPane = (JPanel) mainFrame.getContentPane();
contentPane.add(this);
}
public void run(DisplayMode dm){
setBackground(Color.PINK);
setForeground(Color.WHITE);
setFont(new Font("Arial", Font.PLAIN, 24));
Screen s = new Screen();
s.setFullScreen(dm, this.mainFrame);
try {
Thread.sleep(5000);
} catch (InterruptedException exc) { exc.printStackTrace(); }
s.closeFullScreenWindow();
}
public void paint(Graphics g){
g.drawString("This is some testtext", 200, 200);
}
public static void main(String[] args){
DisplayMode dm = new DisplayMode(800, 600, 32, DisplayMode.REFRESH_RATE_UNKNOWN);
FullscreenTest test = new FullscreenTest();
test.run(dm);
}
}
Screen.setFullScreen(DisplayMode dm, JFrame window) 方法的作用如下:
Here is what the Screen.setFullScreen(DisplayMode dm, JFrame window) method does:
//graphicsDevice = GraphicsEnvironment.getLocalGraphicsEnvironment()
// .getDefaultScreenDevice();
public void setFullScreen(DisplayMode dm, JFrame window){
window.setUndecorated(true);
window.setResizable(false);
graphicsDevice.setFullScreenWindow(window);
if(dm != null && graphicsDevice.isDisplayChangeSupported()){
graphicsDevice.setDisplayMode(dm);
}
}
有人知道为什么我在全屏模式下看不到 JFrame 的内容吗?
Does anyone have a clue why I don't see the JFrame's content in fullscreen?
推荐答案
1) 你有三个一般问题
1) you have three general issues
永远不要使用
Thread.sleep(5000);
阻止 EDT 使用 Swing Timer 代替,演示这里
never block EDT by using
Thread.sleep(5000);
use Swing Timer instead, demonstrations here
(如果没有真正重要的原因)不要混音AWT with Swing 其余的是 这里,并使用 JPanel
而不是 Canvas
(对于 Canvas
有 paint
方法,对于 JPanel
有没有paintComponent
)
(if aren't there really important reasons) don't mixing AWT with Swing the rest is here, and use JPanel
instead of Canvas
(for Canvas
is there paint
method, for JPanel
is there paintComponent
)
你的 public void paint(Graphics g){
是 JFrame
而不是 Canvas
并被 Thread 锁定.sleep(5000);
your public void paint(Graphics g){
is to the JFrame
instead of Canvas
and locked by Thread.sleep(5000);
2) Swing GUI 相关的应该包装成 invokeLater()
意思是
2) Swing GUI rellated should be wrapped into invokeLater()
meaning
public static void main(String[] args){
初始线程中的更多内容
3)在链接代码示例中你可以找到演示如何在Swing中使用后台线程
3) in linked code example you can find out demostrations how to use background thread in the Swing
这篇关于Java - 将 JFrame 设置为全屏时,屏幕变黑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java - 将 JFrame 设置为全屏时,屏幕变黑
基础教程推荐
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01