2012-08-31 82 views
0

每次我改變字體,它會回到默認大小,即12,即使我以前用「Tamano」菜單改變它,它每次只能回到12 ,我的猜測將是我用deriveFont()更改大小的方式,但現在不用我改變它的任何其他方式。字體,回到默認大小

public static class cambiar extends JFrame { 

    public cambiar() { 
     final Font aryal = new Font("Comic Sans MS", Font.PLAIN, 12); 
     JFrame ventana = new JFrame("Cambios en el Texto!"); 
     JPanel adentro = new JPanel(); 
     final JLabel texto = new JLabel("Texto a Cambiar!"); 
     texto.setFont(aryal); 
     JMenuBar menu = new JMenuBar(); 

     JMenu fuentes = new JMenu("Fuentes"); 
     /* Elementos de Fuentes */ 
     JMenuItem arial = new JMenuItem("Arial"); 
     arial.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       Font arrrial = new Font("Arial", Font.PLAIN, 12); 
       float tam = (float) texto.getFont().getSize(); 
       String hola = String.valueOf(tam); 
       texto.setFont(arrrial); 
       texto.setFont(texto.getFont().deriveFont(tam)); 
      } 
     }); 



     fuentes.add(arial); 
     /* FIN Fuentes */ 


     JMenu tamano = new JMenu("Tamano"); 

     /* Elementos de Tamano */ 
     JMenuItem font13 = new JMenuItem("13"); 
     font13.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       texto.setFont(texto.getFont().deriveFont(23.0f)); 
      } 
     }); 

     JMenuItem font14 = new JMenuItem("14"); 
     arial.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       texto.setFont(aryal); 
      } 
     }); 

     JMenuItem font15 = new JMenuItem("15"); 
     arial.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       texto.setFont(aryal); 
      } 
     }); 

     JMenuItem font16 = new JMenuItem("16"); 
     arial.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       texto.setFont(aryal); 
      } 
     }); 

     JMenuItem font17 = new JMenuItem("17"); 
     arial.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       texto.setFont(aryal); 
      } 
     }); 

     JMenuItem font18 = new JMenuItem("18"); 
     arial.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       texto.setFont(aryal); 
      } 
     }); 

     JMenuItem font19 = new JMenuItem("19"); 
     arial.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       texto.setFont(aryal); 
      } 
     }); 

     JMenuItem font20 = new JMenuItem("20"); 
     arial.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       texto.setFont(aryal); 
      } 
     }); 

     tamano.add(font13); 
     /* FIN tanano */ 

     JMenu tipo = new JMenu("Tipo"); 

     /* Elementos de tipo */ 

     /* FIN tipo */ 


     /* Elementos del JMENU */ 
     menu.add(fuentes); 
     menu.add(tamano); 
     menu.add(tipo); 
     /* FIN JMENU */ 

     /* Elementos del JPanel */ 
     adentro.add(menu); 
     adentro.add(texto); 
     /* FIN JPanel */ 

     /* Elementos del JFRAME */ 
     ventana.add(adentro); 
     ventana.setVisible(true); 
     ventana.setSize(250, 250); 
     /* FIN JFRAME */   
    } 
} 

在此先感謝!

+0

JMenuItems上的聽衆將字體設置爲「aryal」,字體大小爲12。您究竟在哪裏嘗試更改大小? –

+0

在Jmenuitem font13中,我將它設置爲23.0f。 –

+0

爲什麼「靜態」類? –

回答

2

首先,你真的只需要一個動作監聽器:

private class MenuListener implements ActionListener { 
    @Override public void actionPerformed(ActionEvent e) { 
     Object caller = e.getSource(); 
     if (caller != null && caller.instanceOf JMenuItem) { 
      JMenuItem src = (JMenuItem)caller; 
      String size = src.getText(); 

      if (size != null) { 
       float fontSize = Float.parseFloat(size); 
       texto.setFont(aryal.deriveFont(fontSize)); 
      } 
     } 
    } 
} 

然後,當你創建你的JMenuItems:

MenuListener listener = new MenuListener(); 
JMenuItem font18 = new JMenuItem("18"); 
font18.setActionListener(listener); 

deriveFont方法返回的字體設置爲指定大小,但它實際上並沒有改變的字體本身。因此您需要用新尺寸的字體呼叫setFont

一般來改變字體大小:

Font font; // some font you already have instantiated 
float size = 20f; // the target font size 
Font newFont = font.deriveFont(size); // the newly sized font 

編輯

在回答這個問題,上述MenuListener代碼或者需要在其所謂的MenuListener.java自己的類去(但你必須使它public你會把它放在類cambiar

public class cambiar extends JFrame { 
    ... your existing code here ... 

    private class MenuListener implements ActionListener { 
     ... 
    } 
} 
+0

我在哪裏聲明「private ActionListener MenuListener」? –

+0

@BladimirRuiz我編輯了我的回覆,以解釋MenuListener類代碼的放置位置。 –

2

原因是在addActionListener()方法你結束了添加相同的亞里爾font

我建議這樣的修改:

.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e){ 
       texto.setFont(aryal.deriveFont(size)); //where size is the size that you want to set 
      } 
     }); 

例如,如果你想給font大小更改爲20,你可能想要做這樣的事情:

JMenuItem font20 = new JMenuItem("20"); 
font20.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
     texto.setFont(aryal.deriveFont(20)); 
    } 
}); 

注意:在這裏,我假設您正在嘗試更改文字的大小,即JLabeltexto。但這個想法在任何你想使用它的地方都是一樣的。