Results 1 to 4 of 4
- 05-13-2011, 11:33 PM #1
Member
- Join Date
- May 2011
- Posts
- 2
- Rep Power
- 0
Need to Understand code to make window centralized
I am in process of learning swing, below code is used to make a window centralized
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class CenterOnScreen extends JFrame {
public CenterOnScreen() {
setSize(300, 200);
setTitle("CenterOnScreen");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Toolkit toolkit = getToolkit();
Dimension size = toolkit.getScreenSize();
setLocation(size.width/2 - getWidth()/2,
size.height/2 - getHeight()/2);
}
public static void main(String[] args) {
CenterOnScreen cos = new CenterOnScreen();
cos.setVisible(true);
}
}
However please helps with my doubts inline:-
1.) With code-line Toolkit toolkit = getToolkit();
I am under impression that we are making object of toolkit class however why we have not used new .i.e.
Toolkit toolkit = new getToolkit();
2.) Please let me know how does this line works...
setLocation(size.width/2 - getWidth()/2,
size.height/2 - getHeight()/2);
Thanks in advance... :-)
Thanks,
Ruchir
-
What I do to center a JFrame:
- Make sure that I use layout managers to aid in the display of my components, often nesting JPanels (containers) each with its own layout.
- Avoid calling setSize(...) on anything.
- Call pack() on my JFrame after having added components to it.
- Call setLocationRelativeTo(null) on the JFrame after having called pack()
- Then call setVisible(true) on the JFrame.
Last edited by Fubarable; 05-13-2011 at 11:38 PM.
- 05-14-2011, 12:10 AM #3
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
The code you suggested wouldn't work - the 'new' keyword expects a class constructor, e.g. new Toolkit(), not a method. However, this isn't done because the toolkit must vary according to the operating system being used, so the appropriate toolkit must be created. This is done using the method 'getToolkit()' which presumably calls the static method Toolkit.getDefaultToolkit() and returns a Toolkit implementation suitable for that OS. This kind of method is known as a 'factory method' because it 'manufactures' a class implementation depending on some context.
It finds the centre of the screen horizontally and offsets it by half the width of the frame, and finds the centre of the screen vertically and offsets it by half the height of the frame. This gives the coordinates of the top left of the frame when centered on the screen. Displaying the frame at these coordinates will display it centered on the screen.2.) Please let me know how does this line works...
setLocation(size.width/2 - getWidth()/2,
size.height/2 - getHeight()/2);
Fubarable's centering trick is much simpler!Last edited by dlorde; 05-14-2011 at 12:14 AM.
- 05-14-2011, 12:49 PM #4
Member
- Join Date
- May 2011
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Understand Code
By Quizzle23 in forum New To JavaReplies: 9Last Post: 03-07-2011, 10:07 PM -
need to understand code
By Masken2 in forum New To JavaReplies: 2Last Post: 02-17-2011, 04:21 PM -
How do you make another window appear
By robertbob in forum AWT / SwingReplies: 5Last Post: 05-22-2010, 07:05 PM -
understand the code
By prof.deedee in forum New To JavaReplies: 8Last Post: 11-11-2009, 02:43 AM -
Trying to understand this code
By new2java2009 in forum New To JavaReplies: 2Last Post: 09-09-2009, 07:18 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks