简体   繁体   中英

Positioning JFrame

I am trying to position my JFrame to be in the center of the screen when it opens. Is there code that I can put here to make my program start in the center of the screen.

import javax.swing.JFrame;

public class StarFuryTest
{
    public static void main(String[] args)
    { 
    StarFury menuFrame = new StarFury(); 
    menuFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    menuFrame.setSize(600,800);//Set the size of the frame
    menuFrame.setVisible(true);
    }
}

I am making a paint program. Also what are the statements that turn off decorations and restricts resizing. What is the statement that enters fullscreen.

First, consider whether you really want to put your window in the center of the screen. No other application does that.

The correct thing to do is use setLocationByPlatform(true) , which will allow the desktop to place your window as it sees fit.

If you feel your application is so much more important than all others that it absolutely has to be placed in the center of the screen, you can use setLocationRelativeTo(null) , which is guaranteed to center the window.

Regardless of whether you call setLocationByPlatform or setLocationRelativeTo, make sure you do it before you make your window visible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM