java displaymode异常

时间:2014-11-22 21:15:03

标签: java swing exception awt

我正在编写一个非常简单的全屏GUI,但我一直收到错误。这是代码:

package me.Kenny.GUI_WINDOW_INITILIZATION;

import java.awt.*;
import javax.swing.*;

public class Main extends JFrame {

    public static void main(String[] args) {    
        DisplayMode dm = new DisplayMode(1200,800,32,DisplayMode.REFRESH_RATE_UNKNOWN);
        Main m = new Main();
        m.run(dm);
    }

    public void run(DisplayMode dm1) {
        setBackground(Color.PINK);
        setForeground(Color.WHITE);
        setFont(new Font("Arial", Font.PLAIN, 24));
        Screen s = new Screen();

        try {           
            s.setFullScreen(dm1, this);
            try {
                Thread.sleep(5000);
            } catch(Exception e){}  
        } finally {
            s.restoreScreen();
        }
    }

    public void paint(Graphics g) {
        g.drawString("This is gonna be awesome", 300, 300);
    }
}

这是主要计划。 Screen类在这里:

package me.Kenny.GUI_WINDOW_INITILIZATION;

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);
                System.out.println("is in try");
            } catch(IllegalArgumentException ex) {          
                System.out.println("Unable to set Display mode");           
            }           
        }   
    }

    public Window getFullScreenWindow() {
        return vc.getFullScreenWindow();
    }

    public void restoreScreen() {       
        Window w = vc.getFullScreenWindow();        
        if (w != null) {
            w.dispose();
        }   
        vc.setFullScreenWindow(null); //closes window
    }
}

在setFullScreen的try / catch语句中,每当我尝试将vc设置为dm的参数时,我都会收到IllegalArgument异常。我检查了if语句条件以确保它们是真的,并且我的dm启动似乎具有适当的参数。每次我试试跑 程序,我激活catch语句,打印"无法设置显示模式"在控制台上。我的代码中是否缺少某些内容?

由于

0 个答案:

没有答案