You are on page 1of 13

Computer

Programming 2 CREATING MENUS


(Part 1)

In this lesson, you will learn to create menus. This makes your program more organized
and make any choices for your users. This will have many options to be chosen by the user to
make the program you will be developing more interactive and efficient.

At the end of this lesson, the student should be able to:

 Apply the concept of List Menu Command

 Utilize the syntax of List menu command

 Combine List menu with other components in the program

 Design your own program using List Menu Command

=======================================================================
Page | 1
Choice

A Choice is an item that lets you choose from a selection of alternatives. If this

sounds like a menu, you're right. Choices are free-spirited relatives of menus. A Choice item

can be positioned anywhere, in any kind of container. It looks something like a button, with

the current selection as its label.

It provides a list of items from which you can select one item at a time.

Example:

Choice choice = new Choice();


choice.addItem(“First option”);
choice.addItem(“Second option”);

Lists

A JList presents the user with a group of items, displayed in one or more columns, to
choose from. Lists can have many items, so they are often put in scroll panes.

In addition to lists, the following Swing components present multiple selectable items to the
user: combo boxes, menus, tables, and groups of check boxes or radio buttons

It is also a collection of strings from which you can choose.

Example:
List list = new List(5);
list.add(“First Item”);
list.add(“Second Item”);
list.add(“Third Item”);
list.add(“Fourth Item”);
list.add(“Fifth Item”);

=======================================================================
Page | 2
The pack() method is defined in Window class in Java and it sizes the frame so that

all its contents are at or above their preferred sizes. An alternative to the pack() method is to

establish a frame size explicitly by calling the setSize() or setBounds() methods. In general,

using the pack() method is preferable to call than setSize() method, since pack leaves the

frame layout manager in charge of the frame size and layout managers are good at adjusting

to platform dependencies and other factors that affect the component size.

The method show() is, indeed, deprecated. Deprecated means that you're not

supposed to use it anymore, as it's been replaced by something better and may be removed in

the future. In this case, you're supposed to use setVisible(true) instead.

Example Program

OUTPUT:
import java.awt.*;
import java.applet.*;
public class listChoice extends Frame
{
Choice entree, dessert;
List beverage;

public listChoice()
{
entree = new Choice();
entree.addItem("Golden Fried Shrimp");
entree.addItem("Beef with Oyster Sauce");
entree.addItem("Calamares");
entree.addItem("Chicken Curry");
entree.addItem("Lengua");

dessert = new Choice();


dessert.addItem("Blueberry CheeseCake");
dessert.addItem("Apple pie");
dessert.addItem("Sansrival");
dessert.addItem("Leche Flan");
dessert.addItem("Chocolate Mousse");

beverage = new List();


beverage.addItem("Beers");
=======================================================================
Page | 3
beverage.addItem("Rhum");
beverage.addItem("Wines");
beverage.addItem("Coffee");
beverage.addItem("Tea");
beverage.addItem("Juices");
beverage.addItem("Vodka");
beverage.addItem("Zombie");
beverage.addItem("Mineral Water");
Panel p = new Panel();
p.setLayout(new GridLayout(2,1));
p.add(entree);
p.add(dessert);
add("West", p);
add("Center", beverage);
pack();
show();
}
public static void main (String args [])
{
new listChoice();
}
}

Instructions: Open the links below to read more concepts of Graphical User Interface in
Java.

1. https://stackoverflow.com/questions/9346372/error-the-method-show-from-the-
type-window-is-deprecated

2. https://docs.oracle.com/javase/tutorial/uiswing/components/menu.html

3. https://docs.oracle.com/javase/tutorial/uiswing/components/list.html

=======================================================================
Page | 4
Instructions: Do the following program as your activity 1 and write the output of the
program on the box provided. Next Meeting will be your submission.

//CUSTOMER BILL
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
//Programmed by: Clarisse Santiago
public class Ass1Mid extends Frame implements ItemListener
{
Choice entree, dessert, rice;
List beverage, slip;
int amount=0;
double total, tax;
int cash;
TextField t;

public Ass1Mid()
{
entree = new Choice();
entree.addItem("DISH");
entree.addItem("Golden Fried Shrimp - 180");
entree.addItem("Beef with Oyster Sauce - 150");
entree.addItem("Calamares - 180");
entree.addItem("Chicken Curry - 100");
entree.addItem("Lengua - 120 ");
entree.addItemListener(this);
entree.setBackground(Color.pink);

dessert = new Choice();


dessert.addItem("DESSERT");
dessert.addItem("Blueberry CheeseCake - 80");
dessert.addItem("Apple pie - 100");
dessert.addItem("Sansrival - 100");
dessert.addItem("Leche Flan - 100");
=======================================================================
Page | 5
dessert.addItem("Chocolate Mousse - 120");
dessert.addItemListener(this);
dessert.setBackground(Color.pink);

rice = new Choice();


rice.addItem("RICE");
rice.addItem("Plain - 15");
rice.addItem("Fried - 25");
rice.addItem("Java - 45");
rice.addItemListener(this);
rice.setBackground(Color.pink);

beverage = new List(9, true);


beverage.addItem("BEVERAGES");
beverage.addItem("");
beverage.addItem("Beers - 150");
beverage.addItem("Rhum - 180");
beverage.addItem("Wines - 250");
beverage.addItem("Coffee - 80");
beverage.addItem("Tea - 80");
beverage.addItem("Juices - 60");
beverage.addItem("Vodka - 150");
beverage.addItem("Zombie - 100");
beverage.addItem("Mineral Water - 50");
beverage.addItemListener(this);
beverage.setBackground(Color.pink);

slip = new List(30, true);


slip.addItem(" Clarissa Restoria");
slip.addItem(" #1003 Gerald St., Brngy.Tapos, Binangonan,
Rizal");
slip.addItem(" ORDER SLIP");
slip.addItem(" ----------------------------------------------------------------------");
slip.setBackground(Color.white);

JButton button1 = new JButton ("Compute the Bill.");


button1.addMouseListener(new compute());
JTextField t = new JTextField(20);

JLabel restoName = new JLabel("SARMIENTO RESTAURANT");

=======================================================================
Page | 6
JLabel motto = new JLabel("We serve foods at their FINEST!");
Panel panel1 = new Panel();
Panel panel2 = new Panel();
Panel panel3 = new Panel();
Panel panel4 = new Panel();

panel1.setBackground(Color.yellow);
panel2.setBackground(Color. yellow);
panel3.setBackground(Color. yellow);
panel4.setBackground(Color. yellow);
panel1.add(entree);
panel1.add(rice);
panel1.add(dessert);
panel2.add(beverage);
panel3.add(button1);
panel4.add(slip);
add(restoName);
add(motto);
add(panel1);
add(panel2);
add(panel3);
add(panel4);
pack();
show();
}

public void itemStateChanged(ItemEvent e)


{
int i;
String clarz[] = new String[9];
clarz=beverage.getSelectedItems();

if(e.getSource()==entree)
{
if(entree.getSelectedItem()=="Golden Fried Shrimp - 180")
{
slip.addItem("Golden Fried Shrimp - 180");
amount=amount+=180;}

else if(entree.getSelectedItem()=="Beef with Oyster Sauce - 150")

=======================================================================
Page | 7
{
slip.addItem("Beef with Oyster Sauce - 150");
amount=amount+=150;}

else if(entree.getSelectedItem()=="Calamares - 180")


{
slip.addItem("Calamares - 180");
amount=amount+=180;}

else if(entree.getSelectedItem()=="Chicken Curry - 100")


{
slip.addItem("Chicken Curry - 100");
amount=amount+=100;}

else if(entree.getSelectedItem()=="Lengua - 120 ")


{
slip.addItem("Lengua - 120");
amount=amount+=120;}
}

else if(e.getSource()==dessert)
{
if(dessert.getSelectedItem()=="Blueberry CheeseCake - 80")
{
slip.addItem("Blueberry CheesCcake - 80");
amount=amount+=80;}
else if(dessert.getSelectedItem()=="Apple pie - 100")
{
slip.addItem("Apple pie - 100");
amount=amount+=100;}
else if(dessert.getSelectedItem()=="Sansrival - 100")
{
slip.addItem("Sansrival - 100");
amount=amount+=100;}
else if(dessert.getSelectedItem()=="Leche Flan - 100")
{
slip.addItem("Leche Flan - 100");
amount=amount+=100;}
else if(dessert.getSelectedItem()=="Chocolate Mousse - 120")
{

=======================================================================
Page | 8
slip.addItem("Chocolate Mousse - 120");
amount=amount+=120;}
}
if(e.getSource()==rice)
{
if(rice.getSelectedItem()=="Plain - 15")
{
slip.addItem("Plain rice - 15");
amount=amount+=15;}
if(rice.getSelectedItem()=="Fried - 25")
{
slip.addItem("Fried rice - 25");
amount=amount+=25;}
if(rice.getSelectedItem()=="Java - 45")
{
slip.addItem("Java rice - 45");
amount=amount+=45;}
}

for(i=0;i<clarz.length;i++)
{
if(clarz[i].equals("Beers - 150"))
{
slip.addItem("Beers - 150");
amount=amount+=150;}
else if(clarz[i].equals("Rhum - 180"))
{
slip.addItem("Rhum - 180");
amount=amount+=180;}
else if(clarz[i].equals("Wines - 250"))
{
slip.addItem("Wines - 250");
amount=amount+=250;}
else if(clarz[i].equals("Coffee - 80"))
{
slip.addItem("Coffee - 80");
amount=amount+=80;}
else if(clarz[i].equals("Tea - 80"))
{
slip.addItem("Tea - 80");

=======================================================================
Page | 9
amount=amount+=80;}
else if(clarz[i].equals("Juices - 60"))
{
slip.addItem("Juices - 60");
amount=amount+=60;}
else if(clarz[i].equals("Vodka - 150"))
{
slip.addItem("Vodka - 150");
amount=amount+=150;}
else if(clarz[i].equals("Zombie - 100"))
{
slip.addItem("Zombie - 100");
amount=amount+=100;}
else if(clarz[i].equals("Mineral Water - 50"))
{
slip.addItem("Mineral Water - 50");
amount=amount+=50;}
}
} // end of public void itemStateChanged event

private class compute implements MouseListener{


public void mouseClicked (MouseEvent e){
tax= amount*.12;
total = amount+tax;
slip.addItem("");
slip.addItem("Tax: 12%");
slip.addItem("Total: P" + String.valueOf(total));
slip.addItem("");
slip.addItem("Get your slip at the OrderPrinter.");
slip.addItem("Show it and pay at the cashier.");
slip.addItem("Thank you!");
slip.addItem("Please come again.");
}
public void mousePressed (MouseEvent e){}
public void mouseReleased (MouseEvent e){}
public void mouseExited (MouseEvent e){}
public void mouseEntered (MouseEvent e){}
}

public static void main (String args [])

=======================================================================
Page | 10
{
Ass1Mid clarisse = new Ass1Mid();
clarisse.setTitle("Clarissa Restoria");
clarisse.setSize(600,750);
clarisse.setVisible(true);
clarisse.setLayout(new FlowLayout());
clarisse.setBackground(Color.pink);
}
}

Output

=======================================================================
Page | 11
Instructions: Create a program that will compute the customer bill in a restaurant where you
are currently working with. Provide receipt for your customer as shown below.

=======================================================================
Page | 12
Books

1. Abante, Marmelo V. et al, Java Programming, 1st Edition, Unlimited Books


Publishing Company, 2018.

2. Pomperada, Jake R, Introduction to JAVA Programming., M


Mindshapers. 2018

3. JAVA Programming for Human Beings: The Ultimate Beginner’s


Introduction. Deshpande, Mohit. 2017 Zenva Pty Ltd.
(https://zenva.com)
4. Jim Keogh, Java De Mystified “A Self Teaching Guide”, Mc Graw Hill,
Osborne, 7th Edition, 2015.

5. Rogers Cadenhead, Programming with Java in 24 Hours, 4th Edition, Sams


Publishing, 2016.
6. Walter Savitch, JAVA, An Introduction to Computer Science & Programming,
Pearson Education Asia Inc., 3rd Edition, 2014.

Websites

1. https://www.javatpoint.com/GridLayout

2. https://www.infoworld.com/article/2077351/events-and-listeners.html

3. https://www.javatpoint.com/java-jpanel

=======================================================================
Page | 13

You might also like