Java AbstractAction set mnemonic key
import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.JButton; import javax.swing.JFrame; public class Main extends JFrame { public Main() { super("Action object with JButton"); setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(new FlowLayout()); JButton closeButton1;// w w w. jav a2 s . co m Action closeAction = new CloseAction(); closeButton1 = new JButton(closeAction); getContentPane().add(closeButton1); } public static void main(String[] args) { Main frame = new Main(); frame.pack(); frame.setVisible(true); } } class CloseAction extends AbstractAction { public CloseAction() { super("Close"); putValue(Action.MNEMONIC_KEY, KeyEvent.VK_C); } @Override public void actionPerformed(ActionEvent event) { System.exit(0); } }