Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.event.*;
- class MnemonicAcceleratorExample extends JFrame {
- public MnemonicAcceleratorExample() {
- setTitle("Mnemonic and Accelerator Example");
- setSize(300, 200);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- JMenuBar menuBar = new JMenuBar();
- setJMenuBar(menuBar);
- JMenu fileMenu = new JMenu("File");
- fileMenu.setMnemonic(KeyEvent.VK_F);
- menuBar.add(fileMenu);
- JMenuItem openMenuItem = new JMenuItem("Open");
- openMenuItem.setMnemonic(KeyEvent.VK_O);
- openMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
- fileMenu.add(openMenuItem);
- JMenuItem saveMenuItem = new JMenuItem("Save");
- saveMenuItem.setMnemonic(KeyEvent.VK_S);
- saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
- fileMenu.add(saveMenuItem);
- openMenuItem.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- System.out.println("Open file selected");
- }
- });
- saveMenuItem.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- System.out.println("Save file selected");
- }
- });
- }
- }
- public class GUI{
- public static void main(String[] args) {
- MnemonicAcceleratorExample example = new MnemonicAcceleratorExample();
- example.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement