在你链接到你的例子中,你会发现你想要做的一些线索。
线
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);
}
他想的是“字体大小成比例的规模了已经在文档中增加。 “但是,您的示例将文档中的所有字体设置为相同的大小。 – ka3ak 2013-02-28 06:02:48