Reputation: 177
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!
Upvotes: 0
Views: 637
Reputation: 15179
First off, you really just need one action listener:
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));
}
}
}
}
Then when you create your JMenuItems:
MenuListener listener = new MenuListener();
JMenuItem font18 = new JMenuItem("18");
font18.setActionListener(listener);
The deriveFont
method returns the font set to the specified size, but it doesn't actually change the font itself. Therefore you need to call setFont
with the newly sized font.
In general to change the font size:
Font font; // some font you already have instantiated
float size = 20f; // the target font size
Font newFont = font.deriveFont(size); // the newly sized font
EDIT
In response to the question, the above MenuListener
code either needs to go in its own class called MenuListener.java (but you have to make it public
) or you'd put it in the class cambiar
:
public class cambiar extends JFrame {
... your existing code here ...
private class MenuListener implements ActionListener {
...
}
}
Upvotes: 2
Reputation: 6783
The reason is in the addActionListener()
method you're ending up adding the same aryal font
.
I would suggest a modification like this:
.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
texto.setFont(aryal.deriveFont(size)); //where size is the size that you want to set
}
});
For example, if you would want to change the font
size to 20, you might want to do something like this:
JMenuItem font20 = new JMenuItem("20");
font20.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
texto.setFont(aryal.deriveFont(20));
}
});
Note: Here I'm assuming that you're trying to change the font size of the JLabel
texto. But the idea would be the same wherever you want to use it.
Upvotes: 2