多个视口到同一个 JTextArea 上?
我想要制作一个组件,它显示 JTextArea 中一组单词的所有匹配项,以及一些上下文(N 行,可能是用户可配置的)。
我已经有了用于突出显示匹配项的代码,因此我知道它们的偏移量因此可以确定它们的界限。我知道文本的行高,因此我可以确定要绘制的上下文区域的边界。
但是是否有像 JViewport 这样的类只能绘制底层组件的多个视图?由于 JViewport 是一个普通的 Swing 组件,因此一旦将相同的文本区域放在另一个视口下,它就会与第一个视口分离。
编辑:实际上,事实证明,我似乎无法确定上下文区域的边界,因为 JTextArea 提供的行到偏移量映射不计算换行数。
There is a component I would like to make which shows all matches of a set of words in a JTextArea, along with some context (N lines, probably user-configurable.)
I already have the code for highlighting the matches so I know their offsets and can therefore determine their bounds. I know the line height of the text so I can determine the bounds of the context areas I want to paint.
But is there some class like JViewport only able to paint multiple views of the underlying component? Since JViewport is a normal Swing component, as soon as you put the same text area under another viewport, it gets detached from the first one.
Edit: Actually as it turns out, I can't seem to determine the bounds of the context areas as the lines-to-offset mapping JTextArea gives me doesn't count wrapped lines.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里的问题是JViewport是一个普通的容器,并将其视图组件视为子组件(并且每个组件只能是一个容器中的子组件)。这是非常合理的,因为始终只有一个组件可以获得焦点,以及类似的事情。
我不知道有任何类似 JViewport 的类可以执行您想要的操作(绘制组件的视图而不是其父级),但在您的情况下,您可以通过使用两个共享相同内容的 JTextArea 来实现大部分相同的效果文档。我想,你只需要同步插入符号的移动(如果愿意的话)。
The problem here is that JViewport is a normal container, and treats its view component as a child component (and each component can only be child in one container). This is quite reasonable, given that always only one component can have the focus, and similar things.
I don't know of any JViewport-like class that would do what you want (painting a view of a component without being parent of it), but in your case, you can achieve most of the same by using two JTextAreas sharing the same Document. You then only would have to synchronize the caret movements (if so wished), I think.
没有任何东西像你描述的那样存在。事实上,听起来您想自定义 JTextArea 的显示。在这种情况下,您可以覆盖并更改 JTextArea 绘制自身的方式。要么在上面画更多的东西,要么彻底改变它。
There is nothing that exists as you describe. In fact it sounds like you want to customize the display of a JTextArea. In which case you can override and change the way JTextArea paints itself. Either painting more on top, or changing it completely.
在图像中绘制 JTextArea 并使用图像的必要片段。将图像的边界保留在原始 JTextArea 中,以便单击图像,您可以计算单击 JTextArea 以获得正确的位置。
Paint your JTextArea in an Image and use necessary fragments of the image. Keep the image's bounds in original JTextArea so clicking bu the image you can calculate click on the JTextArea to get proper position.