Java - 将 JFrame 设置为全屏时,屏幕变黑

2022-01-24 00:00:00 fullscreen java swing jframe awt

我正在尝试在 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 (对于 Canvaspaint 方法,对于 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

相关文章