gpt4 book ai didi

java - 字体,返回默认大小

转载 作者:行者123 更新时间:2023-12-01 19:01:14 26 4
gpt4 key购买 nike

每次我更改字体时,它都会返回到默认大小,即 12,即使我之前使用“ Jade 野”菜单更改它,它每次也只会返回到 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 */
}
}

提前致谢!

最佳答案

首先,您实际上只需要一个 Action 监听器:

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 {
...
}
}

关于java - 字体,返回默认大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12208198/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com