2
我正在Java中製作遊戲,目前設置的背景不起作用,但setforeground正常。這裏是我的主類代碼:Java setBackground無法正常工作
import java.awt.*;
import javax.swing.*;
public class Sam extends JFrame{
public static void main(String[] args) {
DisplayMode dm = new DisplayMode(1920, 1080, 32, 60);
Sam s = new Sam();
s.run(dm);
}
public void run(DisplayMode dm){
this.setBackground(Color.PINK);
this.setForeground(Color.BLACK);
setFont(new Font("Arial", Font.PLAIN, 24));
Screen s = new Screen();
try{
s.setFullScreen(dm, this);
try{
Thread.sleep(5000);
}catch(Exception ex){ex.printStackTrace();}
}finally{
s.restoreScreen();
}
}
public void paint(Graphics g){
g.drawString("THIS IS GUNNA BE AWESOME", 200, 200);
}
}
這裏是我的屏幕類的代碼:
import java.awt.*;
import javax.swing.*;
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){ex.printStackTrace();}
}
}
public Window getFullScreenWindow(){
return vc.getFullScreenWindow();
}
public void restoreScreen(){
Window w = vc.getFullScreenWindow();
if(w != null){
w.dispose();
}
vc.setFullScreenWindow(null);
}
}
的getContent()不工作,的getContentPane()的作品,但它仍然沒有改變顏色的背景 –
你擺脫了'塗料'的方法,你打破油漆鏈合同 – MadProgrammer
好吧,我擺脫了它,但如何添加文本呢? –