最近寫java又有問題了= =|||(因為我是菜鳥><)
一個Frame內有:一個jTextArea
一個jButton
用戶按下jButton後,在jTextArea內選中的文字改成粗體,要怎樣做?我用jTextArea.setFont(new Font("", Font.BOLD,12));
但是所有字型都改了...
請問有什麼方法?
一個Frame內有:一個jTextArea
一個jButton
用戶按下jButton後,在jTextArea內選中的文字改成粗體,要怎樣做?我用jTextArea.setFont(new Font("", Font.BOLD,12));
但是所有字型都改了...
請問有什麼方法?
...
MutableAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setFontFamily(attr, "Font.BOLD");
StyleConstants.setFontSize(attr, 12);
StyledDocument doc = (StyledDocument) sourcePane.
getDocument();
doc.setCharacterAttributes(
sourcePane.getSelectionStart(),
sourcePane.getSelectionEnd()-sourcePane.getSelectionStart(),
attr, false);
StyleConstants.setFontFamily(attr, "宋体");