How to get the EXACT middle of a screen, even when re-sized

前端 未结 3 1231
情书的邮戳
情书的邮戳 2020-11-22 16:47

Ok, this sort of have 2 parts of the question.

  1. When I make a JFrame, and draw something on it, even if I make the width 400, and make it so tha

3条回答
  •  迷失自我
    2020-11-22 17:06

    This is what I use to centre a JFrame on screen, it simply retrieves the height and width of your monitor, then centres your frame

    public static void moveToCenterScreen(JFrame frame) {
        Toolkit kit = frame.getToolkit();       
        GraphicsDevice[] gs = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
        Insets in = kit.getScreenInsets(gs[0].getDefaultConfiguration());
        Dimension d = kit.getScreenSize();
    
        int max_width = (d.width - in.left - in.right);
        int max_height = (d.height - in.top - in.bottom);   
    
        frame.setLocation((int) (max_width - frame.getWidth()) / 2, (int) (max_height - frame.getHeight() ) / 2);
    }
    

提交回复
热议问题