Java图形环境

标签 java swing jframe fullscreen

当我到达“以下内容等效”部分时,我试图展示如何执行 vc.setFullScreenWindow(window)所有这些都在一个命令中,但我自己很困惑,因为我对此很陌生。

这是我的主要问题,但如果有人能够解释有关 GraphicsEnvironment 或 JFrame 的任何值得注意的事情,我将非常感激。

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

// Screens2 can use all methods that JFrame has
public class Screens2 extends JFrame{


// Video Card used for Utilizing the graphics on the computer SCREEN
private GraphicsDevice vc;

// The Constructor merely sets the default Screen graphics up
public Screens2(){

    // The OS specific Graphics Envirionment
    GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();


    // Graphics Device vc = The Default();
    vc = env.getDefaultScreenDevice();

}

// he DisplayMode class encapsulates the bit depth, 
// height, width, and refresh rate of a GraphicsDevice. 
// this method also takes in a JFrame to Display the DisplayMode
public void setFullScree(DisplayMode dm, JFrame window){

    // Nothing fancy
    window.setUndecorated(true);
    window.setResizable(false);

    // The following is equivalent =======================================================
    private GraphicsDevice.GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(window);
    vc.setFullScreenWindow(window);

}
}

最佳答案

你提到的这一行有点困惑:

private GraphicsDevice.GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(window);

编译器假设您要在方法内创建私有(private)成员变量。请参阅Declaring Member Variables 。也不需要启动 GraphicsDevice

将其替换为以下内容:

GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(window);

查看Lesson: Full-Screen Exclusive Mode API有关全屏独占模式的更多详细信息。

考虑以下示例,该示例演示了全屏模式下的框架:

import java.awt.BorderLayout;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;

public class FullScreenDemo {

    private static void createAndShowGUI() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setLayout(new BorderLayout());
        frame.add(new JLabel("Hit Escape to exit full screen", JLabel.CENTER), BorderLayout.CENTER);

        frame.setSize(300, 300);

        KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke("ESCAPE");
        Action escapeAction = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(null);       
            }
        };

        frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeKeyStroke, "ESCAPE");
        frame.getRootPane().getActionMap().put("ESCAPE", escapeAction);     

        GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(frame);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

关于Java图形环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11605894/

相关文章:

java - 尝试正确确定数字是否奇怪

java - 在 Java 中,类型后跟尖括号(如 List<Foo>)是什么意思?

java - 为什么顶层容器中的覆盖油漆如此糟糕?

java - 如何在另一个动态图像下添加背景图像?

java - jFrame 中的多个帧?

java - 我无法将变量传递给 Main 方法(Cucumber)

java - 如何在 mybatis XML 映射器中使用 partials 或 helpers?

java - JGoodies FormLayout 拒绝调整我的 JComboBox 大小

java - 从另一个类更改 Java 卡布局中的 Activity "card"

java - 为什么我必须重新调整 JFrame 的大小才能看到该组件