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

问题描述 投票:0回答:1

我正在尝试编写一种更改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中实现了这个动作侦听器,所以当我从ComboBox中选择一个新的”大小”时,应根据选择增加或减小JTextArea的文本大小。我可以使用哪种方法或实现来解决此问题?

java actionlistener jcombobox jtextarea
1个回答
0
投票

最简单的方法是根据现有的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值而不是String值添加到组合框中,因此无需解析所选的项目。

© www.soinside.com 2019 - 2024. All rights reserved.