在你鏈接到你的例子中,你會發現你想要做的一些線索。
線
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