问题描述
好.以编程方式插入组件很明显:myJTextPane.insertComponent
.
OK. Inserting a component programatically is obvious: myJTextPane.insertComponent
.
访问组件有些棘手,但我使用的是类似myJTextPane.getComponents().getComponents()[0]
的东西. (1)
Accessing components was a little trickier, but I use something like: myJTextPane.getComponents().getComponents()[0]
. (1)
但是如何以编程方式从myJTextPane中删除组件?
But how do I remove a component programatically from myJTextPane?
(1)我实际上是在Clojure中编程,因此语法可能不是100%.
(1) I am actually programming in Clojure, so the syntax may not be 100%.
推荐答案
您将其视为特定位置的字符:
You treat it as a character at a specific position:
myJTextPane.getDocument().remove(int offs, int len)
例如,如果您的文本窗格中的组件顺序如下:
For example if you have a text pane with components in this order:
[Component1]-[Component2]-[Component3]-一些文本
[Component1] - [Component2] - [Component3] - some text
,并且您要删除第二个和第三个组件:
and you want to remove 2nd and 3rd components:
myJTextPane.getDocument().remove(1, 2)
请参见文档
这篇关于如何从JTextPane删除组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!