Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JFrame fixed width

Tags:

java

swing

jframe

Is it possible to set fixed width for JFrame? At least the following code isn't working:

import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;

public final class Main  {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setSize(500, 500);
                f.setMinimumSize(new Dimension(500, 0));
                f.setMaximumSize(new Dimension(500, Integer.MAX_VALUE));
                f.setVisible(true);
            }
        });
    }

}

The minimum setting is working but the maximum is not.

like image 227
perak Avatar asked Oct 20 '12 13:10

perak


2 Answers

Use f.setPreferredSize(new Dimension(500, 500)); in your code. And definitely read this post : Java: Difference between the setPreferredSize() and setSize() methods in components And to prevent your frame growing or shrinking use f.setResizable(false);

like image 156
Juvanis Avatar answered Sep 21 '22 05:09

Juvanis


Set size then write:

JFrame.setResizable(false);
like image 21
Umair Shahid Avatar answered Sep 21 '22 05:09

Umair Shahid