从 JTextArea 中清除附加文本

Clear Appended Text from JTextArea

我正在使用 JTextArea 显示一个 ArrayList,方法是一次一个地将条目附加到区域中。但是,当我使用方法 .setText("") 清除所有附加条目时,它们不会被删除。有没有清除文本区域的方法?

如果您使用的是 JTextArea,并且您希望在输入特定文本后清除所有条目,那么只需将原始的预附加文本存储在一个字符串中,比如说 myOriginalText。而不是打电话 .setText(""); 呼叫 .setText(myOriginalText)。另一种选择是直接使用 JTextArea 的文档,获取原始文本末尾的索引值,然后删除文档中该索引之后的所有文本。

例如:

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

@SuppressWarnings("serial")
public class TextAreaFun extends JPanel {
    private JTextArea textArea = new JTextArea(20, 40);
    private JTextField textEntry = new JTextField(25);
    private AppendAction appendAction = new AppendAction("Append Text");
    private ProtectAction protectAction = new ProtectAction("Protect Text");
    private ClearAction clearAction = new ClearAction("Clear Text");
    private String protectedText = "";

    public TextAreaFun() {
        textArea.setFocusable(false);
        textArea.setEditable(false);
        JScrollPane taScrollPane = new JScrollPane(textArea);
        taScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        textEntry.setAction(appendAction);

        JPanel bottomPanel = new JPanel();
        bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.LINE_AXIS));
        bottomPanel.add(textEntry);
        bottomPanel.add(new JButton(appendAction));
        bottomPanel.add(new JButton(protectAction));
        bottomPanel.add(new JButton(clearAction));        

        setLayout(new BorderLayout());
        add(taScrollPane, BorderLayout.CENTER);
        add(bottomPanel, BorderLayout.PAGE_END);
    }

    private class AppendAction extends AbstractAction {
        public AppendAction(String name) {
            super(name);
            int mnemonic = (int) name.charAt(0);
            putValue(MNEMONIC_KEY, mnemonic);
            putValue(SHORT_DESCRIPTION, "Append text to text area");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            textArea.append(textEntry.getText() + "\n");
            textEntry.selectAll();
            textEntry.requestFocusInWindow();
        }
    }

    private class ProtectAction extends AbstractAction {
        public ProtectAction(String name) {
            super(name);
            int mnemonic = (int) name.charAt(0);
            putValue(MNEMONIC_KEY, mnemonic);
            putValue(SHORT_DESCRIPTION, "Protext text in text area from being cleared");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            protectedText = textArea.getText();
            textEntry.selectAll();
            textEntry.requestFocusInWindow();
        }
    }

    private class ClearAction extends AbstractAction {
        public ClearAction(String name) {
            super(name);
            int mnemonic = (int) name.charAt(0);
            putValue(MNEMONIC_KEY, mnemonic);
            putValue(SHORT_DESCRIPTION, "Clear unprotected text from text area");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            textArea.setText(protectedText);
            textEntry.selectAll();
            textEntry.requestFocusInWindow();
        }
    }

    private static void createAndShowGui() {
        TextAreaFun mainPanel = new TextAreaFun();

        JFrame frame = new JFrame("Text Area Fun");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

如果要根据程序的状态添加或删除 ArrayList 中的每个项目,那么您最好不要在 JTextArea 中显示文本,而是在可以处理每个项目的 JList 中显示文本在 JList 中独立。