JFrame in full screen Java

我只是一个虾纸丫 提交于 2019-12-17 02:10:44

问题


I will be doing a project soon and I will have to use full screen mode in it.

It will draw some graphics in the window. It would be convienient if I use JFrame or something similar.

I don't know what the final resolution of the screen will be. Please tell me if the graphics will be automaticly rescaled?

JFrame jf = new JFrame();
jf.setSize(1650,1080);
//make it fullscreen;
//now is everything is going to be rescaled so it looks like the original?

回答1:


Add:

frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 
frame.setUndecorated(true);
frame.setVisible(true);



回答2:


If you want put your frame in full-screen mode (like a movie in full-screen), check these answers.

  • How can I do full screen in Java on OSX
  • Java setFullScreenWindow() keep on top

The classes java.awt.GraphicsEnvironment and java.awt.GraphicsDevice are used for put an app in full-screen mode on the one screen (the dispositive).

e.g.:

static GraphicsDevice device = GraphicsEnvironment
        .getLocalGraphicsEnvironment().getScreenDevices()[0];

public static void main(String[] args) {

    final JFrame frame = new JFrame("Display Mode");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setUndecorated(true);

    JButton btn1 = new JButton("Full-Screen");
    btn1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            device.setFullScreenWindow(frame);
        }
    });
    JButton btn2 = new JButton("Normal");
    btn2.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            device.setFullScreenWindow(null);
        }
    });

    JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
    panel.add(btn1);
    panel.add(btn2);
    frame.add(panel);

    frame.pack();
    frame.setVisible(true);

}



回答3:


Use setExtendedState(int state), where state would be JFrame.MAXIMIZED_BOTH.




回答4:


One way is to use the Extended State. This asks the underlying OS to maximize the JFrame.

setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);

Other approach would be to manually maximize the screen for you requirement.

    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    setBounds(100, 100, (int) dim.getWidth(), (int) dim.getHeight());
    setLocationRelativeTo(null);

But this has pitfalls in Ubuntu OS. The work around I found was this.

    if (SystemHelper.isUnix()) {
        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(p);
            }
        });

    }
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    setBounds(100, 100, (int) dim.getWidth(), (int) dim.getHeight());
    setLocationRelativeTo(null);

In Fedora the above problem is not present. But there are complications involved with Gnome or KDE. So better be careful. Hope this helps.




回答5:


Just use this code :

import java.awt.event.*;
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) {
            if (Am_I_In_FullScreen == false) {
                PrevX = getX();
                PrevY = getY();
                PrevWidth = getWidth();
                PrevHeight = getHeight();

                // 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.
                dispose();
                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;
            }
        }
    }
}

I hope this helps.




回答6:


Easiest fix ever:

for ( Window w : Window.getWindows() ) {
    GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow( w );
}



回答7:


it will helps you use your object instant of the frame

frame.setExtendedState(JFrame.MAXIMIZED_BOTH);




回答8:


First of all, that is the resolution you would want to use, 1650,1080.

Now add:

frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 

If you have issues with the components on the JFrame, then after you have added all the components using the frame.add(component) method, add the following statement.

frame.pack();



回答9:


You only need this:

JFrame frame = new JFrame();
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

When you use the MAXIMIZED_BOTH modifier, it will max all the way across the window (height and width).

There are some that suggested using this:

frame.setUndecorated(true);

I won't recommend it, because your window won't have a header, thus no close/restore/minimize button.




回答10:


JFrame frame = new JFrame();
frame.setPreferredSize(new Dimension(Toolkit.getDefaultToolkit().getScreenSize()));

This just makes the frame the size of the screen




回答11:


Values highlighted that I mean:




回答12:


Set 2 properties below:

  1. extendedState = 6
  2. resizeable = true

It works for me.



来源:https://stackoverflow.com/questions/11570356/jframe-in-full-screen-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!