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条回答
Juvenile、少年°
2楼-- · 2019-04-19 14:53

I took a look at the code that you attached for FormTTS.java, what I found out is that your screen was set as using the absolute layout hardcoded to some numbers of pixels. Look at the following code:

Menu.getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

Menu.getContentPane().add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(420, 230, 530, 320));

Your JFrame is not using the GridBagLayout, instead it's using AbsoluteLayout from Netbeans library. So I guess you generated these UI codes with the tools from Netbeans.

And then regarding your picture that does not fill all the screen when maximized: jLabel9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/freetts/equations.png")));

Menu.getContentPane().add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 1530, 990));

Same problem here, it's hardcoded to some numbers of pixels.

If you want everything to be centered when you maximized your screen, I think the only way to do is to use the gridbag layout for your JFrame and this requires you to update almost everything in your code. And you will need to fully understand how GridBagLayout works. Here is the place to start.

However if you only want the background image to fill the screen you can follow the steps here to let the picture scaled to fill the size of JLabel: Resize a picture to fit a JLabel

If it still doesn't work, you should also get the size of the screen (from one of the answers here) and then set the prefferedSize of the JLabel with those values in addition of scaling the image.

查看更多
Lonely孤独者°
3楼-- · 2019-04-19 14:55

Usually, as far as games go, it's preferable to use full screen mode instead of using a maximized window. You can do this in Java by using:

GraphicsEnvironment gfxEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gfxDev = gfxEnv.getDefaultScreenDevice();

Window window = new GameWindow();
gfxDev.setFullScreenWindow(window);

If you still want to use a regular frame and center the content panel, you need to define some of the GridBagLayout constraints. It's impossible to tell which without out seeing the code for the rest of the components on that screen, but consider the following:

  1. GridBagConstraints.fill
  2. GridBagConstraints.anchor
  3. GridBagConstraints.weightx
  4. GridBagConstraints.weighty

And finally, regarding setting the screen to the largest size, it is already addressed here: Java JFrame Size according to screen resolution

查看更多
Luminary・发光体
4楼-- · 2019-04-19 14:57

try this, hope it works for you as well.

MyFrame mFrame= new MyFrame();
mFrame.setVisible(true);
mFrame.setExtendedState(mFrame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
查看更多
你好瞎i
5楼-- · 2019-04-19 14:57

I am having a similar problem with my application. the nearest I have come is to set all components that reside on top to either component.setOpaque(false), or component.setBackground(new Color(255,255,255,0)). you could also try panel.setVisible(false) for the unused panels. its hard to offer up code with out the entire program but give this a whirl:

Menu.setBackground(new Color(255,255,255,0);
查看更多
我命由我不由天
6楼-- · 2019-04-19 14:58

if you use panel then you can resize according to panel, it shows in full panel size

yourinternalframe.setSize(mainPanel.getSize());
yourinternalframe.show();

this may be not seem as your real need, you may do something according to this

查看更多
登录 后发表回答