/**
   * @param str name of Menu
   * @param bFlag set mnemonics on menu items
   */
  private JMenuBar createMenuBar(String str, boolean bFlag) {
    menubar = new JMenuBar();

    menu1 = new JMenu(str);
    menu1.setMnemonic(str.charAt(0));
    menu1.addActionListener(this);

    menubar.add(menu1);
    for (int i = 0; i < 8; i++) {
      JMenuItem menuitem = new JMenuItem("1 JMenuItem" + i);
      menuitem.addActionListener(this);
      if (bFlag) {
        menuitem.setMnemonic('0' + i);
      }
      menu1.add(menuitem);
    }

    // second menu
    menu2 = new JMenu("2 - Second Menu");
    menu2.addActionListener(this);
    menu2.setMnemonic('2');

    menubar.add(menu2);
    for (int i = 0; i < 5; i++) {
      JMenuItem menuitem = new JMenuItem("2 JMenuItem" + i);
      menuitem.addActionListener(this);

      if (bFlag) {
        menuitem.setMnemonic('0' + i);
      }
      menu2.add(menuitem);
    }
    JMenu submenu = new JMenu("Sub Menu");
    submenu.setMnemonic('S');
    submenu.addActionListener(this);
    for (int i = 0; i < 5; i++) {
      JMenuItem menuitem = new JMenuItem("S JMenuItem" + i);
      menuitem.addActionListener(this);
      if (bFlag) {
        menuitem.setMnemonic('0' + i);
      }
      submenu.add(menuitem);
    }
    menu2.add(new JSeparator());
    menu2.add(submenu);

    return menubar;
  }
  /** Adds the menu items to the menuber. */
  protected void arrangeMenu() {

    // Build the first menu.
    fileMenu = new JMenu("File");
    fileMenu.setMnemonic(KeyEvent.VK_F);
    menuBar.add(fileMenu);

    viewMenu = new JMenu("View");
    viewMenu.setMnemonic(KeyEvent.VK_V);
    menuBar.add(viewMenu);

    runMenu = new JMenu("Run");
    runMenu.setMnemonic(KeyEvent.VK_R);
    menuBar.add(runMenu);

    // Build the second menu.
    helpMenu = new JMenu("Help");
    helpMenu.setMnemonic(KeyEvent.VK_H);
    menuBar.add(helpMenu);

    programMenuItem = new JMenuItem("Load Program", KeyEvent.VK_O);
    programMenuItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            programMenuItem_actionPerformed();
          }
        });
    fileMenu.add(programMenuItem);

    scriptMenuItem = new JMenuItem("Load Script", KeyEvent.VK_P);
    scriptMenuItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            scriptMenuItem_actionPerformed();
          }
        });
    fileMenu.add(scriptMenuItem);
    fileMenu.addSeparator();

    exitMenuItem = new JMenuItem("Exit", KeyEvent.VK_X);
    exitMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.ALT_MASK));
    exitMenuItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            exitMenuItem_actionPerformed();
          }
        });
    fileMenu.add(exitMenuItem);

    viewMenu.addSeparator();

    ButtonGroup animationRadioButtons = new ButtonGroup();

    animationSubMenu = new JMenu("Animate");
    animationSubMenu.setMnemonic(KeyEvent.VK_A);
    viewMenu.add(animationSubMenu);

    partAnimMenuItem = new JRadioButtonMenuItem("Program flow");
    partAnimMenuItem.setMnemonic(KeyEvent.VK_P);
    partAnimMenuItem.setSelected(true);
    partAnimMenuItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            partAnimMenuItem_actionPerformed();
          }
        });
    animationRadioButtons.add(partAnimMenuItem);
    animationSubMenu.add(partAnimMenuItem);

    fullAnimMenuItem = new JRadioButtonMenuItem("Program & data flow");
    fullAnimMenuItem.setMnemonic(KeyEvent.VK_D);
    fullAnimMenuItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            fullAnimMenuItem_actionPerformed();
          }
        });
    animationRadioButtons.add(fullAnimMenuItem);
    animationSubMenu.add(fullAnimMenuItem);

    noAnimMenuItem = new JRadioButtonMenuItem("No Animation");
    noAnimMenuItem.setMnemonic(KeyEvent.VK_N);
    noAnimMenuItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            noAnimMenuItem_actionPerformed();
          }
        });
    animationRadioButtons.add(noAnimMenuItem);
    animationSubMenu.add(noAnimMenuItem);

    ButtonGroup additionalDisplayRadioButtons = new ButtonGroup();

    additionalDisplaySubMenu = new JMenu("View");
    additionalDisplaySubMenu.setMnemonic(KeyEvent.VK_V);
    viewMenu.add(additionalDisplaySubMenu);

    scriptDisplayMenuItem = new JRadioButtonMenuItem("Script");
    scriptDisplayMenuItem.setMnemonic(KeyEvent.VK_S);
    scriptDisplayMenuItem.setSelected(true);
    scriptDisplayMenuItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            scriptDisplayMenuItem_actionPerformed();
          }
        });
    additionalDisplayRadioButtons.add(scriptDisplayMenuItem);
    additionalDisplaySubMenu.add(scriptDisplayMenuItem);

    outputMenuItem = new JRadioButtonMenuItem("Output");
    outputMenuItem.setMnemonic(KeyEvent.VK_O);
    outputMenuItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            outputMenuItem_actionPerformed();
          }
        });
    additionalDisplayRadioButtons.add(outputMenuItem);
    additionalDisplaySubMenu.add(outputMenuItem);

    compareMenuItem = new JRadioButtonMenuItem("Compare");
    compareMenuItem.setMnemonic(KeyEvent.VK_C);
    compareMenuItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            compareMenuItem_actionPerformed();
          }
        });
    additionalDisplayRadioButtons.add(compareMenuItem);
    additionalDisplaySubMenu.add(compareMenuItem);

    noAdditionalDisplayMenuItem = new JRadioButtonMenuItem("Screen");
    noAdditionalDisplayMenuItem.setMnemonic(KeyEvent.VK_N);
    noAdditionalDisplayMenuItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            noAdditionalDisplayMenuItem_actionPerformed();
          }
        });
    additionalDisplayRadioButtons.add(noAdditionalDisplayMenuItem);
    additionalDisplaySubMenu.add(noAdditionalDisplayMenuItem);

    ButtonGroup formatRadioButtons = new ButtonGroup();

    numericFormatSubMenu = new JMenu("Format");
    numericFormatSubMenu.setMnemonic(KeyEvent.VK_F);
    viewMenu.add(numericFormatSubMenu);

    decMenuItem = new JRadioButtonMenuItem("Decimal");
    decMenuItem.setMnemonic(KeyEvent.VK_D);
    decMenuItem.setSelected(true);
    decMenuItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            decMenuItem_actionPerformed();
          }
        });
    formatRadioButtons.add(decMenuItem);
    numericFormatSubMenu.add(decMenuItem);

    hexaMenuItem = new JRadioButtonMenuItem("Hexadecimal");
    hexaMenuItem.setMnemonic(KeyEvent.VK_H);
    hexaMenuItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            hexaMenuItem_actionPerformed();
          }
        });
    formatRadioButtons.add(hexaMenuItem);
    numericFormatSubMenu.add(hexaMenuItem);

    binMenuItem = new JRadioButtonMenuItem("Binary");
    binMenuItem.setMnemonic(KeyEvent.VK_B);
    binMenuItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            binMenuItem_actionPerformed();
          }
        });
    formatRadioButtons.add(binMenuItem);
    numericFormatSubMenu.add(binMenuItem);

    viewMenu.addSeparator();

    singleStepMenuItem = new JMenuItem("Single Step", KeyEvent.VK_S);
    singleStepMenuItem.setAccelerator(KeyStroke.getKeyStroke("F11"));
    singleStepMenuItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            singleStepMenuItem_actionPerformed();
          }
        });
    runMenu.add(singleStepMenuItem);

    ffwdMenuItem = new JMenuItem("Run", KeyEvent.VK_F);
    ffwdMenuItem.setAccelerator(KeyStroke.getKeyStroke("F5"));
    ffwdMenuItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            ffwdMenuItem_actionPerformed();
          }
        });
    runMenu.add(ffwdMenuItem);

    stopMenuItem = new JMenuItem("Stop", KeyEvent.VK_T);
    stopMenuItem.setAccelerator(KeyStroke.getKeyStroke("shift F5"));
    stopMenuItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            stopMenuItem_actionPerformed();
          }
        });
    runMenu.add(stopMenuItem);

    rewindMenuItem = new JMenuItem("Reset", KeyEvent.VK_R);
    rewindMenuItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            rewindMenuItem_actionPerformed();
          }
        });
    runMenu.add(rewindMenuItem);

    runMenu.addSeparator();

    breakpointsMenuItem = new JMenuItem("Breakpoints", KeyEvent.VK_B);
    breakpointsMenuItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            breakpointsMenuItem_actionPerformed();
          }
        });
    runMenu.add(breakpointsMenuItem);

    profilerMenuItem = new JMenuItem("Profiler", KeyEvent.VK_I);
    profilerMenuItem.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            showProfiler();
          }
        });
    profilerMenuItem.setEnabled(false);
    runMenu.add(profilerMenuItem);

    usageMenuItem = new JMenuItem("Usage", KeyEvent.VK_U);
    usageMenuItem.setAccelerator(KeyStroke.getKeyStroke("F1"));
    usageMenuItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            usageMenuItem_actionPerformed();
          }
        });
    helpMenu.add(usageMenuItem);

    aboutMenuItem = new JMenuItem("About ...", KeyEvent.VK_A);
    aboutMenuItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            aboutMenuItem_actionPerformed();
          }
        });
    helpMenu.add(aboutMenuItem);
  }
Exemplo n.º 3
0
  /**
   * Adds the menus to the main frame Includes adding ActionListeners to respond to menu commands
   */
  private void addMenus() {
    JMenuBar menuBar = new JMenuBar();
    JMenu gameMenu = new JMenu("Game");
    gameMenu.setMnemonic('G');

    JMenuItem newMenu = new JMenuItem("New Game");
    newMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK));

    newMenu.addActionListener(
        new ActionListener() {
          /**
           * Responds to the New menu option
           *
           * @param event The event that selected this menu option
           */
          public void actionPerformed(ActionEvent e) {
            newGame();
            time = 0;
          }
        });

    JMenuItem exitMenu = new JMenuItem("Exit");
    exitMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK));

    exitMenu.addActionListener(
        new ActionListener() {
          /**
           * Responds to the exit game option being selected
           *
           * @param event The event that selected this menu option
           */
          public void actionPerformed(ActionEvent event) {
            System.exit(0);
          }
        });
    gameMenu.add(newMenu);
    gameMenu.add(exitMenu);
    menuBar.add(gameMenu);

    JMenu helpMenu = new JMenu("Help");
    helpMenu.setMnemonic('H');
    JMenuItem instructionsMenuItem = new JMenuItem("Instructions", 'I');
    instructionsMenuItem.addActionListener(
        new ActionListener() {
          /**
           * Responds to the help option being selected
           *
           * @param event The event that selected this menu option
           */
          public void actionPerformed(ActionEvent event) {
            JOptionPane.showMessageDialog(
                BearSweeper.this,
                "The objective of the game is to flag Fabear's hiding spots so people know where to avoid.\n"
                    + "If you click on an area that Fabear does not reside, a digit is revealed.\n"
                    + "This digit indicates the number of adjacent squares in which Fabear resides.\n"
                    + "Mark Fabear's hiding spots by right clicking."
                    + "If you click on a spot where Fabear resides, you will wake up and unleash his wrath.\n"
                    + "Note: You only have enough flags for exactly the number of hiding spots!",
                "Help",
                JOptionPane.INFORMATION_MESSAGE);
          }
        });
    instructionsMenuItem.setAccelerator(
        KeyStroke.getKeyStroke(KeyEvent.VK_I, InputEvent.CTRL_MASK));
    JMenuItem aboutMenuItem = new JMenuItem("About...", 'A');
    aboutMenuItem.addActionListener(
        new ActionListener() {
          /**
           * Responds to the about option being selected
           *
           * @param event The event that selected this menu option
           */
          public void actionPerformed(ActionEvent event) {
            JOptionPane.showMessageDialog(
                BearSweeper.this,
                "Fabear is now a well-known hero on Earth.\n"
                    + "He has saved Earth in the Tera sagas, but not without sacrifice.\n"
                    + "Although the Earth is now clean from the dangerous substances left behind by the aliens, Fabear has changed...\n"
                    + "He had come in contact with the substance and is now under the control of the aliens.\n"
                    + "It is your turn to save Fabear, from himself..."
                    + "He is currently sleeping.\n"
                    + "Find Fabear's hiding spots without waking him from his sleep."
                    + "When all of his possible hiding spots are marked with flags,\n"
                    + "special task forces will take over to secure Fabear and take him to a lab "
                    + "where scientists will do everything they can to\n"
                    + "return Fabear to his original self.\n"
                    + "\nBy: Christopher Li and Aaron Lee - 2009-2010",
                "The Adventures of Fabear",
                JOptionPane.INFORMATION_MESSAGE);
          }
        });
    aboutMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK));

    helpMenu.add(instructionsMenuItem);
    helpMenu.add(aboutMenuItem);
    menuBar.add(helpMenu);
    setJMenuBar(menuBar);
  }