0x6B6F77616C74
0x6B6F77616C74

Reputation: 2630

Dimensions of a window don't match

class Main 
{     
    public static void main(String [] args)
    {
     Window h = new Window(100,100);            
    }        

}



class Window
{
    private JFrame frame;

    public Window(int width,int height)
    {
        Rectangle dim = new Rectangle();

        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(0, 0);
        frame.setSize(width, height);
        frame.setVisible(true);

        frame.getBounds(dim);

        System.out.print(dim); 
    }
}

This program creates a window of width and height specified in the constructor, then measures and "echoes" it's dimensions.

run: java.awt.Rectangle[x=0,y=0,width=132,height=100]

Could you explain why is a real window wider by 32px?

Upvotes: 1

Views: 84

Answers (1)

Hui Zheng
Hui Zheng

Reputation: 10222

That's because JComponent has default minimum size, and in your case, the minimal width is 132px. To solve the problem, you may either increase window's width to at least 132, or add frame.setMinimumSize(new Dimension(100, 100)) before frame.setSize(width, height).

Upvotes: 2

Related Questions