AnorLondo
AnorLondo

Reputation: 37

JFrame appears with different size

enter image description here

enter image description here

I have the following code

package trtyhard;    

import javax.swing.JFrame;

public class TrtyHard {

     public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("TryHard");
                //frame.setSize(700, 500);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setResizable(false);
                frame.setSize(700, 500);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);               
            }
        });          

    }       

}

Sometimes JFrame appears with diffirent size. Sometimes it has 10 additional mm at the bottom, sometimes 5-7 additional mm at the right side. How can i fix it?

Upvotes: 0

Views: 504

Answers (2)

iaresti
iaresti

Reputation: 1

The JFrame class has the setBounds(x, y, width, height) method. You can try it. https://docs.oracle.com/javase/7/docs/api/java/awt/Window.html#setBounds(int,%20int,%20int,%20int)

Upvotes: 0

camickr
camickr

Reputation: 324078

In addition to making sure the GUI is created on the Event Dispatch Thread EDT) try restructuring the code as follow:

JFrame frame = new JFrame("TryHard");
//frame.setSize(700, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setSize(700, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);

The point is to set the frame properties before you do a setSize() or pack() on the frame.

Read the section from the Swing tutorial Initial Thread for more information about the EDT.

Upvotes: 2

Related Questions