简体   繁体   中英

JFrame doesn't show up using variable for width and height

import java.awt.Dimension;
import javax.swing.JFrame;

public class Window {

    public Window(int width, int height, String title){
        JFrame frame = new JFrame(title);
        frame.setPreferredSize(new Dimension(width,height));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }
}

public class Driver {
    private final static int WIDTH = 500, HEIGHT = 500;

    public static void main(String[] args) {
        new Window(WIDTH,HEIGHT,"Title");
    }

}

Instead of doing frame.setPreferredSize(new Dimension(width,height)); , try doing this.

frame.setSize(width, height);

frame.setSize(width,height);

OR

if you want to use frame.setPrefferedSize(new Dimension(width,height));

then You Have to set Minimum and Maximum Size

frame.setMinimumSize(new Dimension(width,height));

frame.setMaximumSize(new Dimension(width,height));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM