21

在我的 Java 应用程序中,我尝试使用以下代码使 JFrame 真正全屏显示:

public class MainFrame extends JFrame {

    private static final long serialVersionUID = 1L;

    public MainFrame() {
        super();
        this.setTitle();
        this.setUndecorated(true);

        this.setExtendedState(JFrame.MAXIMIZED_BOTH);

        this.setVisible(true);
        //this.pack();
    }
}

但在我的 Mac 上,我仍然可以看到 Dock 和 OSX 的顶部工具栏。那么如何创建一个真正占用整个屏幕的 JFrame?

编辑 我必须补充一点,我想从 Eclipse 插件中调用该 JFrame。

4

5 回答 5

12

我还没有尝试过,但是 Java 有全屏 API,应该可以满足您的需求:

http://docs.oracle.com/javase/tutorial/extra/fullscreen/index.html

于 2012-04-07T14:53:16.977 回答
5

我知道答案。首先,我必须承认,如果您正在制作视频或电影播放器​​或动画播放器,以下技巧将不起作用。好的,这是我经过多次尝试后发现的:

假设您想在按下按钮(称为 fullscreenButton)时使 JFrame(称为框架)全屏。然后执行以下操作:

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

public class FullscreenJFrame extends JFrame{

    private JPanel contentPane = new JPanel();
    private JButton fullscreenButton = new JButton("Fullscreen Mode");
    private boolean Am_I_In_FullScreen = false;
    private int PrevX,PrevY,PrevWidth,PrevHeight;

    public static void main(String[] args) {
         FullscreenJFrame frame = new FullscreenJFrame();
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setSize(600,500);
         frame.setVisible(true);
    }

    public FullscreenJFrame(){
        super("My FullscreenJFrame");

        setContentPane(contentPane);
        //From Here starts the trick

        FullScreenEffect effect = new FullScreenEffect();

        fullscreenButton.addActionListener(effect);

        contentPane.add(fullscreenButton);
        fullscreenButton.setVisible(true);

    }

    private class FullScreenEffect implements ActionListener{
        @Override
    public void actionPerformed(ActionEvent arg0) {
         // TODO Auto-generated method stub

             if(Am_I_In_FullScreen == false){

                      PrevX = getX();
          PrevY = getY();
          PrevWidth = getWidth();
          PrevHeight = getHeight();

          dispose(); //Destroys the whole JFrame but keeps organized every Component                               
                      //Needed if you want to use Undecorated JFrame
                      //dispose() is the reason that this trick doesn't work with videos
                      setUndecorated(true);

              setBounds(0,0,getToolkit().getScreenSize().width,getToolkit().getScreenSize().height);
            setVisible(true);
                            Am_I_In_FullScreen = true;
              }
               else{
                    setVisible(true);

                    setBounds(PrevX, PrevY, PrevWidth, PrevHeight);
                    dispose();
        setUndecorated(false);
        setVisible(true);
                    Am_I_In_FullScreen = false;
               }
    }
    }
}

我希望你喜欢它

于 2012-11-07T22:55:29.927 回答
0

一个例子:

import java.awt.FlowLayout;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;




public class FullScreenJFrame extends JFrame{

    private GraphicsDevice vc;

    public FullScreenJFrame(){
     super();

     GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
     vc= e.getDefaultScreenDevice();



     JButton b = new JButton("exit");
     b.addActionListener(
             new ActionListener(){
                 public void actionPerformed(ActionEvent arg0) { 
                     dispose();
                     System.exit(0);

                 }
             }
             );
     this.setLayout(new FlowLayout());
     this.add(b);
     setFullScreen(this);
 }

 public void setFullScreen(JFrame f){

     f.setUndecorated(true);
     f.setResizable(false);
     vc.setFullScreenWindow(f);


 }

 public static void main(String[] args){
     new FullScreenJFrame();
 }

}
于 2015-05-28T14:06:13.577 回答
0
private Dimension screenSize; /* class level vars */
private swidth , sheight;

/*In GUI creating method:put this code: */
screenSize = Toolkit.getDefaultToolkit().getScreenSize();
sheight = screenSize.height;
swidth = screenSize.width;
setBounds(0, 0, swidth, sheight-40);

注意:使用 swidth、sheight 变量可以让您自由进一步调整。
最好的方法是使用intvars 代替- 40egsheight/swidth - margin等。
这里的边距应该来自参数表。全面掌控局面。
也可以直接使用:setBounds(0,0,screenSize.width, screenSize.height);

于 2015-11-15T18:04:58.543 回答
0

使用 com.apple.eawt.FullScreenUtilities。并确保测试系统正在运行 Mac OS。

public void enableFullscreen(Window window, boolean bool) {
    if (System.getProperty("os.name").startsWith("Mac OS")) {
        com.apple.eawt.FullScreenUtilities.setWindowCanFullScreen(window, bool);

    }

}
于 2016-07-13T07:07:00.963 回答