有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JFrame(全屏)背景色不变

我正在使用

getContentPane().setBackground(Color.PINK);

将JFrame的背景设置为粉红色。此JFrame正在使用GraphicsDevice进行全屏幕显示。背景的颜色没有改变。有什么帮助吗

全屏代码:

public static void main(String... args) {
     DisplayMode dMode = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
     GameMain game = new GameMain();
     game.run(dMode);

 }

 public void run(DisplayMode dMode) {
     getContentPane().setBackground(Color.PINK);
     setForeground(Color.WHITE);
     setFont(new Font("Arial", Font.PLAIN, 24));

     Screen s = new Screen();
     try {
         s.setFullScreen(dMode, this);
         try {
             Thread.sleep(5000);
         } catch(Exception e) { }
     } finally {
         s.restoreScreen();
     }

}

 public void setFullScreen(DisplayMode dMode, JFrame window) {
    window.setUndecorated(true);
    window.setResizable(false);
    gDevice.setFullScreenWindow(window);

    if(dMode != null && gDevice.isDisplayChangeSupported()) {
        try {
            gDevice.setDisplayMode(dMode);
        } catch(Exception e) { }
    }
}

共 (1) 个答案

  1. # 1 楼答案

    这对我来说很好

    public class TestFullScreen {
    
        public static void main(String[] args) {
    
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
    
                    FullFrame frame = new FullFrame();
                    frame.setUndecorated(true);
                    frame.getContentPane().setBackground(Color.PINK);
    
                    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
                    GraphicsDevice[] gs = ge.getScreenDevices();
    
                    gs[0].setFullScreenWindow(frame);
    
                }
            });
    
        }
    
        public static class FullFrame extends JFrame {
    
            public FullFrame() {
                super();
    
                addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        System.exit(0);
                    }
                });
    
            }
        }
    }
    

    我甚至把setBackground调用移到了setFullScreenWindow调用之后

    确保内容窗格中没有任何可能占用全部空间的内容,并且内容窗格未被更改