Fonts, goes back to default size

Posted by Bladimir Ruiz on Stack Overflow See other posts from Stack Overflow or by Bladimir Ruiz
Published on 2012-08-31T02:38:03Z Indexed on 2012/08/31 3:38 UTC
Read the original article Hit count: 379

Filed under:
|
|
|

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!

© Stack Overflow or respective owner

Related posts about java

Related posts about swing