我想全屏显示一张图片,程序编译成功,且能运行,但有时会抛出异常,代码如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FullScreenTest{
public static void main(String agrs[]){
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd=ge.getDefaultScreenDevice();
FullScreenWindow myWindow=new FullScreenWindow();
if(gd.isFullScreenSupported())
gd.setFullScreenWindow(myWindow);
else
System.out.println("Unsupported");
}
}
class FullScreenWindow extends JWindow {
Image m;
MediaTracker mt;
public FullScreenWindow(){
mt=new MediaTracker(this);
m=Toolkit.getDefaultToolkit().getImage("B8.jpg");//显示图片
mt.addImage(m,0);
try{
mt.waitForID(0);
}catch(InterruptedException e){e.printStackTrace();}
this.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
quit();
}
});
}
public void quit(){
this.dispose();
}
public void paint(Graphics g){
g.drawImage(m,0,0,this);
g.setFont(new Font("Arial",Font.BOLD,30));
g.setColor(Color.RED);
g.drawString("this is fullscreen",100,100);
}
}
但有时会出现如下的异常,请问为什么,如何解决?
java.lang.InternalError: Could not get display mode
at sun.awt.Win32GraphicsDevice.getCurrentDisplayMode(Native Method)
at sun.awt.Win32GraphicsDevice.getDisplayMode(Win32GraphicsDevice.java:329)
at java.awt.GraphicsDevice.setFullScreenWindow(GraphicsDevice.java:195)
at sun.awt.Win32GraphicsDevice.setFullScreenWindow(Win32GraphicsDevice.java:261)
at FullScreenTest.main(FullScreenTest.java:11)
Exception in thread "main"