I have a JButton开发者_运维问答 that has a callback function like so:
myJButtonCallBackFunc (ActEvtListener evt) {
myFrame.pack();
myFrame.setVisible(true);
}
The myFrame pops up on the upper left hand corner of the screen. My question: is there a property that controls where the pop up pops up?
Thanks
Window.setLocationRelativeTo(Component)
You can get the location of the button using the getLocationOnScreen method and then use the [setLocation][2] to set the position of the JFrame
[2]: http://download.oracle.com/javase/1.4.2/docs/api/java/awt/Component.html#setLocation(int, int)
If myFrame extends component there is the [setLocation(int x, int y)][1] method.
There is also the setLocationRelativeTo( Component c ) method.
[1]: http://download-llnw.oracle.com/javase/6/docs/api/java/awt/Component.html#setLocation(int, int)
精彩评论