Java:改变UI字体(Nimbus)不起作用! [英] Java: Altering UI fonts (Nimbus) doesn't work!
问题描述
我指的是这个Nimbus参考资料。
我试图将全局字体设置为稍大一些:
UIManager.put(defaultFont,new Font(Font.SANS_SERIF,0,16));
...仅适用于菜单,但不适用其他(按钮,标签)。
我试图用
UIManager.put(Button.font,new Font(Font.SANS_SERIF,0,16));
UIManager.put(Label.font,new Font(Font.SANS_SERIF,0,16));
但字体仍然存在。
唯一对我有用的是派生字体:
someButton .setFont(someButton.getFont()的deriveFont(16F)。);
但这不是一个选项,因为每个必须完成
元素。
请注意,为UIManager 派生字体也不起作用:
UIManager.put(Label.font,
UIManager.getFont(Label.font)。deriveFont(16f));
我在Linux和Windows下测试了一切:
我只是无法理解API如何如此混乱。如果一个方法被称为
setFont(..),那么我希望它来设置字体。如果此方法未能在
中设置任何可想象的情况下的字体,则应该弃用。
编辑:
这个问题不仅适用于Nimbus,而且也适用于默认的LAF。
用JDK6和JDK7工作。注意:对于JDK6,更改
javax 复制粘贴并获得乐趣 .swing.plaf.nimbus
到
com。sun。java。swing。plaf。nimbus
。
$ b
代码
import java.awt。*;
import java.lang.reflect。*;
import javax.swing。*;
import javax.swing.plaf.nimbus。*;
public class Main {
public static void main(String [] args)
throws InterruptedException,InvocationTargetException {
SwingUtilities.invokeAndWait (new Runnable(){
@Override
public void run(){
try {
UIManager.setLookAndFeel(new NimbusLookAndFeel(){
@Override
public UIDefaults getDefaults(){
UIDefaults ret = super.getDefaults();
ret.put(defaultFont,
new Font(Font.MONOSPACED ,Font.BOLD,16)); //超过我
返回ret;
}
});
new JFrame(Hello ){
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout(FlowLayout.LEFT));
$ b $ setSize(500,500);
setLocationRelativeTo(null);
add(new JLabel(someLabel 1));
add(new JButton(someButton 1));
add (新的JLabel(someLabel 2 ));
add(new JButton(someButton 2));
setVisible(true);
}
};
} catch(Exception ex){
throw new Error(ex);
}
}
});
}
}
I'm referring to this Nimbus reference.
I tried to set global Font to be slightly larger:
UIManager.put("defaultFont", new Font(Font.SANS_SERIF, 0, 16));
...works only for the menu but nothing else (buttons, labels).
I tried to change labels and buttons fonts with
UIManager.put("Button.font", new Font(Font.SANS_SERIF, 0, 16));
UIManager.put("Label.font", new Font(Font.SANS_SERIF, 0, 16));
but the font remains.
The only thing that worked for me was deriving a font:
someButton.setFont(someButton.getFont().deriveFont(16f));
But this is not an option, since this must be done for each element manually.
Note, that deriving a font for UIManager doesn't work either:
UIManager.put("Label.font",
UIManager.getFont("Label.font").deriveFont(16f));
I tested everything under Linux and Windows: same behavior.
I just can't understand how an API can be so messy. If a method is called setFont(..) then I expect it to set the font. If this method fails to set the font in any thinkable circumstances, then it should be deprecated.
EDIT:
The problem not only applies to Nimbus, but also to the default LAF.
This works with JDK6 and JDK7. Copy+paste and have fun ;)
Note: for JDK6, change
javax.swing.plaf.nimbus
to
com.sun.java.swing.plaf.nimbus
.
Code
import java.awt.*;
import java.lang.reflect.*;
import javax.swing.*;
import javax.swing.plaf.nimbus.*;
public class Main {
public static void main(String[] args)
throws InterruptedException, InvocationTargetException {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(new NimbusLookAndFeel() {
@Override
public UIDefaults getDefaults() {
UIDefaults ret = super.getDefaults();
ret.put("defaultFont",
new Font(Font.MONOSPACED, Font.BOLD, 16)); // supersize me
return ret;
}
});
new JFrame("Hello") {
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout(FlowLayout.LEFT));
setSize(500, 500);
setLocationRelativeTo(null);
add(new JLabel("someLabel 1"));
add(new JButton("someButton 1"));
add(new JLabel("someLabel 2"));
add(new JButton("someButton 2"));
setVisible(true);
}
};
} catch (Exception ex) {
throw new Error(ex);
}
}
});
}
}
这篇关于Java:改变UI字体(Nimbus)不起作用!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!