Every time I change the font, it goes back to the default size, which is 12, even if I change it before with the “Tamano” menu, it only goes back to 12 every time, my guess would be the way I change the size with deriveFont(), but don’t I now any other way to change it.
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 */
}
}
Thanks in Advance!
First off, you really just need one action listener:
Then when you create your JMenuItems:
The
deriveFontmethod returns the font set to the specified size, but it doesn’t actually change the font itself. Therefore you need to callsetFontwith the newly sized font.In general to change the font size:
EDIT
In response to the question, the above
MenuListenercode either needs to go in its own class called MenuListener.java (but you have to make itpublic) or you’d put it in the classcambiar: