我想在JtextPane中有一個規則,例如 如果它包含「 - 」(短劃線)作爲一個字符,否則它不會加粗。如何根據輸入的文本添加規則到JtextPane
我可以在documentFilter中添加一些規則嗎? 我在哪裏可以添加此規則
我想在JtextPane中有一個規則,例如 如果它包含「 - 」(短劃線)作爲一個字符,否則它不會加粗。如何根據輸入的文本添加規則到JtextPane
我可以在documentFilter中添加一些規則嗎? 我在哪裏可以添加此規則
首先使用StyledEditorKit(或者擴展例如一個HTMLEditorKit)。 添加一個DocumentListener。在每次更改時,您都可以檢查新輸入的單詞是否包含' - '字符。我會添加「'鍵入的檢查。 使用實用程序類來獲取單詞開始和單詞結束。 創建一個SimpleAttributeSet並使用StyleConstants.setBold()將其設置爲打開或關閉。然後使用((StyledDocument)yourTextpane.getDocument())。setCharacterAttributes()設置/重置粗體狀態。
嗯,是的Text Component Features教程和DocumentFilter能做到這一點,例如:
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
public class TextAreaTest extends JFrame {
private static final long serialVersionUID = 1L;
private JTextArea textArea;
public TextAreaTest() {
textArea = new JTextArea();
textArea.setPreferredSize(new Dimension(60, 32));
textArea.setOpaque(true);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
((AbstractDocument) textArea.getDocument()).setDocumentFilter(new DocumentFilter() {
@Override
public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
string = string.replaceAll("-", "");
super.insertString(fb, offset, string, attr);
}
@Override
public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
text = text.replaceAll("-", "");
//TODO must do something here
super.replace(fb, offset, length, text, attrs);
}
});
textArea.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void changedUpdate(DocumentEvent e) {
update(e);
}
@Override
public void insertUpdate(DocumentEvent e) {
update(e);
}
@Override
public void removeUpdate(DocumentEvent e) {
update(e);
}
private void update(DocumentEvent e) {
List<String> lines = getLines(textArea);
String lastLine = lines.get(lines.size() - 1);
int tabbedTextWidth = Utilities.getTabbedTextWidth(new Segment(
lastLine.toCharArray(), 0, lastLine.length()), textArea.getFontMetrics(textArea.getFont()), 0, null, 0);
int lineHeight = getLineHeight(textArea);
if (lines.size() * lineHeight > textArea.getHeight() || tabbedTextWidth > textArea.getWidth()) {
System.out.println("Too big! Should refuse the update!");
}
}
});
getContentPane().add(textArea);
}
private static List<String> getLines(JTextArea textArea) {
int lineHeight = getLineHeight(textArea);
List<String> list = new ArrayList<String>();
for (int num = 0;; num++) {
int i = textArea.viewToModel(new Point(0, num * lineHeight));
int j = textArea.viewToModel(new Point(0, (num + 1) * lineHeight));
if (i == 0 && j == 0) {
continue;
}
if (textArea.getDocument().getLength() == i && i == j) {
break;
}
String s = removeTrailingNewLine(textArea.getText().substring(i, j));
list.add(s);
//System.out.println(i + " " + j + " = " + s);
}
return list;
}
private static int getLineHeight(JTextArea textArea) {
return textArea.getFontMetrics(textArea.getFont()).getHeight();
}
private static String removeTrailingNewLine(String s) {
if (s.endsWith("\n")) {
return s.substring(0, s.length() - 1);
} else {
return s;
}
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TextAreaTest test = new TextAreaTest();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.pack();
test.setVisible(true);
}
});
}
}
Arjit詢問如何在JTextPane中使文本變爲粗體。無法在您使用的JTextArea中添加樣式。 – StanislavL 2011-06-07 10:39:05
@StanislavL現在同意這一切都合情合理+1 – mKorbel 2011-06-07 11:00:59