Perdorimi i JCheckBoxMenuItem dhe JRadioButtonMenuItem

 •        Menu

–    Nje liste me opsione te perdoruesit

•        Klasat:

–    JMenuBar

–    JMenu

–    JMenuItem

•        Metoda setJMenuBar()

–    Shton nje JMenuBar tek nje JFrame

•        Metoda add()

–    Shton nje JMenu tek JMenuBar  

Shembull:


     import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
public class JMenuFrame extends JFrame implements
   ActionListener
{
   private JMenuBar mainBar = new JMenuBar();
   private JMenu menu1 = new JMenu("File");
   private JMenu menu2 = new JMenu("Colors");
   private JMenuItem exit = new JMenuItem("Exit");
   private JMenu bright = new JMenu("Bright");
   private JMenuItem dark = new JMenuItem("Dark");
   private JMenuItem white = new JMenuItem("White");
   private JMenuItem pink = new JMenuItem("Pink");
   private JMenuItem yellow = new JMenuItem("Yellow");
   private JLabel label = new JLabel("Hello");
   public JMenuFrame()
   {
      setTitle("Menu Demonstration");
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLayout(new FlowLayout());
      setJMenuBar(mainBar);
      mainBar.add(menu1);
      mainBar.add(menu2);
      menu1.add(exit);
      menu2.add(bright);
      menu2.add(dark);
      menu2.add(white);
      bright.add(pink);
      bright.add(yellow);
      exit.addActionListener(this);
      dark.addActionListener(this);
      white.addActionListener(this);
      pink.addActionListener(this);
      yellow.addActionListener(this);
      add(label);
      label.setFont(new Font("Arial", Font.BOLD, 26));
   }
   public void actionPerformed(ActionEvent e)
   {
      Object source = e.getSource();
      Container con = getContentPane();
      if(source == exit)
         System.exit(0);
      else if(source == dark)
         con.setBackground(Color.BLACK);
      else if(source == white)
         con.setBackground(Color.WHITE);
      else if(source == pink)
         con.setBackground(Color.PINK);
      else con.setBackground(Color.YELLOW);
   }

   public static void main(String[] args)
   {
      JMenuFrame mFrame = new JMenuFrame();
      final int WIDTH = 250;
      final int HEIGHT = 200;
      mFrame.setSize(WIDTH, HEIGHT);
      mFrame.setVisible(true);
   }
}

 Perdorimi i objekteve JCheckBoxMenuItem dhe JRadioButtonMenuItem

•        Objektet JCheckBoxMenuItem shfaqen me nje check box anash tyre

•        Objektet JRadioButtonMenuItem shfaqen me nje radio button anash tyre

•        Metoda isSelected()

–    Percakton gjendjen e nje JCheckBoxMenuItem ose JRadioButtonMenuItem

Perdorimi i addSeparator()

•        Shton nje vije horizontale ne menu per te ndare grupet e njesive te menuse per perdoruesin

•        Nuk ndryshon funksionalitetin e menu-se Perdorimi i setMnemonic()

•        Mnemonic

–    Nje buton i tastieres qe shfaq nje njesi ne menune e hapur

•        metoda setMnemonic()

–    Siguron nje shortcut key per te zgjedhur nje njesi te menuse se hapur

–    Duhet te zgjidhni memonika te ndryshme per njesite e menuse

•        Accelerator

–    Nje kombinim butonash qe shfaq nje njesi te menuse nese ajo eshte e hapur ose jo.

–    Vetem “leaf menu items” mund te kene accelerators  

Shembull:


     import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JMenuFrame3 extends JFrame
{
   private JMenuBar mainBar = new JMenuBar();
   private JMenu menu1 = new JMenu("File");
   private JCheckBoxMenuItem check1 = new
      JCheckBoxMenuItem("Check box A");
   private JCheckBoxMenuItem check2 = new
      JCheckBoxMenuItem("Check box B");
   private JRadioButtonMenuItem radio1 = new
      JRadioButtonMenuItem("Radio option 1");
   private JRadioButtonMenuItem radio2 = new
      JRadioButtonMenuItem("Radio option 2");
   private JRadioButtonMenuItem radio3 = new
      JRadioButtonMenuItem("Radio option 3");
   private ButtonGroup group = new ButtonGroup();

   public JMenuFrame3()
   {
      setTitle("Menu Demonstration");
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLayout(new FlowLayout());
      setJMenuBar(mainBar);
      menu1.setMnemonic('F');
      mainBar.add(menu1);

      menu1.add(check1);
      menu1.add(check2);
      menu1.addSeparator();
      menu1.add(radio1);
      menu1.add(radio2);
      menu1.add(radio3);
      group.add(radio1);
      group.add(radio2);
      group.add(radio3);

   }

   public static void main(String[] args)
   {
      JMenuFrame3 frame = new JMenuFrame3();
      final int WIDTH = 150;
      final int HEIGHT = 200;
      frame.setSize(WIDTH, HEIGHT);
      frame.setVisible(true);
   }
}