简体   繁体   English

鼠标中键单击时自动滚动JTextArea

[英]Auto scroll JTextArea on middle mouse button click

How to auto scroll JTextArea on middle mouse button click? 如何在鼠标中键单击时自动滚动JTextArea To be clear, when we click the mouse wheel (middle button) in Firefox (for example) and push it down/up we will be able to scroll the page automatically when we release the middle button, we can alo see a round icon at the point of middle button click. 明确地说,当我们在Firefox中单击鼠标滚轮(中间按钮)(例如)并向下/向上推时,我们将能够在释放中间按钮时自动滚动页面,我们还可以在看到一个圆形图标单击中间按钮的点。

Here is a screenshot of it. 这是它的屏幕截图。

自动滚动JTextArea图标

I think there is no such functionality for JTextArea in Java. 我认为Java中的JTextArea没有这种功能。 Is there any hack to implement it. 是否有实现它的技巧。

Any answer is appreciated. 任何答案表示赞赏。

To determine which of the Mouse buttons is pressed, these three methods from SwingUtilities could help you: 要确定按下了哪个鼠标按钮, SwingUtilities以下三种方法可以帮助您:

isLeftMouseButton isLeftMouseButton

isMiddleMouseButton isMiddleMouseButton

isRightMouseButton isRightMouseButton

Based on the mouse button clicked, you can take appropriate action to scroll the JTextArea programmatically. 基于单击的鼠标按钮,您可以采取适当的措施以编程方式滚动JTextArea。 Probably, you can use something like this: 可能,您可以使用如下所示的内容:

textArea.setCaretPosition(textArea.getDocument().getLength()); -> to move to the end of the JTextArea ->移至JTextArea的末尾

You can look at this link to get a good idea of positioning the cursor position in JTextArea 您可以查看链接,以获得在JTextArea中定位光标位置的好主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM