swing里默认的菜单字体和按钮字体都是不知名的字体,而且还是粗体,现在我想要宋体14号大小,我有没什么办法可以不用一个个的菜单项、按钮的设置?我在网上找到方法是这样的
Font font = new Font("宋体",Font.PLAIN,15);
UIManager.put("JMenuBar.font", font);
事实证明这个方法是骗人的,根本没有反应
Font font = new Font("宋体",Font.PLAIN,15);
UIManager.put("JMenuBar.font", font);
事实证明这个方法是骗人的,根本没有反应
下面是对Button的字体的测试代码。
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.UIManager;@SuppressWarnings("serial")
public class FontTestFrame extends JFrame {
public FontTestFrame()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(new JButton("测试"));
this.pack();
this.setVisible(true);
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Font font = new Font("宋体",Font.PLAIN,15);
UIManager.put("Button.font", font);
new FontTestFrame();
}}
Font font = new Font("宋体",Font.PLAIN,15);
UIManager.put("Menu.font", font);
运行后还是既不报错也不没变化,就像这条语句不存在似的
【应该没J的吧,UIManager.put("MenuBar.font", font); ,这个方法是可行的,给个例子】【方法: //统一程序字体
private static void InitGlobalFont(Font font) {
FontUIResource fontRes = new FontUIResource(font);
for (Enumeration<Object> keys = UIManager.getDefaults().keys();
keys.hasMoreElements(); ) {
Object key = keys.nextElement();
Object value = UIManager.get(key);
if (value instanceof FontUIResource) {
UIManager.put(key, fontRes);
}
}
】
【调用: InitGlobalFont(new Font("宋体", Font.PLAIN, 12));】
我毕业设计中用过,实验证明是可行的
Menu.acceleratorFont
Menu.acceleratorForeground
Menu.acceleratorSelectionForeground
Menu.arrowIcon
Menu.background
Menu.border
Menu.borderPainted
Menu.checkIcon
Menu.consumesTabs
Menu.disabledForeground
Menu.font
Menu.foreground
Menu.margin
Menu.selectedWindowInputMapBindings
Menu.selectionBackground
Menu.selectionForeground
MenuBar.background
MenuBar.border
MenuBar.font
MenuBar.foreground
MenuBar.windowBindings
MenuBarUI
MenuItem.acceleratorDelimiter
MenuItem.acceleratorFont
MenuItem.acceleratorForeground
MenuItem.acceleratorSelectionForeground
MenuItem.arrowIcon
MenuItem.background
MenuItem.border
MenuItem.borderPainted
MenuItem.checkIcon
MenuItem.disabledForeground
MenuItem.font
MenuItem.foreground
MenuItem.margin
MenuItem.selectionBackground
MenuItem.selectionForeground
MenuItemUI
MenuUI
import java.awt.Font;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.UIManager;@SuppressWarnings("serial")
public class FontTestFrame extends JFrame {
public FontTestFrame()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menubar = new JMenuBar();
JMenu m = new JMenu("文件");
m.add(new JMenuItem("打开"));
menubar.add(m);
this.setJMenuBar(menubar);
this.add(new JButton("测试 "));
this.pack();
this.setVisible(true);
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Font font = new Font("宋体",Font.PLAIN,15);
UIManager.put("Button.font", font);
UIManager.put("Menu.font", font);
UIManager.put("MenuItem.font", font);
new FontTestFrame();
}}