gpt4 book ai didi

java - 如何防止 JTextField 中的箭头键滚动 JScrollPane?

转载 作者:行者123 更新时间:2023-12-03 19:01:46 24 4
gpt4 key购买 nike

我想为我的 JTextField 添加自定义箭头键监听器。但看起来箭头键已绑定(bind)到我放置文本字段的 JScrollPane。如何解除绑定(bind)?

最佳答案

您可以尝试替换 key bindings在滚动 Pane 上,但保留它们以允许用户在他们没有聚焦在您的文本字段中时滚动 Pane 可能是有意义的。

相反,您可以将键绑定(bind)添加到不执行任何操作的文本字段,这将消耗事件并阻止它们开始发送到滚动 Pane ,例如......

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.Scrollable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestScrollPaneKeyBinding {

public static void main(String[] args) {
new TestScrollPaneKeyBinding();
}

public TestScrollPaneKeyBinding() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(new TestPane()));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel implements Scrollable {

public TestPane() {
JTextField field = new JTextField(20);
InputMap im = field.getInputMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "Arrow.up");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "Arrow.down");
AbstractAction doNothing = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
// doing nothing
}
};
ActionMap am = field.getActionMap();
am.put("Arrow.up", doNothing);
am.put("Arrow.down", doNothing);
add(field);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.dispose();
}

@Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(100, 100);
}

@Override
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
return 32;
}

@Override
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
return 32;
}

@Override
public boolean getScrollableTracksViewportWidth() {
return false;
}

@Override
public boolean getScrollableTracksViewportHeight() {
return false;
}
}

}

关于java - 如何防止 JTextField 中的箭头键滚动 JScrollPane?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18652481/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com