我正在尝试编写一种方法来更改 JtextArea 内的文本大小。
JTextArea editorPanel;
Font editorFont;
public void setSize( int size ) {
editorPanel.setFont( new Font( editorFont.getName(), editorFont.getStyle(), size ) );
}
我在另一个类上有一个内部类 ActionListener,看起来像;
class SizeListener implements ActionListener {
String size;
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox) e.getSource();
size = (String) cb.getSelectedItem();
int i = Integer.parseInt( size );
displayFont = display.getEditorFont();
display.setSize( i );
}
}
我已经在 JComboBox 中实现了这个 Action 监听器,这样当我从 ComboBox 中选择新的“大小”时,JTextArea 的文本大小应该根据选择增加或减少。我可以使用哪种方法或实现来解决这个问题?
最佳答案
最简单的方法是根据现有的 Font
派生新的 Font
:
JComboBox cb = (JComboBox) e.getSource();
Integer itemSize = (Integer) cb.getSelectedItem();
float fontSize = itemSize.IntValue();
Font font = editorPanel.getFont();
editorPanel.setFont( font.deriveFont( fontSize ) );
请注意,在这种情况下,您将向组合框添加整数值,而不是字符串值,因此无需解析所选项目。
关于java - 如何改变JTextArea内容的字体大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61471123/