0

Java swing を使用して電子ブック リーダー プログラムを作成しています。
txt ファイルを JTextArea に読み込みました。

JTextComponent.read(Reader in, Object desc). 

@Andrew Thompson のおかげで、キャレット位置を使用して JTextArea の特定の位置にジャンプできるようになりました。しかし、テキストをナビゲートしながらキャレットを更新する方法がまだわからないので、ブックマークとして保存できます。

私の問題を示すためにいくつかのコードを書きました:

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;


public class MyProblem extends JFrame {

    public MyProblem() {
        JTextArea textArea = new JTextArea(20, 60);
        textArea.setEditable(false);
        String aLine = "Line number: ";
        String newLine = System.getProperty("line.separator");
        for (int i=0; i<20; i++) {
            textArea.insert(aLine + i + newLine, textArea.getDocument().getLength());
        }
        String problem = "When scrolling, how can set caret postion automatically on the first line of the viewport so I can save it?";
        textArea.append(problem);
        textArea.append(newLine);
        for (int i=21; i<39; i++) {
            textArea.insert(aLine + i + newLine, textArea.getDocument().getLength());
        }

        JScrollPane scrollPane = new JScrollPane(textArea);
        add(scrollPane);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new MyProblem().setVisible(true);
            }
        });
    }
}
4

0 に答える 0