I am trying to create a menu GUI. I think I am having problems with the action listeners Any help would be great. Thanks

Write an GUI application that display a menu system. The menu system should allow the user to select one package, one phone, and any of the options desired. As the user selects items from the menu, the application should show the prices of the items selected.

(This GUI application uses AWT and SWING imports to display the menu system)

Question Details:

Cell Solutions, a cell phone provider, sells the following packages:
300 minutes per month: $45.00 per month
800 minutes per month: $65.00 per month
1500 minutes per month: $99.00 per month

The provider sells the following phones. ( A 6% sales tax applies to the sale of a phone.)

Model 100: $29.95
Model 110: $49.95
Model 200: $99.95

Customers may also select the following options:

Voice mail: $5.00 per month

Text messaging: $10.00 per month

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.SwingConstants;

public class MenuWindow extends JFrame
{

private JLabel messageLabel;			// to display a message
private final int LABEL_WIDTH = 400;		// to diplay labels width
private final int LABEL_HEIGHT = 200;		// to display labels height

private JMenuBar menuBar;
private JMenu packageMenu;
private JMenu phoneMenu;
private JMenu addonMenu;
private JRadioButtonMenuItem package1;
private JRadioButtonMenuItem package2;
private JRadioButtonMenuItem package3;
private JRadioButtonMenuItem phone1;
private JRadioButtonMenuItem phone2;
private JRadioButtonMenuItem phone3;
private JRadioButtonMenuItem vMail;
private JRadioButtonMenuItem textMessaging;

public MenuWindow()
{
super("Cell Solutions");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

messageLabel = new JLabel("Use Menu to select the plan and phone you wish to purchase, as well as any other add ons that you would like.",
SwingConstants.CENTER);

messageLabel.setPreferredSize(
new Dimension(LABEL_WIDTH, LABEL_HEIGHT));

messageLabel.setForeground(Color.BLACK);

add(messageLabel);

buildMenuBar();

pack();
setVisible(true);
}

private void buildMenuBar()
{
menuBar = new JMenuBar();

buildPackageMenu();
buildPhoneMenu();
buildAddonMenu();

menuBar.add(packageMenu);
menuBar.add(phoneMenu);
menuBar.add(addonMenu);

setJMenuBar(menuBar);
}

private void buildPackageMenu()
{
package1 = new JRadioButtonMenuItem("300 min/ month: $45", true);
package1.setMnemonic(KeyEvent.VK_X);
package1.addActionListener(new PackageListener());

package2 = new JRadioButtonMenuItem("800 min/ month: $65");
package2.setMnemonic(KeyEvent.VK_X);
package2.addActionListener(new PackageListener());

package3 = new JRadioButtonMenuItem("1500 min/ month: $99");
package3.setMnemonic(KeyEvent.VK_X);
package3.addActionListener(new PackageListener());

packageMenu = new JMenu("Package");
packageMenu.setMnemonic(KeyEvent.VK_F);


packageMenu.add(package1);
packageMenu.add(package2);
packageMenu.add(package3);
}

private void buildPhoneMenu()
{
phone1 = new JRadioButtonMenuItem("Model 100: $29.95", true);
phone1.setMnemonic(KeyEvent.VK_B);
phone1.addActionListener(new PhoneListener());

phone2 = new JRadioButtonMenuItem("Model 110: $49.95", true);
phone2.setMnemonic(KeyEvent.VK_B);
phone2.addActionListener(new PhoneListener());

phone3 = new JRadioButtonMenuItem("Model 200: $99.95", true);
phone3.setMnemonic(KeyEvent.VK_B);
phone3.addActionListener(new PhoneListener());

ButtonGroup group = new ButtonGroup();
group.add(phone1);
group.add(phone2);
group.add(phone3);

phoneMenu = new JMenu("Phone");
phoneMenu.setMnemonic(KeyEvent.VK_T);

phoneMenu.add(phone1);
phoneMenu.add(phone2);
phoneMenu.add(phone3);
}


private void buildAddonMenu()
{
vMail = new JRadioButtonMenuItem("Voice Mail: $5.00 / month", true);
vMail.setMnemonic(KeyEvent.VK_B);
vMail.addActionListener(new AddonListener());

textMessaging = new JRadioButtonMenuItem("Text Messaging: $10.00 / month", true);
textMessaging.setMnemonic(KeyEvent.VK_B);
textMessaging.addActionListener(new AddonListener());


ButtonGroup group = new ButtonGroup();
group.add(vMail);
group.add(textMessaging);


addonMenu = new JMenu("Add-Ons");
addonMenu.setMnemonic(KeyEvent.VK_T);

addonMenu.add(vMail);
addonMenu.add(textMessaging);
}
}


class PackageListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int packagePrice;
if (package1.isSelected())
packagePrice = 45;

else if (package2.isSelected())
packagePrice = 65;

else if (package3.isSelected())
packagePrice = 99;

}
}

class PhoneListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
        int phonePrice;
if (phone1.isSelected())
phonePrice = 45;

else if (phone2.isSelected())
phonePrice = 65;

else if (phone3.isSelected())
phonePrice = 99;
}
}

class AddonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
        int vMailPrice;
        
if (vMail.isSelected())
vMailPrice = 5;
        int textPrice;

if (textMessaging.isSelected())
textPrice = 10;

        int addonPrice = vMailPrice + textPrice;


}

public static void main(String[] args)
{
new MenuWindow();
}
}

Try to use the getSource() method prior to your isSelected if statement (within your actionlistener).

I would also make your price integers public.

if ((e.getSource()) == package1) {
if ((package1.isSelected()) {
...

Edited 5 Years Ago by StephNicolaou: n/a

i am new to java and im looking for a program that will let me build a GUI using java i know there are some out there but i cant find a download, i would like something that is like Visual basic were the end product is something you can see and buttons you can click

@olivia
Please start your own thread.

You probably mean NetBeans, with a GUI Builder. It's easy, but it doesnt teach you anything. You should look into AWT and Swing, and start building your own GUIs, it's not hard!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *  The MenuWindow class demonstrates a menu system.
 */

public class MenuWindow extends JFrame
{
   private JLabel messageLabel;            // To display a message
   private JLabel results;// to display result
   private final int LABEL_WIDTH = 400;    // The label's width
   private final int LABEL_HEIGHT = 200;   // The label's height

   // The following variables will reference menu components.
   private JMenuBar menuBar;   // The menu bar
   private JMenu fileMenu;     // The File menu
   private JMenu textMenu;     // The Text menu
   private JMenu cellMenu;     // The phone  menu
   private JMenu addMenu;     // The add on  menu
   private JMenuItem exitItem; // An item to exit the application
   private JRadioButtonMenuItem blackItem; // To make the text black
   private JRadioButtonMenuItem redItem;   // To make the text red
   private JRadioButtonMenuItem blueItem;  // To make the text blue
   private JRadioButtonMenuItem blackItem1; // To make the text black
   private JRadioButtonMenuItem redItem1;   // To make the text red
   private JRadioButtonMenuItem blueItem1;  // To make the text blue

   private JCheckBoxMenuItem voiceItem;  // To toggle visibility
   private JCheckBoxMenuItem textItem;  // To toggle visibility

   /**
    *  Constructor
    */

   public MenuWindow()
   {
      // Call the JFrame constructor.
      super("Cell Phone Package");

      // Specify an action for the close button.
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      // Create the message label and set its size and color.
      messageLabel = new JLabel("Use the  menu to " +
                 "select the best plan for you.",
                 SwingConstants.CENTER);
      messageLabel.setPreferredSize(
                 new Dimension(LABEL_WIDTH, LABEL_HEIGHT));
      messageLabel.setForeground(Color.BLACK);

      // Add the label to the content pane.
      add(messageLabel);



      // Build the menu bar.
      buildMenuBar();

      // Pack and display the window.
      pack();
      setVisible(true);


   }

   /**
    *  The buildMenuBar method builds the menu bar.
    */

   private void buildMenuBar()
   {
      // Create the menu bar.
      menuBar = new JMenuBar();

      // Create the file and text menus.
      buildFileMenu();
      buildTextMenu();
      buildAddMenu();
      buildCellMenu();

      // Add the file and text menus to the menu bar.
      menuBar.add(fileMenu);
      menuBar.add(textMenu);
      menuBar.add(addMenu);
      menuBar.add(cellMenu);


      // Set the window's menu bar.
      setJMenuBar(menuBar);
   }

   /**
    *  The buildFileMenu method builds the File menu
    *  and returns a reference to its JMenu object.
    */

   private void buildFileMenu()
   {
      // Create an Exit menu item.
      exitItem = new JMenuItem("Exit");
      exitItem.setMnemonic(KeyEvent.VK_X);
      exitItem.addActionListener(new ExitListener());

      // Create a JMenu object for the File menu.
      fileMenu = new JMenu("File");
      fileMenu.setMnemonic(KeyEvent.VK_F);

      // Add the Exit menu item to the File menu.
      fileMenu.add(exitItem);
   }

   /**
    * The buildTextMenu method builds the Text menu
    * and returns a reference to its JMenu object.
    */

   private void buildTextMenu()
   {
      // Create the radio button menu items to change the color
      // of the text. Add an action listener to each one.
      blackItem = new JRadioButtonMenuItem("300 minutes:$45 per month", true);
      blackItem.setMnemonic(KeyEvent.VK_B);
      blackItem.addActionListener(new PlanListener());

      redItem = new JRadioButtonMenuItem("800 minutes:$65 per month");
      redItem.setMnemonic(KeyEvent.VK_R);
      redItem.addActionListener(new PlanListener());

      blueItem = new JRadioButtonMenuItem("1500 minutes:$99 per month");
      blueItem.setMnemonic(KeyEvent.VK_U);
      blueItem.addActionListener(new PlanListener());

      // Create a button group for the radio button items.
      ButtonGroup group = new ButtonGroup();
      group.add(blackItem);
      group.add(redItem);
      group.add(blueItem);


      // Create a JMenu object for the Text menu.
      textMenu = new JMenu("Minutes");
      textMenu.setMnemonic(KeyEvent.VK_T);

      // Add the menu items to the Text menu.
      textMenu.add(blackItem);
      textMenu.add(redItem);
      textMenu.add(blueItem);

   }

    private void buildAddMenu()
      {
         // Create the radio button menu items to change the color
         // of the text. Add an action listener to each one.
         blackItem1 = new JRadioButtonMenuItem("Model 100:$29.95", true);
         blackItem1.setMnemonic(KeyEvent.VK_S);
         blackItem1.addActionListener(new PlanListener());

         redItem1 = new JRadioButtonMenuItem("Model 110:$49.95");
         redItem1.setMnemonic(KeyEvent.VK_K);
         redItem1.addActionListener(new PlanListener());

         blueItem1 = new JRadioButtonMenuItem("Model 200:$99.95");
         blueItem1.setMnemonic(KeyEvent.VK_P);
         blueItem1.addActionListener(new PlanListener());

         // Create a button group for the radio button items.
         ButtonGroup group = new ButtonGroup();
         group.add(blackItem1);
         group.add(redItem1);
         group.add(blueItem1);


         // Create a JMenu object for the Text menu.
         addMenu = new JMenu("Cell-Phone model");
         textMenu.setMnemonic(KeyEvent.VK_Z);

         // Add the menu items to the Text menu.
         addMenu.add(blackItem1);
         addMenu.add(redItem1);
         addMenu.add(blueItem1);

   }

    private void buildCellMenu()
	{
	  // Create a check box menu item to make the text
	  // visible or invisible.
	  voiceItem = new JCheckBoxMenuItem("Voice mail option", true);
	  voiceItem.setMnemonic(KeyEvent.VK_E);
      voiceItem.addActionListener(new PlanListener());

      // Create a check box menu item to make the text
	  // visible or invisible.
	  textItem = new JCheckBoxMenuItem("Text message option", true);
	  textItem.setMnemonic(KeyEvent.VK_F);
      textItem.addActionListener(new PlanListener());

      // Create a JMenu object for the Text menu.
	  cellMenu = new JMenu("Add ons");
	  cellMenu.setMnemonic(KeyEvent.VK_D);

	  // Add the menu items to the Text menu.
	  cellMenu.add(voiceItem);
	  cellMenu.add(textItem);

	}


   /**
    * Private inner class that handles the event that
    * is generated when the user selects Exit from
    * the File 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 color from
    * the Text menu.
    */

    private class PlanListener implements ActionListener
    {
      public void actionPerformed(ActionEvent e)
      {
		  double plan1;
		  double package1;
		  double text;
		  double message;
		  double addOn;
		  double total;

         // Determine which color was selected and
         // act accordingly.
          if (blackItem.isSelected()){
		       plan1=45;
		  else if (redItem.isSelected())
		       plan1=65;
		  else if (blueItem.isSelected())
		       plan1=99;
		  if (blackItem1.isSelected())
		       package1=29.95;
		  else if (redItem1.isSelected())
		 	   package1=49.95;
		  else if (blueItem1.isSelected())
		       package1=99.95;
		  if (voiceItem.isSelected())
			  message =5;
		  if(textItem.isSelected())
			   text=10;

			   addOn=message + text;
			   total= package1+(6/package1)*100+ plan1+ addOn;



      }
    }


   /**
    * The main method creates an instance of the MenuWindow
    * class, which causes it to display its window.
    */

   public static void main(String[] args)
   {
      new MenuWindow();
   }
}

I cant figure out what to do with PlanListner actionlistner. Any help?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *  The MenuWindow class demonstrates a menu system.
 */

public class MenuWindow extends JFrame
{
   private JLabel messageLabel;            // To display a message
   private JLabel results;// to display result
   private final int LABEL_WIDTH = 400;    // The label's width
   private final int LABEL_HEIGHT = 200;   // The label's height

   // The following variables will reference menu components.
   private JMenuBar menuBar;   // The menu bar
   private JMenu fileMenu;     // The File menu
   private JMenu textMenu;     // The Text menu
   private JMenu cellMenu;     // The phone  menu
   private JMenu addMenu;     // The add on  menu
   private JMenuItem exitItem; // An item to exit the application
   private JRadioButtonMenuItem blackItem; // To make the text black
   private JRadioButtonMenuItem redItem;   // To make the text red
   private JRadioButtonMenuItem blueItem;  // To make the text blue
   private JRadioButtonMenuItem blackItem1; // To make the text black
   private JRadioButtonMenuItem redItem1;   // To make the text red
   private JRadioButtonMenuItem blueItem1;  // To make the text blue

   private JCheckBoxMenuItem voiceItem;  // To toggle visibility
   private JCheckBoxMenuItem textItem;  // To toggle visibility

   /**
    *  Constructor
    */

   public MenuWindow()
   {
      // Call the JFrame constructor.
      super("Cell Phone Package");

      // Specify an action for the close button.
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      // Create the message label and set its size and color.
      messageLabel = new JLabel("Use the  menu to " +
                 "select the best plan for you.",
                 SwingConstants.CENTER);
      messageLabel.setPreferredSize(
                 new Dimension(LABEL_WIDTH, LABEL_HEIGHT));
      messageLabel.setForeground(Color.BLACK);

      // Add the label to the content pane.
      add(messageLabel);



      // Build the menu bar.
      buildMenuBar();

      // Pack and display the window.
      pack();
      setVisible(true);


   }

   /**
    *  The buildMenuBar method builds the menu bar.
    */

   private void buildMenuBar()
   {
      // Create the menu bar.
      menuBar = new JMenuBar();

      // Create the file and text menus.
      buildFileMenu();
      buildTextMenu();
      buildAddMenu();
      buildCellMenu();

      // Add the file and text menus to the menu bar.
      menuBar.add(fileMenu);
      menuBar.add(textMenu);
      menuBar.add(addMenu);
      menuBar.add(cellMenu);


      // Set the window's menu bar.
      setJMenuBar(menuBar);
   }

   /**
    *  The buildFileMenu method builds the File menu
    *  and returns a reference to its JMenu object.
    */

   private void buildFileMenu()
   {
      // Create an Exit menu item.
      exitItem = new JMenuItem("Exit");
      exitItem.setMnemonic(KeyEvent.VK_X);
      exitItem.addActionListener(new ExitListener());

      // Create a JMenu object for the File menu.
      fileMenu = new JMenu("File");
      fileMenu.setMnemonic(KeyEvent.VK_F);

      // Add the Exit menu item to the File menu.
      fileMenu.add(exitItem);
   }

   /**
    * The buildTextMenu method builds the Text menu
    * and returns a reference to its JMenu object.
    */

   private void buildTextMenu()
   {
      // Create the radio button menu items to change the color
      // of the text. Add an action listener to each one.
      blackItem = new JRadioButtonMenuItem("300 minutes:$45 per month", true);
      blackItem.setMnemonic(KeyEvent.VK_B);
      blackItem.addActionListener(new PlanListener());

      redItem = new JRadioButtonMenuItem("800 minutes:$65 per month");
      redItem.setMnemonic(KeyEvent.VK_R);
      redItem.addActionListener(new PlanListener());

      blueItem = new JRadioButtonMenuItem("1500 minutes:$99 per month");
      blueItem.setMnemonic(KeyEvent.VK_U);
      blueItem.addActionListener(new PlanListener());

      // Create a button group for the radio button items.
      ButtonGroup group = new ButtonGroup();
      group.add(blackItem);
      group.add(redItem);
      group.add(blueItem);


      // Create a JMenu object for the Text menu.
      textMenu = new JMenu("Minutes");
      textMenu.setMnemonic(KeyEvent.VK_T);

      // Add the menu items to the Text menu.
      textMenu.add(blackItem);
      textMenu.add(redItem);
      textMenu.add(blueItem);

   }

    private void buildAddMenu()
      {
         // Create the radio button menu items to change the color
         // of the text. Add an action listener to each one.
         blackItem1 = new JRadioButtonMenuItem("Model 100:$29.95", true);
         blackItem1.setMnemonic(KeyEvent.VK_S);
         blackItem1.addActionListener(new PlanListener());

         redItem1 = new JRadioButtonMenuItem("Model 110:$49.95");
         redItem1.setMnemonic(KeyEvent.VK_K);
         redItem1.addActionListener(new PlanListener());

         blueItem1 = new JRadioButtonMenuItem("Model 200:$99.95");
         blueItem1.setMnemonic(KeyEvent.VK_P);
         blueItem1.addActionListener(new PlanListener());

         // Create a button group for the radio button items.
         ButtonGroup group = new ButtonGroup();
         group.add(blackItem1);
         group.add(redItem1);
         group.add(blueItem1);


         // Create a JMenu object for the Text menu.
         addMenu = new JMenu("Cell-Phone model");
         textMenu.setMnemonic(KeyEvent.VK_Z);

         // Add the menu items to the Text menu.
         addMenu.add(blackItem1);
         addMenu.add(redItem1);
         addMenu.add(blueItem1);

   }

    private void buildCellMenu()
	{
	  // Create a check box menu item to make the text
	  // visible or invisible.
	  voiceItem = new JCheckBoxMenuItem("Voice mail option", true);
	  voiceItem.setMnemonic(KeyEvent.VK_E);
      voiceItem.addActionListener(new PlanListener());

      // Create a check box menu item to make the text
	  // visible or invisible.
	  textItem = new JCheckBoxMenuItem("Text message option", true);
	  textItem.setMnemonic(KeyEvent.VK_F);
      textItem.addActionListener(new PlanListener());

      // Create a JMenu object for the Text menu.
	  cellMenu = new JMenu("Add ons");
	  cellMenu.setMnemonic(KeyEvent.VK_D);

	  // Add the menu items to the Text menu.
	  cellMenu.add(voiceItem);
	  cellMenu.add(textItem);

	}


   /**
    * Private inner class that handles the event that
    * is generated when the user selects Exit from
    * the File 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 color from
    * the Text menu.
    */

    private class PlanListener implements ActionListener
    {
      public void actionPerformed(ActionEvent e)
      {
		  double plan1;
		  double package1;
		  double text;
		  double message;
		  double addOn;
		  double total;

         // Determine which color was selected and
         // act accordingly.
          if (blackItem.isSelected()){
		       plan1=45;
		  else if (redItem.isSelected())
		       plan1=65;
		  else if (blueItem.isSelected())
		       plan1=99;
		  if (blackItem1.isSelected())
		       package1=29.95;
		  else if (redItem1.isSelected())
		 	   package1=49.95;
		  else if (blueItem1.isSelected())
		       package1=99.95;
		  if (voiceItem.isSelected())
			  message =5;
		  if(textItem.isSelected())
			   text=10;

			   addOn=message + text;
			   total= package1+(6/package1)*100+ plan1+ addOn;



      }
    }


   /**
    * The main method creates an instance of the MenuWindow
    * class, which causes it to display its window.
    */

   public static void main(String[] args)
   {
      new MenuWindow();
   }
}
This article has been dead for over six months. Start a new discussion instead.