Java.Swing.button绑定快捷键的3种办法

1.使用button.setMnemonic方法,例如:jbtRemove.setMnemonic(java.awt.event.KeyEvent.VK_D);     

注意使用这个方法只能用alt+d才可以使用快捷键,可以看作是绑定了热键。


2.使用button.registerKeyboardAction()方法,这里可以用button.registerKeyboardAction() 来为该按钮注册一个事件监听;查询JDK,该方法属于JButton的父类 JComponent 的一个方法:
public void registerKeyboardAction(ActionListener anAction, String aCommand, KeyStroke aKeyStroke, int aCondition)

public void registerKeyboardAction(ActionListener anAction, KeyStroke aKeyStroke, int aCondition)
参数:
anAction - 要注册的 Action
aCommand - 要在传递的事件中设置的命令
aKeyStroke - 与动作绑定的 KeyStroke
aCondition - 要符合的条件,见上文

例如jbtRemove.registerKeyboardAction(new BtnActionListener(),KeyStroke.getKeyStroke(KeyEvent.VK_D,0), JComponent.WHEN_IN_FOCUSED_WINDOW);


BtnActionListener为自己写的一个实现ActionListener 接口的类,处理按钮的单击事件.


例如:
private class BtnActionListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			Object[] values = jlist.getSelectedValues();
			for(int i=0;i

也可以写作
jbtRemove.registerKeyboardAction(new ActionListener() 
	{
		Object[] values = jlist.getSelectedValues();
			for(int i=0;i


3.使用监听器KeyAdapter。给控件添加键盘事件,也就是当键盘是的键按下时,会触发这个事件,然后调用keyPressed函数。无需alt
例如:
jlist.addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub
				int keyCode=e.getKeyCode();  //获取按的键盘上的健
				if(keyCode==KeyEvent.VK_D ){  //比较
					Object[] values = jlist.getSelectedValues();
					for(int i=0;i

这是源代码:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.DefaultListModel;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;


public class ListModelDemo extends JApplet {
	private DefaultListModel listModel = new DefaultListModel();
	private JList jlist = new JList(listModel);
	private JButton jbtAdd = new JButton("Add new item");
	private JButton jbtRemove = new JButton("Remove selected item");
	
	public ListModelDemo(){
		listModel.addElement("Iteml");
		listModel.addElement("Item2");
		listModel.addElement("Item3");
		listModel.addElement("Item4");
		listModel.addElement("Item5");
		listModel.addElement("Item6");
		
//		jbtRemove.registerKeyboardAction(new BtnActionListener(), KeyStroke.getKeyStroke(KeyEvent.VK_D,0), JComponent.WHEN_IN_FOCUSED_WINDOW);
//		jbtRemove.setMnemonic(KeyEvent.VK_D);
		JPanel panel = new JPanel();
		panel.add(jbtAdd);
		panel.add(jbtRemove);
		
		add(panel,BorderLayout.NORTH);
		add(new JScrollPane(jlist), BorderLayout.CENTER);
		
		jbtAdd.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String newItem = JOptionPane.showInputDialog("Enter a new item");
				if(newItem!=null){
					if(jlist.getSelectedIndex()==-1)
						listModel.addElement(newItem);
					else
						listModel.add(jlist.getSelectedIndex(), newItem);
				}
			}
		});
		
		jbtRemove.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				Object[] values = jlist.getSelectedValues();
				for(int i=0;i





你可能感兴趣的:(Java.Swing.button绑定快捷键的3种办法)