0
這是一個全屏五秒鐘然後恢復正常的程序。我想要做的是將背景顏色更改爲黃色。我試過getContentPane()。setBackground(Color.YELLOW),但它仍然不起作用。爲什麼我的JFrame程序不能改變背景顏色?
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.
}
}
你」重新封鎖一個Event Dispatching Thread,防止顏色變化。使用類似於javax.swing的定時器,Timer在後臺等待指定的時間 – MadProgrammer
使用應該只用... super.paint(g)內部的繪製方法。 –