I am new in java swing and I created a frame... i checked the documentation API and it has this "setSize()" method.. so yeah i did set the size of my frame but everytime i run the program, it is not on the size that i have specified

public static void main(String[] args)
	{
		CalculatorBody calculator = new CalculatorBody();
		calculator.setTitle("Calculator Body");
		calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		calculator.setSize(400,417);
		calculator.setLocation(400, 250);
		calculator.pack();
		calculator.setVisible(true);
		
		return;
	}

Recommended Answers

All 5 Replies

Check the docs again, they also say there's no guarantee the size will take effect.

I find that setPreferredSize tends to work more often than setSize although as jwentig has mentioned it is not guaranteed. A subsequent call to pack(), validate() or invalidate() often overrides any previous call to setting the size anyway.

this may be irrelevant bcause i know a frame must appear with definite size but still i want to ask... does java allow the size of the frame to be entered by the user through a keyboard? example i will enter 100 for the width and 200 for the height then a frame will appear following the entered size.. if so how will i do it? when i tried doing it still the size is 0... sorry if this may sound stupid for all you experts out there... but please answer...

Then please ask your question in a separate thread. Yes it is possible and yes I know how to do it, but I just won't answer if you hijack 2 year old threads.

this will fit to screen

frame.setExtendedState(JFrame.MAXIMIZED_BOTH);


this will set the size of the window

frame.setSize(600,400);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.