Java JCheckBoxMenuItem class

Description

Java JCheckBoxMenuItem class

import java.awt.FlowLayout;
import java.awt.event.KeyEvent;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.SwingUtilities;

public class Main {
  public Main() {

    JFrame jfrm = new JFrame("Menu Demo");
    jfrm.setLayout(new FlowLayout());
    jfrm.setSize(220, 200);/*w  w w . j  ava  2s.  co  m*/
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar jmb = new JMenuBar();

    // Create the File menu.
    JMenu jmFile = new JMenu("File");
    JCheckBoxMenuItem m1 = new JCheckBoxMenuItem("A");
    m1.setSelected(true);

    JCheckBoxMenuItem m2 = new JCheckBoxMenuItem("B");

    jmFile.add(m1);
    jmFile.add(m2);

    jmb.add(jmFile);

    jfrm.setJMenuBar(jmb);

    jfrm.setVisible(true);
  }

  public static void main(String args[]) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        new Main();
      }
    });
  }
}
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;

public class Main implements ActionListener {
   public Main() {
      // Create a new JFrame container.
      JFrame jfrm = new JFrame("Menu Demo");

      // Specify FlowLayout for the layout manager.
      jfrm.setLayout(new FlowLayout());

      // Give the frame an initial size.
      jfrm.setSize(220, 200);//  w w w.  ja v  a 2  s . c  o  m

      // Terminate the program when the user closes the application.
      jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      // Create the menu bar.
      JMenuBar jmb = new JMenuBar();

      // Create the File menu.
      JMenu jmFile = new JMenu("File");
      jmFile.setMnemonic(KeyEvent.VK_F);

       
      // Create the Colors submenu. 
      JMenu jmColors = new JMenu("Colors"); 
       
      // Use check boxes for colors. This allows 
      // the user to select more than one color. 
      JCheckBoxMenuItem jmiRed = new JCheckBoxMenuItem("Red"); 
      JCheckBoxMenuItem jmiGreen = new JCheckBoxMenuItem("Green"); 
      JCheckBoxMenuItem jmiBlue = new JCheckBoxMenuItem("Blue"); 
       
      jmColors.add(jmiRed); 
      jmColors.add(jmiGreen); 
      jmColors.add(jmiBlue); 
      jmFile.add(jmColors); 
      jmFile.add(jmColors);
      

      // Create the Help menu.
      JMenu jmHelp = new JMenu("Help");
      JMenuItem jmiAbout = new JMenuItem("About");
      jmHelp.add(jmiAbout);
      jmb.add(jmFile);
      jmb.add(jmHelp);


      jmiAbout.addActionListener(this);

      // Add the menu bar to the frame.
      jfrm.setJMenuBar(jmb);

      // Display the frame.
      jfrm.setVisible(true);
   }

   // Handle menu item action events.
   public void actionPerformed(ActionEvent ae) {
      // Get the action command from the menu selection.
      String comStr = ae.getActionCommand();

      // If user chooses Exit, then exit the program.
      if (comStr.equals("Exit"))
         System.exit(0);

      // Otherwise, display the selection.
      System.out.println(comStr + " Selected");
   }

   public static void main(String args[]) {
      // Create the frame on the event dispatching thread.
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            new Main();
         }
      });
   }
}



PreviousNext

Related