繁体   English   中英

如何更改 JTextArea 内容的字体大小?

[英]How to change the font size of the content of the JTextArea?

我正在尝试编写一种方法来更改 JtextArea 内的文本大小。

JTextArea editorPanel;
Font editorFont;

public void setSize( int size ) {
      editorPanel.setFont( new Font( editorFont.getName(), editorFont.getStyle(), size ) );
 }

我在另一个 class 上有一个内部类 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 中实现了这个动作监听器,所以当我从 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 ) );

请注意,在这种情况下,您会将 Integer 值添加到组合框中,而不是字符串值,因此无需解析所选项目。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM