0

每次我更改字体时,它都会恢复到默认大小,即 12,即使我之前使用“Tamano”菜单更改它,它每次也只会恢复到 12,我猜是我更改的方式大小与派生字体(),但我现在没有任何其他方式来改变它。

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 */        
    }
}

提前致谢!

4

2 回答 2

2

原因在于addActionListener()您最终添加相同的方法 aryal 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));
    }
});

注意:这里我假设您正在尝试更改JLabel texto的字体大小。但是无论你想在哪里使用它,这个想法都是一样的。

于 2012-08-31T02:49:21.170 回答
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 {
        ...
    }
}
于 2012-08-31T02:53:48.867 回答