[英]Clear Appended Text from JTextArea
我正在使用JTextArea通過一次將條目追加到區域中來顯示ArrayList。 但是,當我使用方法.setText(“”)清除所有附加條目時,它們不會被刪除。 反正有清除文本區域的方法嗎?
如果您使用的是JTextArea,並且想在輸入某些文本后清除所有條目,則只需將該原始的預先添加的文本存儲在String中,例如稱為myOriginalText
。 而不是調用.setText("");
調用.setText(myOriginalText)
。 另一個選擇是直接使用JTextArea的Document,獲取原始文本結尾的索引值,然后刪除Document中該索引之后的所有文本。
例如:
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中的每個項目。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.