我有两个类文件:
屏幕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);
}
}