高 DPI 屏幕上的 Java Swing
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26877517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Swing on high-DPI screen
提问by user1828108
I have a Java Swing program that uses the System Look And Feel:
我有一个使用系统外观的 Java Swing 程序:
UIManager.setLookAndFeel (UIManager.getSystemLookAndFeelClassName ());
The problem is that on a high-DPI system, the fonts in the frames are way too small. How can I make the text on my frames readable without having to change the fonts for ALL the frames? My program was written using Java 6 and has too many frames to modify.
问题是在高 DPI 系统上,框架中的字体太小了。如何在不必更改所有框架的字体的情况下使框架上的文本可读?我的程序是使用 Java 6 编写的,需要修改的框架太多。
采纳答案by MadProgrammer
You could physically modify the look and feel's font settings...
您可以物理修改外观的字体设置...
import java.awt.EventQueue;
import java.awt.Font;
import java.util.Set;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
setDefaultSize(24);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JLabel("Happy as a pig in a blanket"));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public static void setDefaultSize(int size) {
Set<Object> keySet = UIManager.getLookAndFeelDefaults().keySet();
Object[] keys = keySet.toArray(new Object[keySet.size()]);
for (Object key : keys) {
if (key != null && key.toString().toLowerCase().contains("font")) {
System.out.println(key);
Font font = UIManager.getDefaults().getFont(key);
if (font != null) {
font = font.deriveFont((float)size);
UIManager.put(key, font);
}
}
}
}
}
We use a similar approach to increase/decrease the font size of the running application to test layouts (and eventually allow the user some additional control over the font size)
我们使用类似的方法来增加/减少正在运行的应用程序的字体大小来测试布局(并最终允许用户对字体大小进行一些额外的控制)
回答by Joshua Goldberg
I can't say for sure if this fully addresses the issue, but apparently high DPI Java is fixed for Mac in Java 7, and fixed for Linux and Windows in Java 9.
我不能肯定这是否完全解决了这个问题,但显然高 DPI Java 在 Java 7 中已针对 Mac 修复,在 Java 9 中针对 Linux 和 Windows 修复。
JEP 263: HiDPI Graphics on Windows and Linux: http://openjdk.java.net/jeps/263
JEP 263:Windows 和 Linux 上的 HiDPI 图形:http://openjdk.java.net/jeps/263
corresponding ticket: https://bugs.openjdk.java.net/browse/JDK-8055212