使Java面板全屏显示

wvmv3b1j  于 2023-01-01  发布在  Java
关注(0)|答案(5)|浏览(153)

如何使JComponent(面板、框架、窗口等)全屏显示,使其覆盖屏幕上的所有内容,包括窗口的开始栏?
我不想改变分辨率或任何图形设备像位深等,我只是想重叠其他一切。

c2e8gylq

c2e8gylq1#

查看描述Java全屏模式API的this tutorial
示例代码(摘自教程)。注意,该代码在Window上运行,因此您需要将JPanel嵌入到Window中(例如JFrame)。

GraphicsDevice myDevice;
Window myWindow;

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

klr1opcd2#

您可以尝试一些codes in this page,允许一个容器填充屏幕(因此它不是针对 * 单个 * 组件的解决方案,而是针对像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);
      }
    });
  }
  ...
}
w8f9ii69

w8f9ii693#

您需要使用以下API:http://java.sun.com/docs/books/tutorial/extra/fullscreen/index.html
全屏并不像做一个大面板那么简单,你需要查看底层的操作系统图形,但是你的JPanel代码应该可以很好地翻译。

wz1wpwve

wz1wpwve4#

我需要搜索很多,做同样的事情。这里是一个完整的工作版本的步骤,所以我可以找到它以后也使用它。
Step 1: create a file called fullscreen.java
步骤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);
  }

}

步骤3:编译代码并运行
好的。

57hvy0tb

57hvy0tb5#

如果我是你,我会尝试让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);
    }
}

相关问题