如何让JFrame真正全屏?

时间:2012-04-07 14:49:12

标签: java swing jframe fullscreen

在我的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。

5 个答案:

答案 0 :(得分:12)

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

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

答案 1 :(得分: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;
               }
    }
    }
}

我希望你喜欢它

答案 2 :(得分: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();
 }

}

答案 3 :(得分:0)

formatter.internationalCurrencySymbol
formatter.currencySymbol
formatter.currencyCode

注意:使用宽度,高度变量让你有进一步调整的自由 最好的方法是使用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); 代替int例如- 40等 此边距应来自参数表。完全控制情况。
也可以直接使用:sheight/swidth - margin

答案 4 :(得分: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);

    }

}