2016-12-28 57 views
0

如標題所示,我需要使用SWING更改菜單欄及其上的每個項目(以及ite的項目)的字體大小。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 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); 
     } 
    } 

} 

有了這個它讓我改變字體大小隻有一次,要大,例如(大),如果然後我點擊小或正常它不會做任何事情。

謝謝。

回答

3

我有下面的代碼工作,但不是在運行時,

Font f = new Font(menuBar.getFont().getFontName(), menuBar.getFont().getStyle(), 12); 
UIManager.put("Menu.font", f); 

基本上你需要做一個LAF變化,因此上述應該是:

Font f = new FontUIResource(menuBar.getFont().getFontName(), menuBar.getFont().getStyle(), 12); 
UIManager.put("Menu.font", f); 
SwingUtilities.updateComponentTreeUI(frame); 

你需要確保Font是一個FontUIResource,所以LAF可以更改屬性。

閱讀有關Changing the LAF After Startup的Swing教程的部分以獲取更多信息和示例。

+0

工作!謝謝!還有一個問題,如果你知道,是否有可能同樣更新所有JLabel?因爲我知道一種方式(沒有UIManager),但我必須一對一地做,這是一種痛苦。 –

+1

'是否可以在同一個位置更新所有JLabel? - 爲什麼這種方法不起作用?如果您有問題,請發佈您的[mcve]。 – camickr

+0

在這裏我們使用的是「Menu」和「MenuItem」,我的意思是,我需要使用什麼標籤? –