上次讲到在Mac 操作系统中显示 字体有一些问题, 这次 我们试试用几种办法解决这个问题。
1. 在 JavaFX脚本中 显示字体
不用缺省值,而显式地指定字体.
把原来的代码段:
===================================================
SwingLabel {height: 30 width: 250 text: v1},
SwingLabel {height: 30 width: 250 text: v2},
SwingLabel {height: 30 width: 250 text: v3},
SwingButton {height: 30 width: 250 text: v4},
SwingButton {height: 30 width: 250 text: v5}
===================================================
改成
===================================================
SwingLabel {height: 30 width: 250 text: v1 font: Font{name: "MS Gothic", size: 20}},
SwingLabel {height: 30 width: 250 text: v2 font: Font{name: "MS Gothic", size: 20}},
SwingLabel {height: 30 width: 250 text: v3 font: Font{name: "MS Gothic", size: 20}},
SwingButton {height: 30 width: 250 text: v4 font: Font{name: "MS Gothic", size: 20}},
SwingButton {height: 30 width: 250 text: v5 font: Font{name: "MS Gothic", size: 20}}
===================================================字体的大小变大了,结果是:
但结果是 这种方法 会让javafx脚本 不具备通用性了- 不同的语言需要不同的字体类型。
这里有更好的办法解决这个问题 -感谢 Naoto-san的帮助!
2. 在JavaFX脚本中 设置系统缺省值 Look and Feel
这里我在脚本中指定 Look and Feel (aka LAF)到系统的缺省值。
LAF缺省值是javax.swing.plaf.metal.MetalLookAndFeel. 但是在MacOS中我们最好指定它自己的LAF: apple.laf.AquaLookAndFeel.
在任何情况下,下面的代码是一个比较巧妙的方法。
===================================================
var dLAF = UIManager.getSystemLookAndFeelClassName();
UIManager.setLookAndFeel(dLAF);
===================================================
别忘了用 import加入 UIManager class, 并把刚才设置的字体变回缺省值。
===================================================
import javax.swing.UIManager;
===================================================
下面是执行结果:
看着舒服多了吧!
附 - 这个问题已经被报告: Swing based FX apps look worse comparing to the Swing native apps on MacOSX.
如果你觉得需要解决这个bug,请对它投票吧!