首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >奇怪的FullScreen行为

奇怪的FullScreen行为
EN

Stack Overflow用户
提问于 2018-11-03 20:27:29
回答 1查看 105关注 0票数 0

我目前正在尝试使用Java的FullScreen独占模式。然而,我对此所做的JFrame表现出了奇怪的行为--框架要么是白色的,要么是黑色的,顶部没有"x“--例如,从窗口中退出。

我在这里要做的就是强迫用户在启动时进入全屏排他性模式。

以下是代码:

代码语言:javascript
复制
package com.TurboTeam.Display;

import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Window;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Display {

private GraphicsDevice gDevice;
private DisplayMode dMode;
private Window activeWindow;
private JPanel panel;
private JFrame frame;

public Display() {

    GraphicsEnvironment gEnviro = GraphicsEnvironment.getLocalGraphicsEnvironment();
    gDevice = gEnviro.getDefaultScreenDevice();

    dMode = gDevice.getDisplayMode();

    frame = new JFrame();
    panel = new JPanel();

    init();

    }

private void init() {

    if(gDevice.isFullScreenSupported()) {

        frame.add(panel);

        frame.setVisible(true);

            gDevice.setFullScreenWindow(frame);
            gDevice.setDisplayMode(dMode);

    }

}


}

所有这些都在SwingUtilities线程中运行。

任何帮助都是非常感谢的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-03 20:38:16

在右上角没有"x“可以从窗口退出。

这就是全屏模式的工作方式。如果您想要退出按钮,您需要添加自己的。

下面是我很久以前在网上发现的一个古老的例子:

代码语言:javascript
复制
/*FullScreen01.java
This is a skeleton program that implements the
Full-Screen Exclusive Mode

The program places an undecorated non-resizable JFrame
object on the screen in the Full-Screen Exclusive Mode.

A JButton appears in the North location of the JFrame.
Clicking the button causes the program to exit the full-
screen mode, restore the original graphics mode, and
terminate.

The program places red, green, and white JLabels in the
East, West, South, and Center locations solely to
demonstrate that the graphics object is an undecorated
non-resizable JFrame object.

The program displays a list of the available graphics
devices solely for information purposes in the command-
line window.  However, that window is hidden behind the
full-screen version of the JFrame object and isn't visible
until the full-screen mode is terminated.

Even though the program displays a list of all of the
graphics devices, it operates only on the first graphics
device in the list.

Tested using J2SE5.0 under WinXP.  J2SE 1.4 or later is
required for the setFullScreenWindow method of the
GraphicsDevice object.
**********************************************************/

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

public class FullScreen01 extends JFrame
                                 implements ActionListener{

  private GraphicsDevice graphicsDevice;
  private DisplayMode origDisplayMode;
  private JButton exitButton = new JButton(
                                  "Exit Full-Screen Mode");

  public static void main(String[] args){
    //Get and display a list of graphics devices solely for
    // information purposes.
    GraphicsEnvironment graphicsEnvironment =
         GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] devices = 
                    graphicsEnvironment.getScreenDevices();
    for(int cnt = 0;cnt < 1;cnt++){
      System.out.println(devices[cnt]);
    }//end for loop

    //Construct a full-screen object using
    // graphicsDevice 0.
    new FullScreen01(devices[0]);
  }//end main

  //Constructor
  public FullScreen01(GraphicsDevice graphicsDevice){
    //Save a reference to the graphics device as an
    // instance variable so that it can be used later to
    // exit the full-screen mode.
    this.graphicsDevice = graphicsDevice;

    setTitle("This title will be hidden (undecorated)");

    //Get and save a reference to the original display
    // mode as an instance variable so that it can be
    // restored later.
    origDisplayMode = graphicsDevice.getDisplayMode();

    //Register an action listener on the exitButton.
    exitButton.addActionListener(this);

    //Place the exitButton in the JFrame
    getContentPane().add(exitButton, BorderLayout.NORTH);

    //Place four labels in the JFrame solely for the
    // purpose of showing that it is a full-screen
    // undecorated JFrame.
    JLabel eastLabel = new JLabel("     East     ");
    eastLabel.setOpaque(true);
    eastLabel.setBackground(Color.RED);
    getContentPane().add(eastLabel,BorderLayout.EAST);

    JLabel southLabel =
                 new JLabel("South",SwingConstants.CENTER);
    southLabel.setOpaque(true);
    southLabel.setBackground(Color.GREEN);
    getContentPane().add(southLabel,BorderLayout.SOUTH);

    JLabel westLabel = new JLabel("     West     ");
    westLabel.setOpaque(true);
    westLabel.setBackground(Color.RED);
    getContentPane().add(westLabel,BorderLayout.WEST);

    JTextArea textArea = new JTextArea(20, 30);
    textArea.setText("I'm a JTextArea");
    textArea.setLineWrap(true);
    textArea.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(textArea);
    getContentPane().add(scrollPane,BorderLayout.CENTER);

    if (graphicsDevice.isFullScreenSupported()){
      // Enter full-screen mode witn an undecorated,
      // non-resizable JFrame object.
      setUndecorated(true);
      setResizable(false);
      //Make it happen!
      graphicsDevice.setFullScreenWindow(this);
      validate();
    }else{
      System.out.println("Full-screen mode not supported");
    }//end else

  }//end constructor

  //The following method is invoked when the used clicks
  // the exitButton
  public void actionPerformed(ActionEvent evt){
    //Restore the original display mode
    graphicsDevice.setDisplayMode(origDisplayMode);
    //Terminate the program
    System.exit(0);
  }//end actionPerformed
}//end classListing 26
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53135209

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档