使Java面板全屏

时间:2010-02-27 18:12:34

标签: java swing fullscreen

你如何制作一个JComponent(面板,框架,窗口等)全屏,这样它也会重叠屏幕上的所有内容,包括windows start bar?

我不想改变像bitdepth等图形设备的分辨率或任何东西,我只想重叠其他所有内容。

5 个答案:

答案 0 :(得分:18)

查看描述Java全屏模式API的this tutorial

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

GraphicsDevice myDevice;
Window myWindow;

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

答案 1 :(得分:11)

您可以尝试一些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);
      }
    });
  }
  ...
}

答案 2 :(得分:6)

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

全屏显示并不像制作大面板那么简单,您需要查看底层的OS图形。但是你的JPanel代码应该转换得很好。

答案 3 :(得分:1)

我需要经常搜索,做同样的事情。这里完全是一个步骤的工作版本,所以我以后也可以找到它并使用它。

第1步:创建一个名为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步:编译代码并运行

完成。

答案 4 :(得分:0)

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