JFrame not the size I set it to

87 Views Asked by At

I have followed a handful of tutorials on how to fix this and none of them helped

What I am attempting to do is size the frame to 1280 by 720. But when I export my game to a jar the frame size is smaller then it should be, which leads to a variety of problems. Another confusing thing is that when I give the jar to others it works fine, and there are no problems for them. This is very confusing and super annoying.

My code:

package com.teto.main;
import java.awt.Canvas;
import java.awt.Cursor;
import java.awt.Dimension;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class FullFrame extends Canvas {
    private static final long serialVersionUID = 5486926782194361510L;
    Cursor csr = new Cursor(Cursor.CROSSHAIR_CURSOR);
    public FullFrame(int width, int height, String title, Game game) {
        JFrame frame = new JFrame(title);
        frame.setPreferredSize(new Dimension(width, height));
        frame.setMaximumSize(new Dimension(width, height));
        frame.setMinimumSize(new Dimension(width, height));
        frame.setSize(new Dimension(width, height));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        String imageName = "lgico.png";
        URL imageUrl = frame.getClass().getResource(imageName);

        if (imageUrl == null)
        {
            System.out.println("bruh");
            imageUrl = Thread.currentThread().getContextClassLoader().getResource(imageName);
        }

        ImageIcon icon = new ImageIcon( imageUrl );

        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setIconImage(icon.getImage());
        frame.setCursor(csr);
        frame.add(game);
        game.start();
        frame.pack();
    }
}

People have said that you should override getPreferredSize() but I don't know how I would do that and even if I try it doesn't do anything.

Ps. Yeah, I know my code is messy.

1

There are 1 best solutions below

2
On
frame.add(game);

Components should be added to the frame BEFORE the frame is made visible.

    frame.setPreferredSize(new Dimension(width, height));
    frame.setMaximumSize(new Dimension(width, height));
    frame.setMinimumSize(new Dimension(width, height));
    frame.setSize(new Dimension(width, height));
  1. The above code does nothing. The reason it does nothing is because you use frame.pack() later on in the code which will size the frame based on the preferred size of the components added to the frame. The frame size will therefore be the preferred size of your Game class, plus extra space for the title bar and borders of the frame.
  2. You should not attempt to be controlling the frame size by using those methods. You should NOT specify the size because you have no idea what the resolution of each monitor that plays your game will be.

A better way to set the size is to tell the frame to fill the screen.

So your code should be restructured to look something like:

// load the image
ImageIcon icon = new ImageIcon( imageUrl );

JFrame frame = new JFrame(title);
frame.add(game);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setCursor(csr);
frame.setIconImage(icon.getImage());
frame.setResizable(false);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH); // this maximizes the frame
frame.setVisible(true);
game.start();