JButton 加事件监听(鼠标和键盘)

ActionListener okListener=new ActionListener(){
			
	public void actionPerformed(ActionEvent evt) {
	
		//storing the filechooser's current directory
		Editor.getEditor().getResourcesManager().setCurrentDirectory(fileChooser.getCurrentDirectory());
		setVisible(false);
	}
};

okButton=new JButton(okLabel);
			
//the ok action name
String actionName="okAction";
			
//registering the ok action
Action okAction=new AbstractAction(actionName){
				
	public void actionPerformed(ActionEvent e) {

		okButtonListener.actionPerformed(e);
	}
};
			
okButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), actionName);
okButton.getActionMap().put(actionName, okAction);

okButton.addActionListener(okButtonListener);
 

你可能感兴趣的:(button)