我有一个使用系统外观的
Java Swing程序:
UIManager.setLookAndFeel (UIManager.getSystemLookAndFeelClassName ());
问题是在高DPI系统上,框架中的字体太小了.如何使我的框架上的文本可读,而无需更改所有框架的字体?我的程序是使用Java 6编写的,并且有太多的框架来修改.
解决方法
您可以物理修改外观和感觉的字体设置…
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); } } } } }