Hi friend,
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ShortCutKeysDemo extends JApplet {
JButton button;
JCheckBox checkBox;
JMenuBar menuBar;
JMenu fileMenu, fileMenu1;
JMenuItem exitMenuItem;
JMenuItem openMenuItem, copyMenuItem;
JPanel panel;
public void init(){
Container container = this.getContentPane();
Handler handler = new Handler();
checkBox = new JCheckBox("Hello Rose!");
checkBox.setMnemonic(java.awt.event.KeyEvent.VK_M);
checkBox.addActionListener(handler);
button = new JButton("Welcome to Rose!");
button.setMnemonic(java.awt.event.KeyEvent.VK_D);
button.addActionListener(handler);
panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.add(checkBox);
panel.add(button);
exitMenuItem = new JMenuItem("Exit");
exitMenuItem.setMnemonic('x');
exitMenuItem.addActionListener(handler);
fileMenu = new JMenu("File");
fileMenu.setMnemonic('f');
copyMenuItem = new JMenuItem("Copy");
copyMenuItem.setMnemonic('c');
copyMenuItem.addActionListener(handler);
fileMenu1 = new JMenu("Edit");
fileMenu1.setMnemonic('e');
fileMenu1.add(copyMenuItem);
fileMenu.add(exitMenuItem);
openMenuItem = new JMenuItem("Open");
openMenuItem.setMnemonic('O');
openMenuItem.addActionListener(handler);
fileMenu.add(openMenuItem);
menuBar = new JMenuBar();
menuBar.add(fileMenu);
menuBar.add(fileMenu1);
container.add(menuBar, BorderLayout.NORTH);
container.add(panel, BorderLayout.CENTER);
}
class Handler implements ActionListener{
public void actionPerformed(ActionEvent ae){
if (ae.getSource() == checkBox) {
System.out.println("Action Performed on CHECKBOX");
}
else if(ae.getSource() == button)
{
System.out.println("Action Performed on BUTTON");
}
else if(ae.getSource() == exitMenuItem)
{
System.exit(0);
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Short Cut Keys");
ShortCutKeysDemo shortdemo = new ShortCutKeysDemo();
shortdemo.init();
frame.getContentPane().add(shortdemo);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we){
System.exit(0);
}
}
);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
---------------------------------------------------
Visit for more information.
http://www.roseindia.net/java/example/java/swing/Thanks.