有没有办法让JTextArea的末尾可编辑,并使已经打印到它的任何内容都不可编辑?如何使JTextArea的末尾可编辑
我的意思是,如果我已经写了“Hello World”例如一个JTextArea,我怎么能让它使得用户可以在“Hello World”之后键入任何他们想要的内容,但是他们之前不能输入或删除已经打印的文本?
下面是一个小程序,以证明我的烦恼......
public class Test {
public static void main(String[] args) {
//Here I create a simple JFrame with JTextArea
JTextArea textArea = new JTextArea();
JFrame frame = new JFrame();
JFrame.setDefaultLookAndFeelDecorated(true);
frame.setSize(250, 250);
textArea.setEditable(true);
textArea.setVisible(true);
frame.add(textArea);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/*Here I print "Hello World" onto the text area.. after the ">>" I want the
the user to be able to type whatever they want.. however I don't want them
to be able to edit the "Hello World"*/
textArea.append("Hello World\n>>");
textArea.setCaretPosition(textArea.getDocument().getLength());
}
}
在用户能够进入任何他们想要的文字..的例子就是我想要的。但是他们也能编辑我使用append打印的文本..我不想要..
我该如何解决这个问题?
DocumentFilter可能工作。我会试验一下。 –
也演示了[这里](https://stackoverflow.com/questions/15017148/jtextarea-as-io-console/15025085#15025085);) – MadProgrammer