私はDefaultStyledDocumentに追加した4つの段落を抜き出しています。しかし、それは私が期待するように行動していない。Swing DefaultStyledDocument traversal
私は間違っていますか?私はここに完全なコードを追加しました - それが要求されたものでした。
import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Element;
public class MainFrame extends JFrame {
JTextPane jTextPane = new JTextPane();
public static void main(String[] args) {
new MainFrame().init();
try {
Thread.sleep(95000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void init() {
JFrame frame = new JFrame();
frame.setSize(1000, 800);
frame.setVisible(true);
jTextPane.setSize(995, 795);
frame.add(jTextPane);
DefaultStyledDocument document = new DefaultStyledDocument();
try {
document.insertString(document.getLength(), "DDDD\n", null);
document.insertString(document.getLength(), "CCCC\n", null);
document.insertString(document.getLength(), "BBBB\n", null);
document.insertString(document.getLength(), "AAAA\n", null);
} catch (BadLocationException e) {
e.printStackTrace();
}
document.dump(System.out);
jTextPane.setDocument(document);
for (int x = 0; x < 20; x += 5) {
Element paraGE = document.getParagraphElement(x);
int startOff = paraGE.getStartOffset();
int endOff = paraGE.getEndOffset();
String s = null;
try {
s = document.getText(startOff, endOff);
} catch (BadLocationException e) {
e.printStackTrace();
}
System.out.println(s);
}
}
}
javax.swing.text.BadLocationException: Invalid location
at javax.swing.text.GapContent.getChars(GapContent.java:189)
at javax.swing.text.GapContent.getString(GapContent.java:167)
at javax.swing.text.AbstractDocument.getText(AbstractDocument.java:770)
at blah.MainFrame.init(MainFrame.java:60)
at blah.MainFrame.main(MainFrame.java:14)
javax.swing.text.BadLocationException: Invalid location
at javax.swing.text.GapContent.getChars(GapContent.java:189)
at javax.swing.text.GapContent.getString(GapContent.java:167)
at javax.swing.text.AbstractDocument.getText(AbstractDocument.java:770)
at blah.MainFrame.init(MainFrame.java:60)
at blah.MainFrame.main(MainFrame.java:14)
null
null
:
未テストコードは次のようなものかもしれません。また、あなたのコードのどの行が例外を引き起こした行なのかを説明してください。 – VGR
私はあなたがここで習得しようとしていると思っているように、_notは動作しないと定義するべきですか? – Nicolas