下面的代码创建了一个简单的GUI,中间有一个按钮,单击该按钮时,应更新JMenuBar
组件的字体。为此,setMyFont
上的ActionListener
会触发JButton
方法。然而,经过几次列出的尝试,我没有完成这个,但我不知道为什么。 setMyFont
中使用的代码位于
public void setMyFont(Font f, Font f2) {
//Attempt 1 in the hope it would autodetect that font
//had changed and just update
menuFont = f;
menuItemFont = f2;
//Attempt 2 in the hope on the repaint it would update
//the font with the new UIManager properties
UIManager.put("Menu.font", menuFont);
UIManager.put("MenuItem.font", menuItemFont);
//Attempt 3 in the hope that going over each component
//individually would update the font
for(Component comp: getComponents()) {
if(comp instanceof JMenu) {
comp.setFont(menuFont);
} else {
comp.setFont(menuItemFont);
}
}
validate();
repaint();
}
是否有理由使用当前代码在组件上更新字体?另外,我怎么能改变我的代码以允许字体在组件上更新,即使它们已经被创建了?
SSCCE的完整代码
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.UIManager;
public class Main extends JFrame {
private static final long serialVersionUID = 3206847208968227199L;
JButton but;
MenuBar mB;
private Main() {
setSize(600, 600);
mB = new MenuBar();
setJMenuBar(new MenuBar());
but = new JButton("Change Font");
but.addActionListener(new CustomActionListener());
add(but);
setVisible(true);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
new Main();
}
private class MenuBar extends JMenuBar {
private static final long serialVersionUID = -2055260049565317972L;
Font menuFont = new Font("Courier", Font.ITALIC + Font.BOLD, 12);
Font menuItemFont = new Font("sans-serif", 0, 12);
JMenu menu, subMenu;
MenuBar() {
UIManager.put("Menu.font", menuFont);
UIManager.put("MenuItem.font", menuItemFont);
menu = new JMenu("Menu");
subMenu = new JMenu("Sub Menu");
subMenu.add(new JMenuItem("Sub Item"));
subMenu.add(new JMenu("Sub Menu"));
menu.add(subMenu);
menu.add(new JMenuItem("Sub Item"));
menu.add(new JMenu("Sub Menu"));
add(menu);
menu = new JMenu("Another Menu");
menu.add(new JMenu("Sub Menu"));
menu.add(new JMenuItem("Sub Item"));
menu.add(new JMenu("Sub Menu"));
add(menu);
}
public void setMyFont(Font f, Font f2) {
//Attempt 1 in the hope it would autodetect that font
//had changed and just update
menuFont = f;
menuItemFont = f2;
//Attempt 2 in the hope on the repaint it would update
//the font with the new UIManager properties
UIManager.put("Menu.font", menuFont);
UIManager.put("MenuItem.font", menuItemFont);
//Attempt 3 in the hope that going over each component
//individually would update the font
for(Component comp: getComponents()) {
if(comp instanceof JMenu) {
comp.setFont(menuFont);
} else {
comp.setFont(menuItemFont);
}
}
validate();
repaint();
}
}
private class CustomActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
mB.setMyFont(new Font("sans-serif", 0, 12), new Font("Courier", Font.ITALIC + Font.BOLD, 12));
}
}
}
答案 0 :(得分:4)
设置字体后,您需要让层次结构调用中的每个组件更新它的UI - SwingUtilities有一个方便的方法
UIManager.put("Menu.font", menuFont);
SwingUtilities.updateComponentTreeUI( Main.this );
使用FontUIResource类,例如
FontUIResource menuFont = new FontUIResource("Courier", Font.ITALIC + Font.BOLD, 12);
以下代码改编自已发布的SSCCE适用于我:
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;
public class Main extends JFrame {
private static final long serialVersionUID = 3206847208968227199L;
JButton but;
MenuBar mB;
private Main() {
setSize(600, 600);
mB = new MenuBar();
setJMenuBar(mB);
but = new JButton("Change Font");
but.addActionListener(new CustomActionListener());
add(but);
setVisible(true);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
new Main();
}
private class MenuBar extends JMenuBar {
private static final long serialVersionUID = -2055260049565317972L;
Font menuFont = new FontUIResource("Courier", Font.ITALIC + Font.BOLD, 12);
Font menuItemFont = new FontUIResource("sans-serif", 0, 12);
JMenu menu, subMenu;
MenuBar() {
UIManager.put("Menu.font", menuFont);
UIManager.put("MenuItem.font", menuItemFont);
menu = new JMenu("Menu");
subMenu = new JMenu("Sub Menu");
subMenu.add(new JMenuItem("Sub Item"));
subMenu.add(new JMenu("Sub Menu"));
menu.add(subMenu);
menu.add(new JMenuItem("Sub Item"));
menu.add(new JMenu("Sub Menu"));
add(menu);
menu = new JMenu("Another Menu");
menu.add(new JMenu("Sub Menu"));
menu.add(new JMenuItem("Sub Item"));
menu.add(new JMenu("Sub Menu"));
add(menu);
}
public void setMyFont(Font f, Font f2) {
menuFont = f;
menuItemFont = f2;
UIManager.put("Menu.font", menuFont);
UIManager.put("MenuItem.font", menuItemFont);
SwingUtilities.updateComponentTreeUI(Main.this);
}
}
private class CustomActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
mB.setMyFont(new FontUIResource("sans-serif", 0, 12), new FontUIResource("Courier", Font.ITALIC + Font.BOLD, 12));
}
}
}
答案 1 :(得分:3)
mB = new MenuBar();
setJMenuBar(new MenuBar());
您的ActionListener不会处理您添加到框架中的菜单栏。
代码应为:
mB = new MenuBar();
//setJMenuBar(new MenuBar());
setJMenuBar(mB);
getComponents()方法不是递归的。所以你的循环只会将JMenu组件添加到JMenuBar,而不是JMenuItems。
同样在创建字体时,为什么不使用更大的字体,以便您可以实际查看是否有变化。
同样在您发布的代码中,在创建组件之前设置UIManager字体属性会使按钮失效。您的菜单已经是所需的字体。
答案 2 :(得分:1)
也许你可以试试这个:
public class MenuFontChange {
public static void changeMenuFont(JMenuBar jBar, Font font) {
Component[] components = jBar.getComponents();
for (Component component : components) {
component.setFont(font);
changeMenuItemFont((JMenu) component, font);
}
}
public static void changeMenuItemFont(JMenu jMenu, Font font) {
int n = jMenu.getItemCount();
for (int i = 0; i < n; i++) {
JMenuItem item = jMenu.getItem(i);
item.setFont(font);
}
}