Michael Shoenberger
Michael Shoenberger

Reputation: 25

Java Screen Resolution Size

I need some direction on this project that I am creating. I am currently trying to create a JFrame that automatically opens to the resolution of the screen. I have looked online for help with this aspect of the project and I have found multiple ways to do it. After doing even more research on the methods through the API and reference code of how to use the methods I have put together some code. When I run my program I get a window that pops up like this... (not full screen...)

Program Results

I would appreciate some pointers on where I messed up, I would prefer help in the way of tips or tricks but I will accept code as well (sorry just trying to learn and not copy code). Thank you for your time.

import java.awt.Toolkit;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.Frame;

public class Window {

private static int ScreenWidth;
private static int ScreenHeight;

public int getWidth(){
    return  ScreenHeight;
}

public int getLength(){
    return ScreenHeight;
}

public void WindowSetup(){
    Dimension UserScreen = Toolkit.getDefaultToolkit().getScreenSize();
    int ScreenWidth = (int) UserScreen.getWidth();
    int ScreenHeight = (int) UserScreen.getHeight();
}

static void CreateJframe(){
    JFrame gui  = new JFrame("Changeable Resolution");
    gui.setDefaultLookAndFeelDecorated(true);
    gui.setDefaultCloseOperation(gui.EXIT_ON_CLOSE);
    gui.setPreferredSize(new Dimension(ScreenWidth, ScreenHeight));
    gui.pack();
    gui.setVisible(true);

}


public static void main(String[] args) {
    Window window = new Window();
    Window.CreateJframe();      
}

}

Upvotes: 0

Views: 3235

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347334

  1. You never call WindowSetup
  2. You assign the results of getScreenSize to local variables
  3. Then you call pack...pack uses the preferred size of the content, not the window, to make decisions about who best to size the window.

So that ain't going to work...

The simplest way would be to use JFrame#setExtendedState and pass it JFrame.MAXIMIZED_BOTH

JFrame gui = new JFrame("Changeable Resolution");
gui.setExtendedState(JFrame.MAXIMIZED_BOTH);
gui.setDefaultCloseOperation(gui.EXIT_ON_CLOSE);
gui.pack();
gui.setVisible(true);

Now, you might be saying, but I want to cover the taskbar...

In which case you could try something more like...

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();

GraphicsConfiguration gc = gd.getDefaultConfiguration();
Rectangle bounds = gc.getBounds();

JFrame gui = new JFrame("Changeable Resolution");
gui.setSize(bounds.width, bounds.height);
gui.setLocation(bounds.x, bounds.y);
gui.setDefaultCloseOperation(gui.EXIT_ON_CLOSE);
gui.setVisible(true);

But you'll probably find that the task bar doesn't like been covered, in that case, you would need to resort to full screen exclusive mode...

See Full-Screen Exclusive Mode API for more details...

Upvotes: 1

Related Questions