我试图在画布上绘制一些东西,将其添加到JFrame,然后将此JFrame设置为“全屏”.我的问题是:在全屏模式下,我只会看到黑屏.
在屏幕变黑之前,我很快就能看到画布的粉红色背景.
直接在JFrame上绘制,然后将其设置为全屏,效果很好,我可以看到测试文本.我假设正确显示画布存在问题.
这是我的代码:
public class FullscreenTest extends Canvas {
private JFrame mainFrame;
public FullscreenTest(){
this.mainFrame = new JFrame();
JPanel contentPane = (JPanel) mainFrame.getContentPane();
contentPane.add(this);
}
public void run(DisplayMode dm){
setBackground(Color.PINK);
setForeground(Color.WHITE);
setFont(new Font("Arial", Font.PLAIN, 24));
Screen s = new Screen();
s.setFullScreen(dm, this.mainFrame);
try {
Thread.sleep(5000);
} catch (InterruptedException exc) { exc.printStackTrace(); }
s.closeFullScreenWindow();
}
public void paint(Graphics g){
g.drawString("This is some testtext", 200, 200);
}
public static void main(String[] args){
DisplayMode dm = new DisplayMode(800, 600, 32, DisplayMode.REFRESH_RATE_UNKNOWN);
FullscreenTest test = new FullscreenTest();
test.run(dm);
}
}
这是Screen.setFullScreen(DisplayMode dm,JFrame window)方法的作用:
//graphicsDevice = GraphicsEnvironment.getLocalGraphicsEnvironment()
// .getDefaultScreenDevice();
public void setFullScreen(DisplayMode dm, JFrame window){
window.setUndecorated(true);
window.setResizable(false);
graphicsDevice.setFullScreenWindow(window);
if(dm != null && graphicsDevice.isDisplayChangeSupported()){
graphicsDevice.setDisplayMode(dm);
}
}
有谁知道为什么我无法全屏显示JFrame的内容?