(描述后代码中的内容会很清楚)
我有一个包含JScrollBar,JTextArea,JTextFields,JButton和其他一些东西的程序。 (我只添加了SSCCE代码)
好的,我为JScrollBar的左右JButtons添加了键绑定,我使用了getInputMap
和getActionMap
来实现这一点。
如果我的GUI程序中有JScrollBar而没有可编辑的组件(如JTextField或JTextArea),那么keyStrokes KeyEvent.VK_LEFT
和KeyEvent.VK_RIGHT
将正常工作(如预期的那样)
但是,如果我有一个带有JScrollBar的可编辑组件,那么它将不会响应KeyEvent.VK_LEFT
和KeyEvent.VK_RIGHT
(未预期)为什么会发生这种情况!?
更奇怪的事情是,如果我选择不同的keyStrokes与左边的KeyEvent.VK_S
和右边的KeyEvent.VK_F
绑定,它会起作用!!现在为什么?
如何修复,当可编辑组件共存时,如何为JScrollBar JButton创建左右箭头的键绑定?
这是SSCCE风格的两个工作代码。第一个包含没有可编辑组件的JScrollBar,第二个包含带有JTextArea的JScrollBar。
第一个可以正常工作,它会响应键盘的左右箭头。 (它不包含可编辑的组件)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ScrollTest extends JPanel
{
JPanel panel;
JScrollBar scrollBar;
JButton sliderLeftButton;
JButton sliderRightButton;
public ScrollTest()
{
scrollBar = new JScrollBar(JScrollBar.HORIZONTAL, 0, 6, 0, 300);
sliderLeftButton = (JButton) scrollBar.getAccessibleContext().getAccessibleChild(1);
sliderLeftButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "leftmove");
sliderLeftButton.getActionMap().put("leftmove", leftmove);
sliderRightButton = (JButton) scrollBar.getAccessibleContext().getAccessibleChild(0);
sliderRightButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "rightmove");
sliderRightButton.getActionMap().put("rightmove", rightMove);
panel = new JPanel(new GridLayout(2, 0));
panel.add(scrollBar);
this.setLayout(new BorderLayout());
this.add(panel, BorderLayout.NORTH);
}
AbstractAction leftmove = new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent ae)
{
int increment = scrollBar.getBlockIncrement();
int current = scrollBar.getValue();
current -= increment;
scrollBar.setValue(current);
System.out.println("left");
}
};
AbstractAction rightMove = new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent ae)
{
int increment = scrollBar.getBlockIncrement();
int current = scrollBar.getValue();
current += increment;
scrollBar.setValue(current);
System.out.println("right");
}
};
private static void createAndShowGUI()
{
JFrame frame;
frame = new JFrame("Scroll Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(880, 100);
frame.add(new ScrollTest(), BorderLayout.CENTER);
frame.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
}
}
第二个 NOT 工作,它不会响应键盘的左右箭头。 (它确实包含可编辑的组件 - 一个JTextArea)
更奇怪的事情
将KeyEvent.VK_LEFT
替换为KeyEvent.VK_S
,将KeyEvent.VK_RIGHT
替换为KeyEvent.VK_F
。现在 S 将适用于左边, F 将适用于右边!为什么它不适用于左右箭头。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ScrollTest extends JPanel
{
JPanel panel;
JPanel panel2;
JScrollBar scrollBar;
JButton sliderLeftButton;
JButton sliderRightButton;
public ScrollTest()
{
scrollBar = new JScrollBar(JScrollBar.HORIZONTAL, 0, 6, 0, 300);
sliderLeftButton = (JButton) scrollBar.getAccessibleContext().getAccessibleChild(1);
sliderLeftButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "leftmove");
sliderLeftButton.getActionMap().put("leftmove", leftmove);
sliderRightButton = (JButton) scrollBar.getAccessibleContext().getAccessibleChild(0);
sliderRightButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "rightmove");
sliderRightButton.getActionMap().put("rightmove", rightMove);
panel = new JPanel(new GridLayout(2, 0));
panel.add(scrollBar);
panel2 = new JPanel(new GridLayout(1, 0));
panel2.add(new JTextArea(50, 10));
this.setLayout(new BorderLayout());
this.add(panel, BorderLayout.NORTH);
this.add(panel2, BorderLayout.SOUTH);
}
AbstractAction leftmove = new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent ae)
{
int increment = scrollBar.getBlockIncrement();
int current = scrollBar.getValue();
current -= increment;
scrollBar.setValue(current);
System.out.println("left");
}
};
AbstractAction rightMove = new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent ae)
{
int increment = scrollBar.getBlockIncrement();
int current = scrollBar.getValue();
current += increment;
scrollBar.setValue(current);
System.out.println("right");
}
};
private static void createAndShowGUI()
{
JFrame frame;
frame = new JFrame("Scroll Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(880, 100);
frame.add(new ScrollTest(), BorderLayout.CENTER);
frame.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
}
}
答案 0 :(得分:0)
这是因为JTextArea正在窃取你的击键:
textPane.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "pressed RIGHT");
textPane.getActionMap().put("pressed RIGHT", rightMove);
查看 BasicTextUI.installKeyboardActions()