Java2 Week-11 Student
Java2 Week-11 Student
Java2 Week-11 Student
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.
=======================================================================
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
It provides a list of items from which you can select one item at a time.
Example:
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
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
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");
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);
=======================================================================
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();
}
if(e.getSource()==entree)
{
if(entree.getSelectedItem()=="Golden Fried Shrimp - 180")
{
slip.addItem("Golden Fried Shrimp - 180");
amount=amount+=180;}
=======================================================================
Page | 7
{
slip.addItem("Beef with Oyster Sauce - 150");
amount=amount+=150;}
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
=======================================================================
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
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