trying to figure out how to make the menus work except exit and help

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 807 Views - Last Post: 03 December 2017 - 10:47 PM Rate Topic: -----

#1 Mike1874   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 01-December 17

trying to figure out how to make the menus work except exit and help

Posted 01 December 2017 - 11:56 PM

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.JOptionPane;

public class Menu extends JFrame
{
   private JLabel messageLabel; //Displays a message

   private final int LABEL_WIDTH = 200; //Label's width

   private final int LABEL_HEIGHT = 200; //Label's height

   private JMenuBar menuBar; //The menu bar

   private JMenu arrayMenu; //The Array menu

   private JMenu helpMenu; //The help menu

   private JMenuItem createItem; //To create

   private JMenuItem exitItem; //To exit

   private JMenuItem sortItem; //To sort

   private JMenuItem searchItem; //To search

   private JMenuItem displayItem; //To display

   private JMenuItem aboutItem; //To explain program

   RandomArray ra = new RandomArray();

    /**
        Constructor
    */

   public Menu()
   
   {
       //set the title
   
      setTitle("Array Menu");
   
      //Specify an action for the close button
   
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   
      //Create the messageLabel label.
   
      messageLabel = new JLabel("Click the 'Array' menu to begin", SwingConstants.CENTER);
   
      //Set the labels size
   
      messageLabel.setPreferredSize( new Dimension(LABEL_WIDTH, LABEL_HEIGHT));
   
      //Add the label to content pane
   
      add(messageLabel);
   
      //Build the menu bar.
   
      buildMenuBar();
   
      //Pack and display
   
      pack();
   
      setVisible(true);
   
   }

    /**
      The buildMenuBar builds the menu bar
    */

   public void buildMenuBar()
   
   {
        //Create the menu bar
   
      menuBar = new JMenuBar();
   
        //Create the Array menu and Help menus.
   
      buildArrayMenu();
   
      buildHelpMenu();
   
        //Add the Array and Help menus to the menu bar.
   
      menuBar.add(arrayMenu);
   
      menuBar.add(helpMenu);
   
    //Set the windows menu bar
   
      setJMenuBar(menuBar);
   
   }

   private void buildArrayMenu()
   
   {
   
    //Create a createMenu item. Add action listener
      createItem = new JMenu("Create Array");
   
      createItem.setMnemonic(KeyEvent.VK_C);
   
      createItem.addActionListener(new ArrayListener());
   
    //Create a sort item. Add action listener
   
      sortItem = new JMenu("Sort");
   
      sortItem.setMnemonic(KeyEvent.VK_S);
   
      sortItem.addActionListener(new ArrayListener());
   
    //Create a searchMenu item. Add action listener
      searchItem = new JMenuItem("Binary Search");
   
      searchItem.setMnemonic(KeyEvent.VK_B)/>/>;
   
      searchItem.addActionListener(new ArrayListener());
   
    //Create a displayMenu item. Add action listener
   
      displayItem = new JMenu("Display content");
   
      displayItem.setMnemonic(KeyEvent.VK_D);
   
      displayItem.addActionListener(new ArrayListener());
   
    //Create a exitMenu item. Add action listener
   
      exitItem = new JMenuItem("Exit");
   
      exitItem.setMnemonic(KeyEvent.VK_X);
   
      exitItem.addActionListener(new ExitListener());
   
    //Create a JMenu object for the Array menu
   
      arrayMenu = new JMenu("Array");
   
      arrayMenu.setMnemonic(KeyEvent.VK_R);
   
    //Add the menu items to the Array menu
   
      arrayMenu.add(createItem);
   
      arrayMenu.add(sortItem);
   
      arrayMenu.add(searchItem);
   
      arrayMenu.add(displayItem);
   
      arrayMenu.add(exitItem);
   
   }

     /**
     The buildHelpMenu builds the help menu
     */

   private void buildHelpMenu()
   
   {
   //Create about menu item
   
      aboutItem = new JMenuItem("About");
   
      aboutItem.setMnemonic(KeyEvent.VK_A);
   
      aboutItem.addActionListener(new HelpListener());
   
   //Create a JMenu objector for the Help menu
   
      helpMenu = new JMenu("Help");
   
      helpMenu.setMnemonic(KeyEvent.VK_H);
   
   //Add the about menu item to the Help menu
   
      helpMenu.add(aboutItem);
   
   }

/**
Private inner class that handles the event that is generated when the user selects

Exit from the array menu.
*/

   private class ExitListener implements ActionListener
   
   {
   
      public void actionPerformed(ActionEvent e)
      
      {
      
         System.exit(0);
      
      }
   
   }

/**
Private inner class that handles the event that is generated when the user selects 

a choice from the array menu
*/

   private class ArrayListener implements ActionListener
   
   {
   
      public void actionPerformed(ActionEvent e)
      
      {
      
         if (createItem.isSelected())
         
            ra.createArray();
         
         else if (sortItem.isSelected()) 
         
            ra.sortArray();
         
         else if (searchItem.isSelected())
         
            ra.binarySearch();
         
         else if (displayItem.isSelected())
         
            ra.displayArray();
      
      
      }
   
   }
/**
Private inner class that handles the event that is generated when the user selects a choice form the help menu.
*/

   private class HelpListener implements ActionListener
   
   {
   
      public void actionPerformed(ActionEvent e)
      {
      
         JOptionPane.showMessageDialog(null, "This program will fill an array the size of 100 with random"
            + " numbers 1-100. To begin the process first click the Array menu. It is important that the "
            
            + "steps be executed in the correct order. \n\n1. 'Create Array' is the first step. This will "
            
            + "create and fill the array. \n\n2. 'Sort' is the second step. This will will organize the "
            
            + "elements in the array in ascending order. \n\n3. 'Binary Search' is the next step. This "
            
            + "allows you to enter a number that will then be search for in the array. The numbers "
            
            + "position will then be given if it is found. \n\n4. 'Display Content' is the final step "
            
            + "in this program. This option will display the content in the array.");
      
      }
   
   }

/**
       The main method creates an instance of the Menu classs, which displays its window
*/


   public static void main(String[] args)
   
   {
      Menu me = new Menu();
   
   }
}

This post has been edited by ndc85430: 02 December 2017 - 12:45 AM
Reason for edit:: Added code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: trying to figure out how to make the menus work except exit and help

#2 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 849
  • View blog
  • Posts: 3,407
  • Joined: 13-June 14

Re: trying to figure out how to make the menus work except exit and help

Posted 02 December 2017 - 12:47 AM

You need to explain exactly what problem you're having, what you've tried and post any error messages in their entirety. Also, in future, please post code within "[CODE]" tags as you're asked in the box where you type your posts. I've done it for you this time, but you can use the "[CODE]" button in the editor to insert them for you.
Was This Post Helpful? 0
  • +
  • -

#3 Mike1874   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 01-December 17

Re: trying to figure out how to make the menus work except exit and help

Posted 02 December 2017 - 01:03 AM

Its hard to explain the code does not give me any error messages when I try to compile or run it, when it is finished running it will display the java menu and when I try to click on the tab menu nothing is displayed so I tried changing the exit menu to [ exitItem = new JMenuItem("Exit");] and it took away the arrow and made the code run so now I can exit. ive tried changing the other menus but it still wont let me click on any of the arrows.

the line I changed for the exit menu was 141

This post has been edited by ndc85430: 02 December 2017 - 02:21 AM
Reason for edit:: Removed quote of previous post.

Was This Post Helpful? 0
  • +
  • -

#4 Mike1874   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 01-December 17

Re: trying to figure out how to make the menus work except exit and help

Posted 02 December 2017 - 01:17 AM

this is the first part to making the code run if you want to check what I mean when it doesn't display an error message or lets me click on anything except about and exit which I fixed.

[/import] java.util.Random;

[/import] javax.swing.JOptionPane;

[/import] java.util.List;

[import] java.util.ArrayList;

[import] java.util.Arrays;

[import] java.util.Scanner;

[public class] RandomArray

{

[private final int] NUM_ELEMENTS = 5;[///Size of the array]

[private int][] numbers = [new] [int][NUM_ELEMENTS];[//Array of numbers]

Scanner keyboard = [new] Scanner(System.in);

/**

The createArray method fill the array with random number to 100.

*/


[public void] createArray()

{

//Creates a Random object

Random rand = [new] Random();


//Fills an array the size of 100 with random numbers 1-100


[for] ([int] i = 0; i < numbers.length; i++)
{

[int] n = rand.nextInt(NUM_ELEMENTS);

numbers[i] = n;
}

JOptionPane.showMessageDialog [(null, "Create and filling array.")];
}

/**

The sortArray method sorts all the numbers in ascending order

*/

[public void] sortArray()

{

[int] lastPos;//Marks the last position to compare

[int] temp;//Used to swap to elements


[for]( lastPos = numbers.length - 1; lastPos >= 0; lastPos--)
{

//The inner loop steps through the array, comparing each element to

//compare during each pass through the array. Initially lastPos is the

//index of the last element in the array. During each iteration, it is

//decreased by one.


[for] ([int] i = 0; i <= lastPos - 1; i++)

{

//Compare an element with its neighbors


[if](numbers[i] > numbers[i + 1])

{

temp = numbers[i];

numbers[i] = numbers[i + 1];

numbers[i + 1] = temp;

}

}

}

JOptionPane.showMessageDialog([null], ["Sorting in ascending order."]);

//Displays array in ascending order.

System.out.println(["Sorted Order"]);

System.out.println(["*************"]);


[for] ([int] i = 0; i < numbers.length; i++)

{

System.out.println(["Position "] + (i+1) + [":"] + numbers[i]);

}

}

/**

The binarySearch method performs a binary search on the array. The array is

searched for the number input by user.

*/


[public void] binarySearch()
{

String input;

[int] counter;//Counter variable     

[int] item;//Number input by user

[int] first;//First array element

[int] last;//Last array element

[int] middle;//Mid point of search

//Get the number.

input = JOptionPane.showInputDialog(["Enter the search value:"]);

item = Integer.parseInt(input);

//Set the initial values

first = 0;

last = NUM_ELEMENTS - 1;

middle = (first + last)/2;//Midpoint

//Search for the value   


[while]( first <= last )

{


[if] ( numbers[middle] < item )

first = middle + 1;


[else if] ( numbers[middle] == item )

{

JOptionPane.showMessageDialog([null], item + " [found at location] " + (middle + 1) + ["."]);


[break];

}


[else]

{

last = middle - 1;

}

middle = (first + last)/2;

}


[if] ( first > last )

JOptionPane.showMessageDialog([null],item + " [is not found.\n]");
}

/**

The display method displays the contents of the array

*/


[public void] displayArray()

{

System.out.println("[\nOriginal Order]");

System.out.println("[****************]");

//Displays the original order of contents


[for] ([int] i = 0; i < numbers.length; i++)
{

System.out.println("[Position] " + (i+1) + " [:] " + numbers[i]);

}

}

}
Was This Post Helpful? 0
  • +
  • -

#5 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 849
  • View blog
  • Posts: 3,407
  • Joined: 13-June 14

Re: trying to figure out how to make the menus work except exit and help

Posted 02 December 2017 - 01:32 AM

Please learn how to use code tags properly. You just need to press the button in the editor to insert the tags and then put your code between them.
Was This Post Helpful? 0
  • +
  • -

#6 Mike1874   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 01-December 17

Re: trying to figure out how to make the menus work except exit and help

Posted 02 December 2017 - 01:35 AM

Sorry still getting used to this site don't know how to edit it to fix, where do i go?

This post has been edited by ndc85430: 02 December 2017 - 01:38 AM
Reason for edit:: Removed quote of previous post. Just press "Reply" in future.

Was This Post Helpful? 0
  • +
  • -

#7 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 849
  • View blog
  • Posts: 3,407
  • Joined: 13-June 14

Re: trying to figure out how to make the menus work except exit and help

Posted 02 December 2017 - 01:40 AM

You need to have at least 16 posts to edit a post (see the FAQ). For now, Just post the code again, in correct code tags.

This post has been edited by ndc85430: 02 December 2017 - 01:40 AM

Was This Post Helpful? 0
  • +
  • -

#8 Mike1874   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 01-December 17

Re: trying to figure out how to make the menus work except exit and help

Posted 02 December 2017 - 02:11 AM

import
java.util.Random;

import
javax.swing.JOptionPane;

import 
java.util.List;

import
java.util.ArrayList;

import
java.util.Arrays;

import
java.util.Scanner;

public class 
RandomArray

{

private final int
NUM_ELEMENTS = 5;
//Size of the array

   
private int
[] numbers =
new
int
[NUM_ELEMENTS];
//Array of numbers

   
Scanner keyboard =
new 
Scanner(System.in);

/**

The createArray method fill the array with random number to 100.

*/



 public void 
createArray()

{

//Creates a Random object


Random rand =
new
Random();


//Fills an array the size of 100 with random numbers 1-100



for
(
int
i = 0; i < numbers.length; i++)
{

int
n = rand.nextInt(NUM_ELEMENTS);

numbers[i] = n;
}

JOptionPane.showMessageDialog(
null
, "Create and filling array.");
}

/**

The sortArray method sorts all the numbers in ascending order

*/


public void 
sortArray()

{

 int 
lastPos;
//Marks the last position to compare
   
      
int
temp;
//Used to swap to elements



for
( lastPos = numbers.length - 1; lastPos >= 0; lastPos--)
{
      //The inner loop steps through the array, comparing each element to
      
      //compare during each pass through the array. Initially lastPos is the
      
      //index of the last element in the array. During each iteration, it is
      
      //decreased by one.



 for
(
int
i = 0; i <= lastPos - 1; i++)

{

//Compare an element with its neighbors



if
(numbers[i] > numbers[i + 1])

{

temp = numbers[i];

numbers[i] = numbers[i + 1];

numbers[i + 1] = temp;

}

}

}

JOptionPane.showMessageDialog(
null
, "
Sorting in ascending order.
");

//Displays array in ascending order.


System.out.println("
Sorted Order
");

System.out.println("
*************
");


for
(
int
i = 0; i < numbers.length; i++)

{

System.out.println("
Position
" + (i+1) + "
:
" + numbers[i]);

}

}

/**

The binarySearch method performs a binary search on the array. The array is 

searched for the number input by user.

*/



public void
binarySearch()
{

String input;

int
counter;//Counter variable     

 int
item;//Number input by user

int 
first;//First array element

int
last;//Last array element

 int
middle;//Mid point of search

//Get the number.


input = JOptionPane.showInputDialog("
Enter the search value
:");

item = Integer.parseInt(input);

//Set the initial values


first = 0;

last = NUM_ELEMENTS - 1;

middle = (first + last)/2;//Midpoint

//Search for the value 
  


while
( first <= last )

{


if
( numbers[middle] < item )

first = middle + 1;


else if 
( numbers[middle] == item )

{

JOptionPane.showMessageDialog(
null
, item + "
 found at location 
" + (middle + 1) + ".");


break
;

}


 else


{

last = middle - 1;

}

middle = (first + last)/2;

}


 if
( first > last )

JOptionPane.showMessageDialog(null,item + "
is not found.\n
");
}

/**

The display method displays the contents of the array

*/



public void 
displayArray()

{

System.out.println("
\nOriginal Order
");

System.out.println("
****************
");

//Displays the original order of contents



for
(
int 
i = 0; i < numbers.length; i++)
{

System.out.println("
Position 
" + (i+1) + "
:
" + numbers[i]);

}

}

}

is there a delete option?
Was This Post Helpful? 0
  • +
  • -

#9 Mike1874   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 01-December 17

Re: trying to figure out how to make the menus work except exit and help

Posted 02 December 2017 - 02:17 AM

import java.util.Random;

import javax.swing.JOptionPane;

import java.util.List;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Scanner;

public class RandomArray

{

   private final int NUM_ELEMENTS = 5;//Size of the array

   private int[] numbers = new int[NUM_ELEMENTS];//Array of numbers

   Scanner keyboard = new Scanner(System.in);

/**

The createArray method fill the array with random number to 100.

*/


   public void createArray()
   
   {
   
   //Creates a Random object
   
      Random rand = new Random();
   
   
   //Fills an array the size of 100 with random numbers 1-100
   
   
      for (int i = 0; i < numbers.length; i++)          
      {
      
         int n = rand.nextInt(NUM_ELEMENTS);
      
         numbers[i] = n;         
      }
   
      JOptionPane.showMessageDialog(null, "Create and filling array.");
   }

/**

The sortArray method sorts all the numbers in ascending order

*/

   public void sortArray()
   
   {
   
      int lastPos;//Marks the last position to compare
   
      int temp;//Used to swap to elements
   
   
      for( lastPos = numbers.length - 1; lastPos >= 0; lastPos--)
      {
      
      //The inner loop steps through the array, comparing each element to
      
      //compare during each pass through the array. Initially lastPos is the
      
      //index of the last element in the array. During each iteration, it is
      
      //decreased by one.
      
      
         for (int i = 0; i <= lastPos - 1; i++)
         
         {
         
         //Compare an element with its neighbors
         
         
            if(numbers[i] > numbers[i + 1])
            
            {
            
               temp = numbers[i];
            
               numbers[i] = numbers[i + 1];
            
               numbers[i + 1] = temp;
            
            }
         
         }
      
      }
   
      JOptionPane.showMessageDialog(null, "Sorting in ascending order.");
   
   //Displays array in ascending order.
   
      System.out.println("Sorted Order");
   
      System.out.println("*************");
   
   
      for (int i = 0; i < numbers.length; i++)
      
      {
      
         System.out.println("Position " + (i+1) + ":" + numbers[i]);
      
      }
   
   }

/**

The binarySearch method performs a binary search on the array. The array is 

searched for the number input by user.

*/


   public void binarySearch()
   {
   
      String input;
   
      int counter;//Counter variable      
   
      int item;//Number input by user
   
      int first;//First array element
   
      int last;//Last array element
   
      int middle;//Mid point of search
   
   //Get the number.
   
      input = JOptionPane.showInputDialog("Enter the search value:");
   
      item = Integer.parseInt(input);
   
   //Set the initial values
   
      first = 0;
   
      last = NUM_ELEMENTS - 1;
   
      middle = (first + last)/2;//Midpoint
   
   //Search for the value    
   
   
      while( first <= last )
      
      {
      
      
         if ( numbers[middle] < item )
         
            first = middle + 1;
         
         
         else if ( numbers[middle] == item )
         
         {
         
            JOptionPane.showMessageDialog(null, item + " found at location " + (middle + 1) + ".");
         
         
            break;
         
         }
         
         
         else
         
         {
         
            last = middle - 1;
         
         }
      
         middle = (first + last)/2;
      
      }
   
   
      if ( first > last )
      
         JOptionPane.showMessageDialog(null,item + " is not found.\n");
   }

/**

The display method displays the contents of the array

*/


   public void displayArray()
   
   {
   
      System.out.println("\nOriginal Order");
   
      System.out.println("****************");
   
   //Displays the original order of contents
   
   
      for (int i = 0; i < numbers.length; i++) 
      {
      
         System.out.println("Position " + (i+1) + " : " + numbers[i]);
      
      }
   
   }

}

Was This Post Helpful? 0
  • +
  • -

#10 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 849
  • View blog
  • Posts: 3,407
  • Joined: 13-June 14

Re: trying to figure out how to make the menus work except exit and help

Posted 02 December 2017 - 02:18 AM

Ok, I didn't really understand the problem you're having, possibly because I don't know Swing that well. Also, how does this new code relate to the problem?

This post has been edited by ndc85430: 02 December 2017 - 02:24 AM

Was This Post Helpful? 0
  • +
  • -

#11 Mike1874   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 01-December 17

Re: trying to figure out how to make the menus work except exit and help

Posted 02 December 2017 - 02:23 AM

View Postndc85430, on 02 December 2017 - 02:18 AM, said:

Sigh. It really isn't that hard. Press the "[CODE]" button to insert the tags in your post. Then, paste your code between the two tags.


fixed it right this time
Was This Post Helpful? 0
  • +
  • -

#12 Mike1874   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 01-December 17

Re: trying to figure out how to make the menus work except exit and help

Posted 02 December 2017 - 02:33 AM

the new code is the GUI which you need to run the first code I posted, without it you get error message "cannot find symbol" if you run the first code I posted, and the problem is when you run the first code it will display the array menu but you try to click on the submenu nothing happens it is supposed to display a list of numbers, sort them, and display them but it doesn't do any of that.

This post has been edited by ndc85430: 02 December 2017 - 02:37 AM
Reason for edit:: Removed quote of previous post.

Was This Post Helpful? 0
  • +
  • -

#13 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 849
  • View blog
  • Posts: 3,407
  • Joined: 13-June 14

Re: trying to figure out how to make the menus work except exit and help

Posted 02 December 2017 - 02:37 AM

So, your program doesn't compile. I did ask you to post error messages in their entirety. The error means that you're trying a variable/class/method that hasn't been declared. Which is it and why haven't you declared it?

Also, please stop quoting the previous post. There's no need to; just use the "Reply" button.
Was This Post Helpful? 0
  • +
  • -

#14 Mike1874   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 01-December 17

Re: trying to figure out how to make the menus work except exit and help

Posted 02 December 2017 - 02:43 AM

it does compile without error messages I just said that cause I thought you tried running the first code, but I do not receive any errors it runs it fine but I cannot use the array menu to click through the subemenus, i cant explain it anymore, only by running it you can see what i mean
Was This Post Helpful? 0
  • +
  • -

#15 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3561
  • View blog
  • Posts: 16,232
  • Joined: 20-September 08

Re: trying to figure out how to make the menus work except exit and help

Posted 02 December 2017 - 04:25 AM

There aren't any menus in the last code you posted (which is completely different to the code you first posted)
You need to wake up ;)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2