How to position the form in the center screen?

后端 未结 9 1862
小鲜肉
小鲜肉 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:30

    public class Example extends JFrame {
    
    public static final int WIDTH = 550;//Any Size
    public static final int HEIGHT = 335;//Any Size
    
    public Example(){
    
          init();
    
    }
    
    private void init() {
    
        try {
            UIManager
                    .setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
    
            SwingUtilities.updateComponentTreeUI(this);
            Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
            setSize(WIDTH, HEIGHT);
    
            setLocation((int) (dimension.getWidth() / 2 - WIDTH / 2),
                    (int) (dimension.getHeight() / 2 - HEIGHT / 2));
    
    
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }
    
    }
    }
    

提交回复
热议问题