在您链接到的示例中,您会发现一些您正在尝试做的事情的线索。
线
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);
}