4

我有两个类文件:

屏幕https://gist.github.com/3020101

JMain https://gist.github.com/3020107

我试图让它全屏显示 5 秒并显示背景(或者,此时,甚至是前景)但是当我运行它时,它会全屏显示 5 秒,是的,但它只是一个空白的浅灰色屏幕。

我究竟做错了什么?最终,我将使用图像作为背景,并且我想确保我不会在某个地方搞砸。

多谢你们!

编辑:当我在 JMain 类的末尾添加它时,字体颜色与前景色相同,但无论我在程序中将其更改为哪种颜色,背景始终为黑色。

public void paint(Graphics g) {
    g.drawString("This is gonna be awesome", 200, 200);
}

来自github的代码

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

public class JMain extends JFrame {

    private JFrame frame = new JFrame();

    public static void main(String[] args) {
        DisplayMode dm = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
        JMain m = new JMain();
        m.run(dm);
    }

    public void run(DisplayMode dm) {
        this.getContentPane().setBackground(Color.RED);
        frame.setForeground(Color.BLACK);
        frame.setFont(new Font("Arial", Font.PLAIN, 24));
        Screen s = new Screen();
        try {
            s.setFullScreen(dm, this);
            try {
                Thread.sleep(5000);
            } catch (Exception ex) {
            }
        } finally {
            s.restoreScreen();
        }
    }
}

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

public class Screen {

    private GraphicsDevice vc;

    public Screen() {
        GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
        vc = env.getDefaultScreenDevice();
    }

    public void setFullScreen(DisplayMode dm, JFrame window) {
        window.setUndecorated(true);
        window.setResizable(false);
        vc.setFullScreenWindow(window);
        if (dm != null && vc.isDisplayChangeSupported()) {
            try {
                vc.setDisplayMode(dm);
            } catch (Exception ex) {
            }
        }
    }

    public Window getFullScreenWindow() {
        return vc.getFullScreenWindow();
    }

    public void restoreScreen() {
        Window w = vc.getFullScreenWindow();
        if (w != null) {
            w.dispose();
        }
        vc.setFullScreenWindow(null);
    }
}
4

2 回答 2

11
  1. Don't extend a JFrame, but instead create a local JFrame variable and use it.

  2. You can't paint the JFrame's background Color, but you can do this for the JFrame's contentPane (usually a JPanel). An example of code that does this follows:

    this.getContentPane().setBackground(Color.RED);

  3. Never use Thread.sleep(int) in code called on the Swing event thread, as this will completely block this thread, preventing it from performing its necessary actions of painting the GUI and interacting with the user and effectively freezing the application for as long as the thread is sleeping.

  4. Use a Swing Timer in place of Thread.sleep(...)

于 2012-06-29T20:45:58.443 回答
6

Replace:

setBackground(Color.RED);

With:

getContentPane().setBackground(Color.RED);

Separately, you should try to put graphics related code in SwingUtilities.invoke... as you can get unexpected issues if the graphics related class are used from the main thread. If you make this change make sure to avoid the sleep within SwingUtilities.invoke... as it will block your painting.

于 2012-06-29T20:43:43.253 回答