创建全屏模式选择类的问题

时间:2016-03-28 18:42:13

标签: java swing graphics jframe

我正在尝试创建一个类,允许我选择是否需要全屏窗口或我选择大小的普通JFrame窗口。这是代码:

public class Frame extends JFrame {

boolean fse = false;
int fsm = 0;
GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[1];

public Frame(String title, int width, int height) {
    setTitle(title);
    setSize(width, height);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);
}

private void checkFullscreenMode() {
    switch(fsm) {
    case 0:
        setUndecorated(false);
        System.out.println("No Fullscreen");
        break;
    case 1:
        setUndecorated(true);
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        break;
    case 2:
        setUndecorated(true);
        device.setFullScreenWindow(this);
        break;
    }
}

public void setFullscreenMode(int fsnm) {
    fse = true;
    if(fsm <= 2) {
        this.fsm = fsnm;
    }
    else {
        System.err.println("Error: Mode " + fsnm + " is not Supported!");
    }
}

}

当我尝试调用方法setFullscreenMode时,它不起作用。不管我选择什么模式,它只给了我一个1280 x 720帧。以下是我的称呼方式:

public class Main {
    public static void main(String[] args) {
        Frame frame = new Frame("TheDlooter", 1280, 720);
        frame.setFullscreenMode(1);
        frame.setVisible(true);
    }
}

1 个答案:

答案 0 :(得分:1)

我意识到你没有打电话给checkFullscreenMode()。但实际上这种方法使你的画面全屏。因此,请覆盖setVisible中的Frame方法,然后在此处调用checkFullscreenMode()

以下是工作代码:

import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class Main {
    public static void main(String[] args) {
        Frame frame = new Frame("TheDlooter");
        frame.setFullscreenMode(1);
        frame.setVisible(true);
    }
}

class Frame extends JFrame {

    boolean fse = false;
    int fsm = 0;
    GraphicsDevice device =
    GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[1];

    public Frame(String title) {
        setTitle(title);

        add(new JLabel("ads"));

        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
    }

    @Override
    public void setVisible(boolean b) {
        checkFullscreenMode();
        super.setVisible(b);
    }

    public void checkFullscreenMode() {
        switch (fsm) {
        case 0:
            setUndecorated(false);
            System.out.println("No Fullscreen");
            break;
        case 1:
            setUndecorated(true);
            setExtendedState(JFrame.MAXIMIZED_BOTH);
            break;
        case 2:
            setUndecorated(true);
            device.setFullScreenWindow(this);
            break;
        }
    }

    public void setFullscreenMode(int fsnm) {
        fse = true;
        if (fsm <= 2) {
            this.fsm = fsnm;
        } else {
            System.err.println("Error: Mode " + fsnm + " is not Supported!");
        }
    }
}