JFrame in full screen Java

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?
329681 次浏览

Add:

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

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

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);


}

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.

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.

Set 2 properties below:

  1. extendedState = 6
  2. resizeable = true

It works for me.

Values highlighted that I mean:

JFrame - Properties

Easiest fix ever:

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

it will helps you use your object instant of the frame

frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

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();

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.

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

This just makes the frame the size of the screen

You can use properties tool.
Set 2 properties for your JFrame:

extendedState 6
resizeable    true

you can simply do like this -

public void FullScreen() {
if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) {
final View v = this.activity.getWindow().getDecorView();
v.setSystemUiVisibility(8);
}
else if (Build.VERSION.SDK_INT >= 19) {
final View decorView = this.activity.getWindow().getDecorView();
final int uiOptions = 4102;
decorView.setSystemUiVisibility(uiOptions);
}
}