JScrollPane 使用方向键滚动
我在 JScrollPane 中有一个 JTextArea 组件,并且文本区域不可编辑。我想使用向上和向下箭头键启用文本区域的滚动(即按箭头键将使文本区域滚动一行)。有什么想法如何实现这一目标?
I've a JTextArea component inside JScrollPane and the text area is not editable. I would like to enable scrolling of the text area with up and down arrow keys (i.e. pressing the arrow keys will scroll the text area by one line). Any ideas how to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,键绑定是可行的方法,但您并不总是需要创建自己的操作。 Swing 组件附带了您经常可以重用的默认操作。
有关这些操作的完整列表,请参阅按键绑定。
现在您知道了 Action 名称,您可以将其绑定到 keyStroke:
Yes Key Bindings is the way to go, but you don't always need to create your own actions. Swing components come with default Actions that you can often reuse.
See Key Bindings for a complete list of these Actions.
Now that you know the Action name you can just bind it to a keyStroke:
如果 JTextArea 不可编辑且不可聚焦,则它将不会响应箭头键。我不确定是否有一种规范的方法来解决这个问题,但使其响应的一种方法是设置其键绑定,以在 JTextArea 位于可聚焦窗口中时响应向上和向下键。示例如下:
If the JTextArea is non-editable and non-focuseable, it will not respond to the arrow keys. I'm not sure if there is a canonical way to get around this, but one way to make it respond is to set its key binding to respond to the up and down keys when the JTextArea is in the focusable window. An example of this is as follows:
刚刚遇到这个问题,虽然答案对于推动我走向正确的方向很有用,但从那时起,解决方案的某些部分可能已经发生了变化。它对我有用,他进行了以下更改:
- 必须更改 JScrollPane 实例的 InputMap
- actionMapKeys 必须是:“unitScrollX”和/或“scrollX”(X= 下、上、左、右)。它们驻留在 BasicScrollPaneUI 中。
Just came across this problem and while the answers was useful in driving me to the right direction some bits of the solution may have changed since then. It worked for me with he following changes:
- it was the InputMap of JScrollPane instance that had to be changed
- actionMapKeys had to be: "unitScrollX" and/or "scrollX" (X= Down, Up, Left, Right). They reside in BasicScrollPaneUI.
您应该将 KeyListener 添加到 JScrollPane 中。
You should add KeyListener to your JScrollPane.
我所要做的就是使滚动窗格请求聚焦于鼠标输入(如回答)。
但是我不确定如何调整这些键的操作。也许可以按照 tinca 的答案 所述直接调整
JScrollPane
上的操作。对
scrollPane.getActionMap()
的调用显示了定义的以下操作"unitScrollRight" -> {BasicScrollPaneUI$Actions@4310}
“unitScrollDown”-> {BasicScrollPaneUI$Actions@4312}
“scrollDown”-> {BasicScrollPaneUI$Actions@4314}
“scrollHome”-> {BasicScrollPaneUI$Actions@4316}
“scrollRight”-> {BasicScrollPaneUI$Actions@4318}
“scrollUp”-> {BasicScrollPaneUI$Actions@4320}
“unitScrollLeft”-> {BasicScrollPaneUI$Actions@4322}
“unitScrollUp”-> {BasicScrollPaneUI$Actions@4324}
“scrollEnd”-> {BasicScrollPaneUI$Actions@4326}
“scrollLeft”-> {BasicScrollPaneUI$Actions@4328}
All I had to do was to make the scroll pane request focus on mouse enter (as explained in this answer).
However I'm not sure on how to tweak the actions of these keys. Maybe by tweaking the actions on the
JScrollPane
directly as mentioned by tinca's answer.The call to
scrollPane.getActionMap()
are showing the following actions defined"unitScrollRight" -> {BasicScrollPaneUI$Actions@4310}
"unitScrollDown" -> {BasicScrollPaneUI$Actions@4312}
"scrollDown" -> {BasicScrollPaneUI$Actions@4314}
"scrollHome" -> {BasicScrollPaneUI$Actions@4316}
"scrollRight" -> {BasicScrollPaneUI$Actions@4318}
"scrollUp" -> {BasicScrollPaneUI$Actions@4320}
"unitScrollLeft" -> {BasicScrollPaneUI$Actions@4322}
"unitScrollUp" -> {BasicScrollPaneUI$Actions@4324}
"scrollEnd" -> {BasicScrollPaneUI$Actions@4326}
"scrollLeft" -> {BasicScrollPaneUI$Actions@4328}