java - 奇怪的全屏行为

标签 java swing

我目前正在尝试使用 Java 的全屏独占模式。然而,我执行此操作的 JFrame 表现出奇怪的行为 - 例如,框架将是全白或全黑,右上角没有“x”来退出窗口。

我在这里想做的就是强制用户在启动时进入全屏独占模式。

这是代码:

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 线程中运行。

非常感谢任何帮助。

最佳答案

with no "x" at the top-right to exit from the window

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

这是我很久以前在网上找到的一个旧示例:

/*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

关于java - 奇怪的全屏行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53135209/

相关文章:

java - 有没有办法将 JLabel 的 PreferredSize 设置为其组件的大小?

java - 非常基本的 JFrame 未显示

java - 使用 netbeans 中另一个 jFrame 中的按钮处理 jFrame

java 循环在 JFrame 中显示图像

Java 的无符号位移右 (>>>) 是有符号位移

java - setImageBitmap() 比 image.setImageDrawable()、image.setImageResource() 慢

java - 管理多个上传文件到mysql db

java - 将数据解析回 mainPanel (java/swing)

GUI 中的 Java StackOverflowException

Java 类实例