Swing GUI layout is strange in 4K panel

冷暖自知 提交于 2020-01-16 04:15:36

问题


I have a Java application that is using Swing GUI, when I execute application in MAC pro or surface, UI size is been very small but the font size is normal, if I change the font size it can be suit, but the font and UI objects will become very small it is hard to read.

Can I let the layout looks like full HD panel in the high resolution panels?


回答1:


You need to calculate resolution factor. This code blocks calculate the factor for all OS

public static Float getRetinaScaleFactor(){
  Object obj = Toolkit.getDefaultToolkit().getDesktopProperty("apple.awt.contentScaleFactor");
  if(obj != null){
   if(obj instanceof Float)
    return (Float) obj;
  }
  return null;
}


public static boolean hasRetinaDisplay(){
  Float fRetinaFactor = getRetinaScaleFactor();
  if(fRetinaFactor != null){
     if(fRetinaFactor > 0){
        int nScale = fRetinaFactor.intValue();
        return (nScale == 2); // 1 indicates a regular mac display, 2 is for retina
     }
   }
   return false;
}


 private static float getResulationFactor(){
   float fResolutionFactor = ((float) Toolkit.getDefaultToolkit().getScreenResolution() / 96f);
   if(hasRetinaDisplay()){
      fResolutionFactor = fResolutionFactor * getRetinaScaleFactor().floatValue();
   }
 return fResolutionFactor;
}

Now we have a resolution factor value. Lets use it. You set this value one for each one like this.

JLabel jTestLabel = new JLabel("hello world");

Font jNewFont = jTestLabel.getFont().deriveFont(Font.Plain, jTestLabel.getFont().getSize() * getResulationFactor()); 

jTestLabel.setFont(jNewFont);

Or you can use this value just one time by overriding defaultFont value of your lookandFeel.



来源:https://stackoverflow.com/questions/36617892/swing-gui-layout-is-strange-in-4k-panel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!