How to position the form in the center screen?

后端 未结 9 1857
小鲜肉
小鲜肉 2021-01-30 05:05

I\'m a .Net developer but somehow I was task to create a simple application in java for some extra reason. I was able to create that application but my problem is how can i cent

9条回答
  •  北恋
    北恋 (楼主)
    2021-01-30 05:40

    i hope this will be helpful.

    put this on the top of source code :

    import java.awt.Toolkit;
    

    and then write this code :

    private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  
        int lebar = this.getWidth()/2;
        int tinggi = this.getHeight()/2;
        int x = (Toolkit.getDefaultToolkit().getScreenSize().width/2)-lebar;
        int y = (Toolkit.getDefaultToolkit().getScreenSize().height/2)-tinggi;
        this.setLocation(x, y);
    }
    

    good luck :)

提交回复
热议问题