Question?

Ads
 

Question?

My question is how to:

Add a menu bar to the program with a File menu. In the File menu, add a submenu (JMenuItem) called About. When the user clicks on the About menu item, display a JOptionPane message dialog that contains your name

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class JLottery2 extends JFrame implements ItemListener
{
   final int NUM = 31;
   final int PICKS = 6;
   FlowLayout flow = new FlowLayout();
   JLabel greeting = new JLabel
      ("Play the lottery - Select " + PICKS + " numbers");
   Font serifBold = new Font("Serif", Font.BOLD, 20);
   JCheckBox[] choice = new JCheckBox[NUM];
   JLabel[] label = new JLabel[NUM]; 
   String winningsString = "The winning numbers are: ";
   String youPickedString = "You chose:   ";
   JTextArea outArea = new JTextArea("");
   int x, y;
   int[] ran = new int[PICKS];
   int[] chosen = new int[PICKS];
   int chosenCount = 0;
   int matches = 0;
   int[] winDollars = {0, 0, 0, 100, 10000, 50000, 1000000};
   public JLottery2()
   {
      super("Lottery Game");
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLayout(flow);
      add(greeting);
      greeting.setFont(serifBold);
      for(x = 0; x < NUM; ++x)
      {
          choice[x] = new JCheckBox();
          label[x] = new JLabel("       " + x);
          add(label[x]);
          add(choice[x]);
          choice[x].addItemListener(this);
      }

      add(outArea);

      for(x = 0; x < PICKS; ++x)
      {
          ran[x] = (int)Math.floor(Math.random()*NUM);
          for(y = 0; y < x; ++y)
               if(ran[x] == ran[y])
                  --x;
      }
      for(x = 0; x < PICKS; ++x)
      {
         winningsString += ran[x] + "   ";
      }
    // System.out.println(winningsString);
    // Useful while testing

   }
   public static void main(String[] arguments)
   {
      JLottery2 lotFrame = new JLottery2();
      lotFrame.setSize(380,300);
      lotFrame.setVisible(true);
   }
   public void itemStateChanged(ItemEvent check)
   {
      Object source = check.getItem();
      if(chosenCount == PICKS)
      {
          for(x= 0; x < NUM; ++x)
          {
               if(source == choice[x])
                  choice[x].setSelected(false);
          }
      }
      else
      {

          for(x= 0; x < NUM; ++x)
          {
               if(source == choice[x])
               {
          int select = check.getStateChange();
          if(select == ItemEvent.SELECTED)
          {
                      chosen[chosenCount] = x;
                      youPickedString += x + "   ";
                      ++chosenCount;
                  }    
               }
          }
          if(chosenCount == PICKS)
          {
                for(x = 0; x < PICKS; ++x)
                   for(y = 0; y < PICKS; ++y)
                        if(chosen[x] == ran[y])
                               ++matches; 
                outArea.append(winningsString);
                outArea.append("\n");
                outArea.append(youPickedString);
                outArea.append("\nYou matched " + matches + " numbers");
                outArea.append("\nYou win $" + winDollars[matches]); 
          }
      }
   }
}
View Answers

August 29, 2012 at 1:10 PM

Here is an example that prompt the user to enter the name which is to shown on menuitem selection.

import java.awt.*; 
import java.util.*;
import javax.swing.*; 
import java.awt.event.*;
public class JLottery2 extends JFrame implements ItemListener {
    final int NUM = 31;
    final int PICKS = 6;
    FlowLayout flow = new FlowLayout(); 
    JLabel greeting = new JLabel ("Play the lottery - Select " + PICKS + " numbers");
    Font serifBold = new Font("Serif", Font.BOLD, 20);
    JCheckBox[] choice = new JCheckBox[NUM];
    JLabel[] label = new JLabel[NUM];
    String winningsString = "The winning numbers are: ";
    String youPickedString = "You chose: ";
    JTextArea outArea = new JTextArea("");
    int x, y;
    int[] ran = new int[PICKS]; 
    int[] chosen = new int[PICKS];
    int chosenCount = 0;
    int matches = 0;
    int[] winDollars = {0, 0, 0, 100, 10000, 50000, 1000000};
    public JLottery2(final String str) {
        super("Lottery Game"); 
        JMenuBar menubar = new JMenuBar();
        JMenu filemenu = new JMenu("File");
        JMenuItem fileItem = new JMenuItem("About");
        fileItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "Welcome "+str);

            }

        });
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(flow); add(greeting); 
        greeting.setFont(serifBold);
        for(x = 0; x < NUM; ++x) {
            choice[x] = new JCheckBox();
            label[x] = new JLabel(" " + x); 
            add(label[x]);
            add(choice[x]); 
            choice[x].addItemListener(this);
            }
            filemenu.add(fileItem);
  menubar.add(filemenu);
  setJMenuBar(menubar);
  add(outArea);

  for(x = 0; x < PICKS; ++x)
  {
      ran[x] = (int)Math.floor(Math.random()*NUM);
      for(y = 0; y < x; ++y)
           if(ran[x] == ran[y])
              --x;
  }
  for(x = 0; x < PICKS; ++x)
  {
     winningsString += ran[x] + "   ";
  }

} public static void main(String[] arguments) {
    Scanner input=new Scanner(System.in);
    System.out.print("Enter Name: ");
    String str=input.nextLine();
    JLottery2 lotFrame = new JLottery2(str);
    lotFrame.setSize(380,300);
    lotFrame.setVisible(true); 
    } public void itemStateChanged(ItemEvent check) {
        Object source = check.getItem(); 
        if(chosenCount == PICKS) {
            for(x= 0; x < NUM; ++x) {
                if(source == choice[x]) 
                    choice[x].setSelected(false);
                } 
                }
                else {

      for(x= 0; x < NUM; ++x)
      {
           if(source == choice[x])
           {
      int select = check.getStateChange();
      if(select == ItemEvent.SELECTED)
      {
                  chosen[chosenCount] = x;
                  youPickedString += x + "   ";
                  ++chosenCount;
              }    
           }
      }
      if(chosenCount == PICKS)
      {
            for(x = 0; x < PICKS; ++x)
               for(y = 0; y < PICKS; ++y)
                    if(chosen[x] == ran[y])
                           ++matches; 
            outArea.append(winningsString);
            outArea.append("\n");
            outArea.append(youPickedString);
            outArea.append("\nYou matched " + matches + " numbers");
            outArea.append("\nYou win $" + winDollars[matches]); 
      }
  }

} 
}

Ads









Related Tutorials/Questions & Answers:
Core Java Interview Questions Page 2, Core Java QuestionQ
servlet questions
Advertisements
beginners questions
programming questions
questions
questions
questions
questions
questions
questions
questions
questions
questions
questions
Related Questions
javascript questions on date
Hi - Hibernate Interview Questions
multiple choice questions program
multiple choice questions program
Java Interview Questions
intervw questions for an Android
Example questions of JSP & Servlet
About interview questions
multiple choice questions
multiple choice questions
Interview Questions - What is JRE?
Questions about Java's String pool
questions from an xml file
Interview Questions - What is JDK?
Interview Questions - What is Java?
Hibernate interview Questions

Ads