1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| import java.awt.Component;
import java.awt.Font;
import java.awt.Toolkit;
import java.util.HashMap;
import java.util.Map;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Demo {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Box panel = new Box(BoxLayout.PAGE_AXIS);
Font javaFont = getResolutionAdaptedFonts().get("win.frame.captionFont");
panel.add(createLabel("java", javaFont));
Font winCaptionFont = getWindowsFonts().get("win.frame.captionFont");
JLabel label = createLabel("windows", winCaptionFont);
panel.add(label);
frame.setTitle(label.getText());
frame.getContentPane().add(panel);
frame.setSize(300,100);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static JLabel createLabel(String type, Font font) {
String name = type + " " + font.getName() + " " + font.getSize();
JLabel label = new JLabel(name);
label.setFont(font);
return label;
}
/**
* Donne les polices a utiliser directement (adaptée à la résolution de l'écran
*/
public static Map<String, Font> getResolutionAdaptedFonts() {
return getWindowsFonts(false);
}
/**
* Donne les polices telles qu'elles sont paramétrées dans Windows
*/
public static Map<String, Font> getWindowsFonts() {
return getWindowsFonts(true);
}
private static Map<String, Font> getWindowsFonts(boolean fixRes) {
final Map<String, Font> map = new HashMap<>();
final Toolkit toolkit = Toolkit.getDefaultToolkit();
final int screenRes = Toolkit.getDefaultToolkit().getScreenResolution();
for (final String propertyName : (String[]) toolkit
.getDesktopProperty("win.propNames")) {
if (propertyName.matches("win\\.[^.]+\\.font")
|| propertyName.matches("win\\.frame\\.[^.]+aptionFont")) {
final Font font = (Font) toolkit
.getDesktopProperty(propertyName);
if (fixRes) {
final float fontSize = (72f / screenRes) * font.getSize();
map.put(propertyName, font.deriveFont(fontSize));
} else {
map.put(propertyName, font);
}
}
}
return map;
}
} |
Partager