![]() |
|
|
|||||||
![]() |
Java - How to place a window in the center of the screen ? |
|
|
Thread Tools | Search this Thread |
|
|
#11 |
|
(Tom Parson) wrote in
news:4379d7b5$0$21942$: > I would like to open a window and to placed it at a certain position > on a screen (e.g. in the center). > > public static void main(String args[]) { > testGBL window = new testGBL(); > window.setTitle("test"); > window.pack(); > window.setVisible(true); } > } > > put this in --- Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); int x = (int) ((d.getWidth() - this.getWidth())/ 2); int y = (int) ((d.getHeight() - this.getHeight())/ 2); this.setLocation(x, y); //this.setAlwaysOnTop(true); // show on top - 5.0 this.setVisible(true); red eff |
|
|
|
|
#12 |
|
Posts: n/a
|
> I would like to open a window and to placed it at a certain position on a screen
If the variables "width" and "height" are the width and height of your JFrame, something like this: int width = 700; int height = 233; Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); int x = (screen.width-width)/2; int y = (screen.height - height)/2; setBounds(x,y,width,height); jonck@vanderkogel.net |
|
|
|
#13 |
|
Posts: n/a
|
I had to do this a few days ago.
This is the code I wrote : public static void center(JFrame frame) { frame.pack(); double height = frame.getHeight(); double width = frame.getWidth(); Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); double x = screen.getWidth() - width; double y = screen.getHeight() - height; x = x / 2.0; y = y / 2.0; if ( x < 0) x = 0; if ( y < 0) y = 0; frame.setBounds((int)x,(int)y,(int)width,(int)heig ht); } Hope this will help Olivier "Tom Parson" <> a écrit dans le message de news: 4379d7b5$0$21942$... >I would like to open a window and to placed it at a certain position on a >screen > (e.g. in the center). > > How do I do this? My (simplified) current code looks like: > > > > public class testGBL extends JFrame implements ActionListener { > JLabel lab1; > .... > JButton butBack; > > public testGBL() { > > ... > Container contentPane = getContentPane(); > GridBagLayout gridbag = new GridBagLayout(); > GridBagConstraints c = new GridBagConstraints(); > contentPane.setLayout(gridbag); > > c.fill = GridBagConstraints.HORIZONTAL; > > butBack = new JButton("Back"); > c.gridx = 0; > c.gridy = 0; > c.weightx = 1.0; > c.gridwidth = 2; > gridbag.setConstraints(butBack, c); > butBack.setEnabled(false); > butBack.addActionListener(this); > contentPane.add(butBack); > .... > } > > public static void main(String args[]) { > testGBL window = new testGBL(); > window.setTitle("test"); > window.pack(); > window.setVisible(true); } > } > tele2 |
|
|
|
#14 |
|
Posts: n/a
|
Tom Parson wrote:
> I would like to open a window and to placed it at a certain position on a screen > (e.g. in the center). > > How do I do this? My (simplified) current code looks like: > > This puts the window itself in the middle of the screen and not just the top left corner of the window. Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); frame.setSize(dim.width/4, dim.height/4); frame.setLocation(dim.width/2 - dim.width/8, dim.height/2 - dim.height/ > > public class testGBL extends JFrame implements ActionListener { > JLabel lab1; > .... > JButton butBack; > > public testGBL() { > > ... > Container contentPane = getContentPane(); > GridBagLayout gridbag = new GridBagLayout(); > GridBagConstraints c = new GridBagConstraints(); > contentPane.setLayout(gridbag); > > c.fill = GridBagConstraints.HORIZONTAL; > > butBack = new JButton("Back"); > c.gridx = 0; > c.gridy = 0; > c.weightx = 1.0; > c.gridwidth = 2; > gridbag.setConstraints(butBack, c); > butBack.setEnabled(false); > butBack.addActionListener(this); > contentPane.add(butBack); > .... > } > > public static void main(String args[]) { > testGBL window = new testGBL(); > window.setTitle("test"); > window.pack(); > window.setVisible(true); } > } > Brandon McCombs |
|
|
|
#15 |
|
Posts: n/a
|
Here is the code to place ur frame in the center of the screen:
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = frame.getSize(); if (frameSize.height > screenSize.height) { frameSize.height = screenSize.height; } if (frameSize.width > screenSize.width) { frameSize.width = screenSize.width; } int frameX = (screenSize.height - frameSize.height) / 2; int frameY = (screenSize.height - frameSize.height) / 2; frame.setLocation(frameX, frameY); Raja |
|
|
|
#16 |
|
Posts: n/a
|
Raja wrote:
> Here is the code to place ur frame in the center of the screen: > > Dimension screenSize = > Toolkit.getDefaultToolkit().getScreenSize(); > Dimension frameSize = frame.getSize(); > > if (frameSize.height > screenSize.height) { > frameSize.height = screenSize.height; > } > > if (frameSize.width > screenSize.width) { > frameSize.width = screenSize.width; > } > > int frameX = (screenSize.height - frameSize.height) / 2; width???? > int frameY = (screenSize.height - frameSize.height) / 2; > > frame.setLocation(frameX, frameY); > Vova Reznik |
|
|
|
#17 |
|
Posts: n/a
|
Tom Parson wrote:
> I would like to open a window and to placed it at a certain position on a screen > (e.g. in the center). > How do I do this? Specifically to place it in the center (but not some arbitrary other place), Sun shows a pretty way to do this in: http://java.sun.com/docs/books/tutor...rameDemo2.java The line in question says (for a JFrame): frame.setLocationRelativeTo(null); //center it and is line 263 in that example. That actually works, though why it does is probably some Java internals defaults mystery. Otherwise, the usual trick is to use setLocation, which uses the frame top left corner coordinates (in some form) as its input. HTH xanthian. Kent Paul Dolan |
|
![]() |
| Thread Tools | Search this Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| new window minimizes to taskbar | -D- | A+ Certification | 0 | 03-03-2007 01:07 AM |
| New releases: World Trade Center, Jackass Number Two & Wicker Man: Updated complete downloadable R1 DVD DB & info lists | Doug MacLean | DVD Video | 0 | 10-24-2006 06:04 AM |
| Hook Up DVD Player to a Flat Screen PC Monitor? | weft2 | DVD Video | 13 | 02-06-2006 06:14 PM |
| How can I replace a DVD's wide screen movie with a full screen movie from another DVD? | blackhole@aol.com | DVD Video | 15 | 10-19-2005 03:53 PM |
| Panasonic Widscreen 86cm TV Info Needed Screen Size TX-86PW300A. | The Other Guy. | DVD Video | 0 | 01-10-2005 01:32 PM |