这是一个全屏显示五秒钟然后恢复正常的程序。我想要做的是将背景颜色更改为黄色。我试过 getContentPane().setBackground(Color.YELLOW) 但它仍然不起作用。
package gamedev;
import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.Font;
import java.awt.Graphics;
import java.io.PrintWriter;
import javax.swing.JFrame;
public class GameDev extends JFrame {
public static void main(String[] arg) {
DisplayMode dm = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
GameDev b = new GameDev();
b.getContentPane().setBackground(Color.yellow);
b.run(dm);
}
public void run(DisplayMode dm) {
setBackground(Color.YELLOW);
setForeground(Color.red);
setFont(new Font("Arial", Font.PLAIN, 25));
Screen s = new Screen();
try {
s.setFullScreen(dm, this);
try {
Thread.sleep(5000);
} catch (Exception ex) {
}
} finally {
s.restoreScreen();
}
}
public void paint(Graphics g) {
g.drawString("You know you love me home boy.", 200, 200);
}
}
和二等
package gamedev;
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Window;
import javax.swing.JFrame;
public class Screen {
GraphicsDevice videoCard;
public Screen() {
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
videoCard = env.getDefaultScreenDevice(); // acces computer screen
}
public void setFullScreen(DisplayMode dm, JFrame window) {
window.setUndecorated(true);
window.setResizable(false);
videoCard.setFullScreenWindow(window);
if (dm != null && videoCard.isDisplayChangeSupported()) {
try {
videoCard.setDisplayMode(dm);
} catch (Exception ex) {
}
}
}
public Window getFullScreenWindow() {
return videoCard.getFullScreenWindow(); //returns ur window
}
public void restoreScreen() {
Window w = videoCard.getFullScreenWindow();
if (w != null) {
w.dispose();
}
videoCard.setFullScreenWindow(null); // takes away from full screen.
}
}