gpt4 book ai didi

java - 使用 JButton 增加/减少 textArea 内的字体大小

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:41:46 25 4
gpt4 key购买 nike

我正在使用 Java 创建一个便签应用程序。

我想做什么:每次单击增加大小时,我都想增加 textArea 中文本的大小。显然我会知道如何做相反的事情。

短代码:

        JButton incButton = new JButton("+");
fontFrame.add(incButton);
incButton.addActionListener(new fontIncAction());
JButton DecButton = new JButton("-");
fontFrame.add(DecButton);

//textArea.setFont( Font("Serif", Font.PLAIN, fz));
}
}

private class fontIncAction implements ActionListener{
public void actionPerformed(ActionEvent e){

textArea.setFont(new Font("Serif",Font.PLAIN,20));
}
}

最佳答案

为了使代码更通用,您可以在 ActionListener 中执行以下操作:

Font font = textArea.getFont();
float size = font.getSize() + 1.0f;
textArea.setFont( font.deriveFont(size) );

关于java - 使用 JButton 增加/减少 textArea 内的字体大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8675038/

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