프로그램의 경우, KeyListener를 사용하여 '1'버튼을 눌렀을 때 ArrayList에 무언가를 추가했습니다. 이 목록에있는 객체는 지속적으로 시각화됩니다. KeyListener를 사용하면 버튼을 계속 누르고 있어도 유창했습니다.Java Swing - KeyListener보다 훨씬 느린 ActionListener
나중에 JMenuBar를 GUI에 추가했습니다. ArrayList에 무언가를 추가하면 액셀레이터가 KeyStroke '1'로 설정된 자체 JMenuItem과 전에 KeyListener와 동일한 작업을 수행하는 ActionListener가 있습니다. 그러나 성능이 매우 나쁩니다. '1'키를 계속 누르고 있으면 KeyListener와 비교해 매우 느려집니다.
왜 그렇게 느린가요? 내가 뭔가 잘못하고 있는거야? 더 좋은 방법이 있습니까?
...
AL al = new AL();
menu.add(createMenuItem("Add", KeyEvent.VK_1, al));
}
private JMenuItem createMenuItem(String text, int key, ActionListener al){
JMenuItem menuItem = new JMenuItem(text);
menuItem.setAccelerator(KeyStroke.getKeyStroke(key, 0));
menuItem.addActionListener(al);
return menuItem;
}
private class AL implements ActionListener{
public void actionPerformed(ActionEvent e){
int keycode = ((JMenuItem)e.getSource()).getAccelerator().getKeyCode();
bla(keycode);
}
}
당신의'keyPressed (keycode);'코드는 무엇을해야합니까? –
이 메서드를 전혀 변경하지 않았으므로 KeyListener와 잘 작동합니다. 하지만 귀하의 질문에 대답 해주십시오. (Keycode == KeyEvent.VK_1) 인 경우 ArrayList에 객체를 추가하거나 == KeyEvent.VK_ESCAPE 인 경우 프로그램을 종료합니다. – Morrow
키 리스너를 트리핑하면됩니다. –