JScrollPane: Blinker(cursor) 被边框覆盖

JScrollPane: Blinker(cursor) is covered by the border

我正在开发我自己的小文本编辑器。可以在 JScrollPane 中编辑文件,现在如果一行文本比 window 长,您可以按原样向右滚动。但是当 Blinker(或者它叫什么)在行的最后时,它是不可见的,因为它似乎被边框覆盖了。

//the JTextArea is inside the JScrollPane of course
Border scrollPaneBorder = new LineBorder(interfaceColor, 8, true);
Border textAreaBorder = new LineBorder(backgroundColor, 4, true);   

将两个边框都设置为 0 不会改变任何内容。有没有人有办法解决这个问题?

我认为最优雅的解决方案是 Notepad++ 中的解决方案。一旦你靠近边界,它就会以某种方式在文本和边界之间放置一些 space 。但我不知道 how/if 这在 java 中是可能的。

不管是什么解决方案,谢谢大家的帮助。

来自documentation of setMargin

Sets margin space between the text component's border and its text. The text component's default Border object will use this value to create the proper margin. However, if a non-default border is set on the text component, it is that Border object's responsibility to create the appropriate margin space (else this property will effectively be ignored).

有点奇怪,Swing的文本组件如果没有边距,会把​​光标向上移动到被绘制覆盖的位置Border,不过这解释了为什么设置自定义的时候会出现这个问题Border 但不使用默认边框,因为边距的默认值 属性 是非零的。

您可以创建一个读取边距 属性 的自定义边框并实现该尺寸的未绘制内部区域,或者,如果您不需要支持不同的边距值 属性,您可以将边框与空边框组合起来以获得类似的效果(硬编码边距 space):

Border textAreaBorder=BorderFactory.createCompoundBorder(
  BorderFactory.createLineBorder(backgroundColor, 4, true),
  BorderFactory.createEmptyBorder(2, 2, 2, 2)
);