Java Swing-在运行时更改菜单栏和菜单项的字体大小
正如标题所说,我需要使用SWING更改菜单栏的字体大小和菜单栏上的每个项目(以及站点的项目) 我有以下代码,但不能在运行时运行,我需要它是当点击菜单项Java Swing-在运行时更改菜单栏和菜单项的字体大小,java,swing,Java,Swing,正如标题所说,我需要使用SWING更改菜单栏的字体大小和菜单栏上的每个项目(以及站点的项目) 我有以下代码,但不能在运行时运行,我需要它是当点击菜单项 Font f = new Font("sans-serif", Font.PLAIN, 12); UIManager.put("Menu.font", f); UIManager.put("MenuItem.font", f); 我菜单上的代码是 private class foo{ private JMenu mnArchiv
Font f = new Font("sans-serif", Font.PLAIN, 12);
UIManager.put("Menu.font", f);
UIManager.put("MenuItem.font", f);
我菜单上的代码是
private class foo{
private JMenu mnArchivo;
private JMenuBar menuBar;
menuBar = new JMenuBar();
frmAdministracinHospital.setJMenuBar(menuBar);
JRadioButtonMenuItem rdbtnmntmGrande = new JRadioButtonMenuItem("Grande");
rdbtnmntmGrande.addActionListener(new MiGrandeActionListener());
rdbtnmntmGrande.setIcon(new ImageIcon(PrincipalWindow.class.getResource("/presentacion/fontbig.png")));
buttonGroup.add(rdbtnmntmGrande);
mnTamanoFuente.add(rdbtnmntmGrande);
private class MiGrandeActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Font f = new Font(menuBar.getFont().getFontName(), menuBar.getFont().getStyle(), 12);
UIManager.put("Menu.font", f);
}
}
我还没有发现任何类似的问题,在运行时如何实现这一点
编辑。无需工作即可添加代码字体大小更改多次。
package presentacion;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import dominio.Appointment;
import dominio.Patient;
import dominio.Specialist;
public class pepe {
private JFrame a;
private JTabbedPane tabbedPane;
private JMenuBar menuBar;
private final ButtonGroup buttonGroup = new ButtonGroup();
public pepe() {
initialize();
a.setVisible(true);
}
public static void main(String[] args) {
try {
// Set System L&F
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
}
catch (UnsupportedLookAndFeelException e) {
// handle exception
}
catch (ClassNotFoundException e) {
// handle exception
}
catch (InstantiationException e) {
// handle exception
}
catch (IllegalAccessException e) {
// handle exception
}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
pepe window = new pepe();
window.a.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private void initialize() {
a = new JFrame();
a.setTitle("Administraci\u00F3n Hospital");
a.setBounds(100, 100, 1195, 710);
a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
tabbedPane.setBackground(SystemColor.info);
menuBar = new JMenuBar();
a.setJMenuBar(menuBar);
JMenu mnVer = new JMenu("Ver");
menuBar.add(mnVer);
JMenu mnTamanoFuente = new JMenu("Tama\u00F1o fuente");
mnVer.add(mnTamanoFuente);
JRadioButtonMenuItem rdbtnmntmPequeo = new JRadioButtonMenuItem("Peque\u00F1o");
rdbtnmntmPequeo.addActionListener(new MiPequenaActionListener());
buttonGroup.add(rdbtnmntmPequeo);
mnTamanoFuente.add(rdbtnmntmPequeo);
JRadioButtonMenuItem rdbtnmntmGrande = new JRadioButtonMenuItem("Grande");
rdbtnmntmGrande.addActionListener(new MiGrandeActionListener());
buttonGroup.add(rdbtnmntmGrande);
mnTamanoFuente.add(rdbtnmntmGrande);
}
private class MiPequenaActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Font f = new Font(a.getFont().getFontName(), a.getFont().getStyle(), 10);
UIManager.put("Label.font", f);
UIManager.put("Menu.font", f);
UIManager.put("MenuItem.font", f);
SwingUtilities.updateComponentTreeUI(a);
}
}
private class MiGrandeActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Font f = new Font(a.getFont().getFontName(), a.getFont().getStyle(), 13);
UIManager.put("Label.font", f);
UIManager.put("Menu.font", f);
UIManager.put("MenuItem.font", f);
SwingUtilities.updateComponentTreeUI(a);
}
}
}
有了它,我只需将字体大小更改一次,例如,如果单击“小”或“普通”,它将不会做任何事情
多谢各位
我有以下代码,但在运行时不起作用
基本上,您需要进行LAF更改,因此上面应该是:
Font f = new FontUIResource(menuBar.getFont().getFontName(), menuBar.getFont().getStyle(), 12);
UIManager.put("Menu.font", f);
SwingUtilities.updateComponentTreeUI(frame);
您需要确保字体是FontUIResource,以便LAF可以更改属性
有关更多信息和示例,请阅读上Swing教程的部分
我有以下代码,但在运行时不起作用
基本上,您需要进行LAF更改,因此上面应该是:
Font f = new FontUIResource(menuBar.getFont().getFontName(), menuBar.getFont().getStyle(), 12);
UIManager.put("Menu.font", f);
SwingUtilities.updateComponentTreeUI(frame);
您需要确保字体是FontUIResource,以便LAF可以更改属性
有关更多信息和示例,请阅读上Swing教程的部分。这很有效!非常感谢。如果您知道,还有一个问题,是否可以同时更新所有JLabel?因为我知道一种方法(没有UIManager),但我必须一个接一个地做,这很痛苦。
是否可以同时更新所有JLabel
-为什么这种方法不起作用?如果您有问题,请发布您的帖子。在这篇文章中,我们使用“菜单”和“菜单项”,我的意思是,我必须使用什么作为标签?@UlisesCT,在这篇文章中,我们使用“菜单”和“菜单项”
-我给您一个猜测?例如,如果我将其设置为大,那么如果我尝试将其设置为小或正常大小,它将不起作用。
-对我来说很好(在其他组件上,我没有尝试使用JMenu或JMenuItem)。正如我在前面的评论中所说的,发布您的演示问题。顺便说一句,您可以查看其他UI属性。这很有效!谢谢!如果您知道,还有一个问题,是否可以同时更新所有JLabel?因为我知道一种方法(没有UIManager)但是我必须一个接一个地做,这很痛苦。是否可以同时更新所有JLabel
-为什么这种方法不起作用?如果你有问题,请发布你的。在这里,我们使用“菜单”和“菜单项”,我的意思是,我必须使用什么作为标签?@UlisesCT,在这里,我们使用“菜单”和“菜单项”,
-我给你一个猜测?例如,如果我将其设置为大,那么如果我尝试将其设置为小或正常大小,它将不起作用。
-对我来说很好(在其他组件上,我没有试过JMenu或JMenuItem)。正如我在前面的评论中所说的,请发布您的演示问题的帖子。顺便说一下,您可以查看其他UI属性。