我在 JScrollPane 中有一个 JTextArea 组件,并且文本区域不可编辑。我想使用向上和向下箭头键启用文本区域的滚动(即按箭头键将文本区域滚动一行)。任何想法如何实现这一目标?
5 回答
是的,键绑定是要走的路,但您并不总是需要创建自己的操作。Swing 组件带有您经常可以重复使用的默认操作。
有关这些操作的完整列表,请参阅键绑定。
现在您知道了 Action 名称,您可以将其绑定到 keyStroke:
JScrollBar vertical = scrollPane.getVerticalScrollBar();
InputMap im = vertical.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
im.put(KeyStroke.getKeyStroke("DOWN"), "positiveUnitIncrement");
im.put(KeyStroke.getKeyStroke("UP"), "negativeUnitIncrement");
如果 JTextArea 是不可编辑和不可聚焦的,它不会响应箭头键。我不确定是否有解决此问题的规范方法,但使其响应的一种方法是设置其键绑定以在 JTextArea 位于可聚焦窗口中时响应向上和向下键。一个例子如下:
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.text.JTextComponent;
@SuppressWarnings("serial")
public class TestScrollingArea extends JPanel {
private static final String UP = "Up";
private static final String DOWN = "Down";
private JTextArea area = new JTextArea(20, 40);
private JScrollPane scrollPane = new JScrollPane(area);
public TestScrollingArea() {
// make textarea non-editable and non-focusable
area.setEditable(false);
area.setFocusable(false);
area.setWrapStyleWord(true);
area.setLineWrap(true);
add(scrollPane);
// fill area with letters
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 100; j++) {
area.append("abcdefg ");
}
}
// have JTextArea tell us how tall a line of text is.
int scrollableIncrement = area.getScrollableUnitIncrement(scrollPane.getVisibleRect(),
SwingConstants.VERTICAL, 1);
// add key bindings to the JTextArea
int condition = JTextComponent.WHEN_IN_FOCUSED_WINDOW;
InputMap inMap = area.getInputMap(condition);
ActionMap actMap = area.getActionMap();
inMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), UP);
inMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), DOWN);
actMap.put(UP, new UpDownAction(UP, scrollPane.getVerticalScrollBar().getModel(),
scrollableIncrement));
actMap.put(DOWN, new UpDownAction(DOWN, scrollPane.getVerticalScrollBar().getModel(),
scrollableIncrement));
}
// Action for our key binding to perform when bound event occurs
private class UpDownAction extends AbstractAction {
private BoundedRangeModel vScrollBarModel;
private int scrollableIncrement;
public UpDownAction(String name, BoundedRangeModel model, int scrollableIncrement) {
super(name);
this.vScrollBarModel = model;
this.scrollableIncrement = scrollableIncrement;
}
@Override
public void actionPerformed(ActionEvent ae) {
String name = getValue(AbstractAction.NAME).toString();
int value = vScrollBarModel.getValue();
if (name.equals(UP)) {
value -= scrollableIncrement;
vScrollBarModel.setValue(value);
} else if (name.equals(DOWN)) {
value += scrollableIncrement;
vScrollBarModel.setValue(value);
}
}
}
private static void createAndShowUI() {
JFrame frame = new JFrame("TestScrollingArea");
frame.getContentPane().add(new TestScrollingArea());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
刚刚遇到这个问题,虽然答案有助于我朝着正确的方向前进,但从那时起,一些解决方案可能已经发生了变化。它对我有用,他进行了以下更改: - 必须更改 JScrollPane 实例的 InputMap - actionMapKeys 必须是:“unitScrollX”和/或“scrollX”(X = Down,Up,Left,Right)。它们驻留在 BasicScrollPaneUI 中。
您应该将 KeyListener 添加到您的 JScrollPane。
我所要做的就是使滚动窗格请求专注于鼠标输入(如this answer中所述)。
var scrollPane = new JScrollBar(jPanelCanvas);
scrollPane.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
// this seems to enable key navigation
if ((e.getComponent() instanceof JScrollPane)) {
e.getComponent().requestFocus();
}
}
});
但是我不确定如何调整这些键的操作。也许通过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}