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



我试图创建一个类,让我可以选择是想要全屏窗口还是普通的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);
    }
}

我意识到您没有调用checkFullscreenMode()。但实际上,这种方法使你的框架全屏。因此,重写Frame中的setVisible方法,并在此处调用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!");
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新