我正在将我的 Swing 应用程序迁移到 Java 11 以利用 HiDPI 显示支持。我使用的是三星显示器,分辨率设置为 3840x2160,缩放比例为 125%,Windows 10。
尽管 java 9 及更高版本被宣传为可以正确处理 HiDPI 缩放,但在显示简单的 JTable 时,网格线会出现不同的粗细,如下所示:
这是此的代码:
import javax.swing.*;
public class TestTable {
public static void main(String[] args) {
new TestTable();
}
public TestTable() {
JTable table = new JTable(12,6);
JDialog dialog = new JDialog();
JScrollPane sp = new JScrollPane(table);
table.setShowGrid(true);
table.setRowHeight(25);
dialog.setContentPane(sp);
dialog.setSize(300,300);
dialog.setVisible(true);
dialog.setLocationRelativeTo(null);
}
}
但是,在设置 Nimbus L&F 时,问题就消失了:
import javax.swing.*;
public class TestTable {
public static void main(String[] args) {
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) { }
new TestTable();
}
public TestTable() {
JTable table = new JTable(12,6);
JDialog dialog = new JDialog();
JScrollPane sp = new JScrollPane(table);
table.setShowGrid(true);
table.setRowHeight(25);
dialog.setContentPane(sp);
dialog.setSize(300,300);
dialog.setVisible(true);
dialog.setLocationRelativeTo(null);
}
}
如何使用默认的 Windows L&F 实现相同的效果?
(使用 java 9 和 10 观察到相同的行为)