制作 Java 面板全屏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2348412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:26:08  来源:igfitidea点击:

Making a Java panel fullscreen

javaswingfullscreen

提问by clamp

How would you make a JComponent(panel, frame, window, etc.) fullscreen, so that it also overlaps everything on the screen including the windows start bar?

您将如何制作JComponent(面板、框架、窗口等)全屏,以便它也覆盖屏幕上的所有内容,包括窗口开始栏?

I don't want to change the resolution or anything with the graphics device like bitdepth etc, I just want to overlap everything else.

我不想更改分辨率或任何图形设备(如位深度等),我只想重叠其他所有内容。

采纳答案by VonC

You can try some of the codes in this page, allowing a container to fill the screen (so it is not a solution for an individualcomponent, but for a set of components within a container like a JFrame)

你可以试试这个页面中的一些代码,允许一个容器填满屏幕(所以它不是针对单个组件的解决方案,而是针对一个容器内的一组组件,比如一个JFrame

public class MainWindow extends JFrame
{
  public MainWindow()
  {
    super("Fullscreen");
    getContentPane().setPreferredSize( Toolkit.getDefaultToolkit().getScreenSize());
    pack();
    setResizable(false);
    show();

    SwingUtilities.invokeLater(new Runnable() {
      public void run()
      {
        Point p = new Point(0, 0);
        SwingUtilities.convertPointToScreen(p, getContentPane());
        Point l = getLocation();
        l.x -= p.x;
        l.y -= p.y;
        setLocation(l);
      }
    });
  }
  ...
}

回答by Sandro

You need to use the following API: http://java.sun.com/docs/books/tutorial/extra/fullscreen/index.html

您需要使用以下 API:http: //java.sun.com/docs/books/tutorial/extra/fullscreen/index.html

Going full screen isn't as simple as making a large panel, you need to look into the underlying OS graphics. But your JPanel code should translate just fine.

全屏并不像制作大面板那么简单,您需要查看底层的操作系统图形。但是您的 JPanel 代码应该可以很好地翻译。

回答by Adamski

Check out this tutorialdescribing Java's Full-Screen mode API.

查看本教程描述 Java 的全屏模式 API。

Example code (taken from the tutorial). Note that the code operates on a Windowso you would need to embed your JPanelwith a Window(e.g. JFrame) in order to do this.

示例代码(取自教程)。请注意,代码在 a 上运行,Window因此您需要JPanel使用 a Window(例如JFrame)嵌入您的代码才能执行此操作。

GraphicsDevice myDevice;
Window myWindow;

try {
    myDevice.setFullScreenWindow(myWindow);
    ...
} finally {
    myDevice.setFullScreenWindow(null);
}

回答by VonC

I needed to search a lot, to do the same. Here is completely a working version of it by steps, so that i can find it later also, and use it.

我需要搜索很多,做同样的事情。这是它的完整工作版本,按步骤进行,以便我以后也可以找到它并使用它。

Step 1: create a file called fullscreen.java

第 1 步:创建一个名为 fullscreen.java 的文件

Step 2: copy this code and paste it as it is:

第 2 步:复制此代码并按原样粘贴:

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

public class fullscreen extends Window 
{
  private Button button;

  public fullscreen() 
  {
    super(new Frame());
    button = new Button("Close");
    button.addActionListener(new ActionListener() 
    {
      public void actionPerformed(ActionEvent e) 
      {
        System.exit(0);
      }
    });

    setLayout(new FlowLayout());
    add(button);

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    setBounds(0,0,screenSize.width, screenSize.height);
  }

  public static void main(String[] args) 
  {
    // This will take over your whole screen tested and works in my:
    // Fedora 12/13/14
    // CentOS 5.0
    // if this works for you, in other platforms, please leave a comments which OS it worked.
    // happy coding!
    new fullscreen().setVisible(true);
  }

}

Step 3: compile the code and run

第三步:编译代码并运行

Done.

完毕。

回答by user3426905

If I were you I would try to make Java not draw the border of the Jframe, then make it take all the screen.

如果我是你,我会尝试让 Java 不绘制 Jframe 的边框,然后让它占据整个屏幕。

import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;

import javax.swing.JFrame;


public class FenNoBorder extends JFrame {

    public FenNoBorder () {
        setUndecorated(true);
        setVisible(true);
        GraphicsEnvironment graphicsEnvironment=GraphicsEnvironment.getLocalGraphicsEnvironment();
        Rectangle maximumWindowBounds=graphicsEnvironment.getMaximumWindowBounds();
        setBounds(maximumWindowBounds);
    }
}