How to output java full screen properly

2019-04-19 14:27发布

Now what I am doing in my program is that I am using setundecorated = true and MAXIMIZED_BOTH So it makes it go full screen and the display looks very nice, But the problem is that there are images (border) on the left and the right side of my screen and also a blue background. What happens is that in changing screens and resolutions these get disturbed and are not shown properly. Those grey patches come up again

enter image description here

History:

I have a java program which I wanted to always open in full screen; I was not able to find a way to do it properly so I had adjusted the minimum to (1370, 727) and maximum size. Thus, it started opening properly on my laptop, but when I changed my laptop's display to LCD, it started giving problems:

It opens in a smaller window: Login Window

If I then click on the maximize button, a grey area comes on the side and bottom (I wanted the items on screen to get stretched or center themselves): enter image description here

And here for example, there is a grey patch at the bottom. Instead, I want the background to cover the whole screen. enter image description here

Update 1 If I change to stretchable gridbaglayout, this is the code I used and what happens:

Menu.setExtendedState(MAXIMIZED_BOTH);
GridBagLayout gbl = new GridBagLayout();
Menu.setLayout(gbl);
JButton component = new JButton("1");
gbl.layoutContainer(Menu);
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.NONE;
gbl.setConstraints(component, gbc);
Menu.add(component);
Menu.pack();
Menu.setVisible(true);

enter image description here

Question

  1. How do I set "this" frame to setExtendedState(MAXIMIZED_BOTH) as I have done to others? (if I do this in main function, I get an error; even if I make a function for this and call it in main I get an error)

  2. How do I get everything to stretch/rearrange themselves according to the extra grey space?

Update 2 My files in this project:

enter image description here

Update 3 This is the current file I am working on "FormTTS.java" Search for "MAXIMIZED_BOTH" in there and you will find the code I think you will want to check.

11条回答
Melony?
2楼-- · 2019-04-19 14:41

I am also having same requirement as you have, below code works for me.

Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(0,0,d.width,d.height); // i assume you have extended JFrame
查看更多
劫难
3楼-- · 2019-04-19 14:44

To add on to @eitanfar's answer, the best way of enabling fullscreen in Java is using the FSEM (FullScreen Exclusive Mode) API.


As he stated, this is achieved by setting the windows as fullscreen on the GraphicsDevice you want the window to appear fullscreen on, usually the default one. Even if your device does not support FSEM (id est isFullscreenSupported() returns false), setting the window as fullscreen will still partially work as the API will emulate fullscreen. The only safety check is to verify whether the GraphicsEnvironment is headless (isHeadless()). If it is, then there are no devices to display to.


The advantage FSEM gives you is that all graphics processing is run on the GPU (the GraphicsDevice is the GPU, not the monitor), therefore making it faster on most systems. In your program's options, you can allow the user to choose to enable or not FSEM so that they can run at optimal performance.


However, the system's repaint events are undefined when in FSEM, you're better off using active rendering, therefore you're better off ignoring repaint (setIgnoreRepaint(true)) and then using a custom thread for drawing.

查看更多
4楼-- · 2019-04-19 14:45

I know this is a terrible answer because I don't have time to write any code. Have you tried creating a listener so you can get the proper maximum size once the window is actually created, and then setting the GridBagConstraints weightx and weighty properties accordingly?

查看更多
手持菜刀,她持情操
5楼-- · 2019-04-19 14:46

You can easily call

setExtendedState(MAXIMIZED_BOTH); on jframe or

use bellow code to set screen size to any PC.

//size of the screen

 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

//height of the task bar

Insets scnMax = Toolkit.getDefaultToolkit().getScreenInsets(getGraphicsConfiguration());
    int taskBarSize = scnMax.bottom;

//available size of the screen

 setSize(screenSize.width, screenSize.height - taskBarSize);
    setLocation(screenSize.width - getWidth(), screenSize.height - taskBarSize - getHeight());

if u want you can remove taskbar size to get full screen anyway this is the code and this will help you.

查看更多
爱情/是我丢掉的垃圾
6楼-- · 2019-04-19 14:48

Did you try this code

JFrame frame = new JFrame();
frame.setSize(Toolkit.getDefaultToolkit().getScreenSize());
frame.setVisible(true);

You can get full screen size of any device by "Toolkit.getDefaultToolkit().getScreenSize()" in java. Above code I set frame size to fullscreen.

int height = Toolkit.getDefaultToolkit().getScreenSize().height;
int width = Toolkit.getDefaultToolkit().getScreenSize().width;

You can get hight and width of screen to your code by using above codes. I think this will be a help.

查看更多
The star\"
7楼-- · 2019-04-19 14:49

Try setting image as a background to you JFrame. So it will adjust with frame size

How to set Jframe Background Image in GroupLayout Java

so even in full screen it will be adjusting..

查看更多
登录 后发表回答