【发布时间】:2018-03-17 13:46:19
【问题描述】:
有没有办法使 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 可能会起作用。我会尝试一下。
-
还演示了here ;)