Position Image in any Screen Resolution

前端 未结 3 499
[愿得一人]
[愿得一人] 2020-12-04 04:21

I have a problem with my program. Every time when I change my screen resolution, my image starts to move out of position. Any suggestions on how to make my image stay in the

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-04 04:33

    create a percentage/ratio of the "normal" screen size to what it currently is and multiply that by the pos. Example if the screen size you are working with was 400x600

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    double width = screenSize.getWidth();
    double height = screenSize.getHeight();
    double widthRat = width/600
    double heightRat = height/400
    

    then

    jbtn2.setLocation(140*widthRat, 380*heightRat);
    

    that way say the screen size was doubled to 1200x800 widthRat would = 2 and double the position. This is kind of a sloppy example but gives the idea

提交回复
热议问题