2016-04-14 28 views
1

我有一个使用Swing GUI的Java应用程序,当我在MAC pro或surface中执行应用程序时,UI尺寸非常小但字体大小正常,如果更改字体大小可以适合,但字体和UI对象会变得很小,很难阅读。Swing GUI布局在4K面板中很奇怪

我可以让布局看起来像高分辨率面板中的全高清面板吗?

回答

2

您需要计算分辨率因子。此代码块计算所有操作系统的因子

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; 
} 

现在我们有一个分辨率因子值。让我们使用它。你为每个像这样的人设置一个值。

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

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

jTestLabel.setFont(jNewFont); 

或者您可以通过重写lookandFeel的defaultFont值来使用此值。

+0

谢谢,如果我使用这个设置,UI对象的大小将会同时改变或者只是字体大小。 – Ives

+0

由于对象的布局,字体大小导致调整UI对象的大小。但是,如果你的UI对象有特定的配置,你也需要为它们设置这个值。例如jLabel.setBorder(新的EmptyBorder(2,2,2,2);你应该像这样改变,以便能够根据用户的屏幕分辨率改变所有的UI配置jLabel.setBorder(新的EmptyBorder(2 * factor,2 *因子,2 *因子,2 *因子); – ziLk