我维护一个旧的 Swing 应用程序,该应用程序使用自定义边框实现作为窗口装饰,并具有look and feel
扩展MetalLookAndFeel的实现。look and feel
覆盖并安装自initComponentDefaults(UIDefaults table)
定义边框"RootPane.frameBorder"
等等。
自定义边框本身通过实现将 32 行预定义颜色水平绘制在一起paintBorder(Component c, Graphics g, int x, int y, int width, int height)
。当display scaling
在Settings > Display > Scale and layout
. 当display scaling
设置为 125% 或更高时,将无法正确绘制边框,它在应该一起绘制的线条之间有白线。
Fix scaling for apps
inAdvanced scaling settings
似乎不影响边框绘制。
我用AdoptOpenJDK OpenJDK 11.0.4+11 x64
. 我希望 Swing 也能扩大线条,如字体等。
为什么会这样?如何解决这个问题?
边界实现:
public class CustomWindowBorder implements Border{
public static final int BORDER_THICKNESS = 3;
private static final CustomWindowBorder globalInstance = new CustomWindowBorder();
public static CustomWindowBorder getInstance(){
return CustomWindowBorder.globalInstance;
}
private CustomWindowBorder(){}
@Override
public Insets getBorderInsets(Component c){
return new Insets(1,
CustomWindowBorder.BORDER_THICKNESS,
CustomWindowBorder.BORDER_THICKNESS,
CustomWindowBorder.BORDER_THICKNESS);
}
@Override
public boolean isBorderOpaque(){
return true;
}
@Override
public void paintBorder(Component c,
Graphics g,
int x,
int y,
int width,
int height){
Color[] decorationColors = [ommited]; // 30 colors in total
Color[] borderColors = [ommited]; // 3 colors in total
int yStart = 30;
for(int i = 0; i < decorationColors.length; i++ ){
// top
Color clr = decorationColors[i];
g.setColor(clr);
g.drawLine(0,
i,
width,
i);
}
for(int i = 0; i < borderColors.length; i++ ){
Color clr = borderColors[i];
g.setColor(clr);
// left
g.drawLine(i,
yStart,
i,
height - i - 1);
// right
g.drawLine(width - i - 1,
yStart,
width - i - 1,
height - i - 1);
// below
g.drawLine(i,
height - i - 1,
width - i - 1,
height - i - 1);
}
}
}