我有一个 JTextArea,可以使用 JButtons 填充文本。我想要一个 JButton,我可以在不使用 Robot 类或退格键的情况下使用退格键,而只需用鼠标单击屏幕上的按钮即可。如何使用此按钮,通过使用自行创建的退格键,使用 JTextArea 的
public void actionPerformed(ActionEvent e) {
来操作文本?如果您有任何疑问或对我的问题感到困惑,请告诉我。
Document
。
Swing 中的每个文本组件都有一个
Document
模型,用于控制文本的状态(以及适用的属性和结构)。
您可以使用
JTextArea
的Document
直接删除字符。
类似...
Document doc = textArea.getDocument();
doc.remove(doc.getLength() - 2, 1);
您可以从字符串中提取方法
String text = textArea.getText();
textArea.setText(text.subString(0, text.length() - 1);
从 JtextArea 中删除最后一行 JtextArea.setText(JtextArea.getText().substring(0, JtextArea.getText().lastIndexOf(" “)));