在链接到的示例中,您将找到有关您要执行的操作的一些线索.
线
StyleConstants.setFontSize(attrs, font.getSize());
更改JTextPane的字体大小并将其设置为您作为参数传递给此方法的字体的大小.您要基于当前大小将其设置为新大小的内容.
//first get the current size of the font
int size = StyleConstants.getFontSize(attrs);
//now increase by 2 (or whatever factor you like)
StyleConstants.setFontSize(attrs, size * 2);
这将导致JTextPane的字体大小加倍.您当然可以较慢的速度增加.
现在,您需要一个按钮来调用您的方法.
JButton b1 = new JButton("Increase");
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
increaseJTextPaneFont(text);
}
});
因此,您可以编写类似于示例中的方法:
public static void increaseJTextPaneFont(JTextPane jtp) {
MutableAttributeSet attrs = jtp.getInputAttributes();
//first get the current size of the font
int size = StyleConstants.getFontSize(attrs);
//now increase by 2 (or whatever factor you like)
StyleConstants.setFontSize(attrs, size * 2);
StyledDocument doc = jtp.getStyledDocument();
doc.setCharacterAttributes(0, doc.getLength() + 1, attrs, false);
}