Java 字体,返回默认大小

Java 字体,返回默认大小,java,swing,fonts,actionlistener,Java,Swing,Fonts,Actionlistener,每次我更改字体时,它都会返回到默认大小,即12,即使我以前使用“Tamano”菜单更改它,每次也只会返回到12,我猜是我使用deriveFont()更改大小的方式,但我现在没有其他方法更改它了 public static class cambiar extends JFrame { public cambiar() { final Font aryal = new Font("Comic Sans MS", Font.PLAIN, 12); JFrame

每次我更改字体时,它都会返回到默认大小,即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 */        
    }
}

提前谢谢

原因是在添加相同aryal
font
addActionListener()
方法中

我建议进行如下修改:

.addActionListener(new ActionListener(){ 
            public void actionPerformed(ActionEvent e){
                texto.setFont(aryal.deriveFont(size)); //where size is the size that you want to set
            }
        });
JMenuItem font20 = new JMenuItem("20");
font20.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        texto.setFont(aryal.deriveFont(20));
    }
});
例如,如果要将
字体
大小更改为20,则可能需要执行以下操作:

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

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

原因是在
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
            }
        });
JMenuItem font20 = new JMenuItem("20");
font20.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        texto.setFont(aryal.deriveFont(20));
    }
});
例如,如果要将
字体
大小更改为20,则可能需要执行以下操作:

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

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

首先,你真的只需要一个动作监听器:

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));
            }
        }
    }
}
然后,在创建项目时:

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的类中(但必须将其
公开),要么放在类
cambiar

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

    private class MenuListener implements ActionListener {
        ...
    }
}

首先,您只需要一个动作侦听器:

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));
            }
        }
    }
}
然后,在创建项目时:

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的类中(但必须将其
公开),要么放在类
cambiar

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

    private class MenuListener implements ActionListener {
        ...
    }
}

JMenuItems上的侦听器将字体设置为
aryal
,字体大小为12。您实际上在哪里尝试更改大小?在Jmenuitem font13中,我将其设置为23.0f。JMenuItems上的侦听器将字体设置为
aryal
,字体大小为12。您实际上在哪里尝试更改大小?在Jmenuitem font13中,我将其设置为23.0f。我在哪里声明“private ActionListener MenuListener”?@BladimirRuiz我编辑了我的响应以解释将MenuListener类代码放在哪里。我在哪里声明“private ActionListener MenuListener”?@BladimirRuiz我编辑了我的回复,解释了MenuListener类代码的放置位置。