簡體   English   中英

如何在JTextArea中的行之間切換

[英]How to switch between lines in JTextArea in swing

我正在嘗試設置goto行號以在我的swing應用程序中的行之間切換。 我使用getLineCount()獲得了JTextArea實例中的行數,但不知道如何getLineCount()之間切換?

有人可以建議我嗎?

您可以使用textArea.setCaretPosition設置插入符位置。 這樣的事情會起作用

textArea.setCaretPosition(textArea.getDocument().getDefaultRootElement()
                         .getElement(index).getStartOffset());

在下面的示例中,我只使用一個JComboBox ,並使用JTextArea的行號填充索引。 當您在組合框中選擇一個數字時,插入符號將移動到JTextArea那一行。

該程序雖然不是一個很好的程序。 您可能希望通過添加和刪除行來動態填充ComboBoxModel 但這應該為您提供所需的答案。

免責聲明

我沒有添加任何功能來使滾動窗格聚焦於當前行。 您可能需要查看下面的@Balder評論,以尋求幫助。

在此處輸入圖片說明

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class TestCaret {

    public TestCaret() {
        JTextArea textArea = createTextArea();
        JComboBox cBox = createComboBox(textArea);

        JFrame frame = new JFrame();
        frame.add(new JScrollPane(textArea));
        frame.add(cBox, BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private JComboBox createComboBox(final JTextArea textArea) {
        DefaultComboBoxModel<Integer> model  = new DefaultComboBoxModel<>();
        int lines = textArea.getLineCount();
        for (int i = 0; i < lines; i++) {
            model.addElement(i);
        }
        final JComboBox cBox = new JComboBox(model);
        cBox.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                int index = (Integer)cBox.getSelectedItem();
                textArea.setCaretPosition(
                        textArea.getDocument().getDefaultRootElement().getElement(index).getStartOffset());
                textArea.requestFocusInWindow();
            }
        });
        return cBox;
    }

    private JTextArea createTextArea() {
        JTextArea textArea = new JTextArea(10, 50);
        textArea.setMargin(new Insets(15, 15, 15, 15));
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        String text 
                = "0 Hello World\n" + 
                "1 Hello World\n" + 
                "2 Hello World\n" + 
                "3 Hello World\n" + 
                "4 Hello World\n" + 
                "5 Hello World\n" + 
                "6 Hello World\n" +
                "7 Hello World\n" + 
                "8 Hello World\n" + 
                "9 Hello World\n"; 
        textArea.setText(text);
        return textArea;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new TestCaret();
            }
        });
    }
}

一種方法是通過以下方式建立while語句:

String line = null;
while ((line = text.readLine())!=null && line.getLineNumber() < desiredNumber) {}

最后,行將引用具有所需行號的行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM