How to Create a Restaurant Menu With a Java Program Applet

Save

Using the Java Applet feature, you can embed an automatic menu for your restaurant into it's web page. Among other features, this will allow you to separate dishes from the style of the applet, making trivial changes to the look of the applet or to the dishes within which will be updated instantly.

The Applet Class

  • Open your favorite text editor, or, if you prefer, a dedicated Java development environment such as Netbeans. Immediately save the file as "Main.java."

  • Paste the following within "Main.java:"

    package restaurantmenu;

    import java.awt.GridLayout;

    import java.awt.Image;

    import java.awt.Toolkit;

    import java.util.ArrayList;

    import javax.swing.JApplet;

    import java.awt.Panel;

    public class Main extends JApplet {

    ArrayList<Panel> dishList = new ArrayList<Panel>();

    @Override

    public void init() {

    this.setLayout(new GridLayout(0, 1));

    // To load new dishes, simple repeat these two lines, as often as needed, for every dish.

    Image i = Toolkit.getDefaultToolkit().getImage("kungpao.jpg");

    dishList.add(new ItemPanel("Kung Pao Chicken", "Spicy Chicken Dish", "$9.95", i));

    Image i = Toolkit.getDefaultToolkit().getImage("orangechicken.jpg");

    dishList.add(new ItemPanel("Orange Chicken", "A tasty dish.","$10.25" ,i));

    for (Panel dish : dishList) {

    this.add(dish);

    }

    }

    }

    This creates a basic applet containing an array list of dishes. As soon as the applet is loaded, it creates two new dishes using the "ItemPanel" class that you will create in section two: Kung Pao Chicken and Orange Chicken and adds them to the menu.

  • Save your work and close the document.

The ItemPanel Class

  • Open another copy of your favorite text editor. Save the document as "ItemPanel.java."

  • Paste the following into it:

    package restaurantmenu;

    import java.awt.BorderLayout;

    import java.awt.Dimension;

    import java.awt.Image;

    import javax.swing.ImageIcon;

    import javax.swing.JButton;

    import javax.swing.JLabel;

    import java.awt.Panel;

    public class ItemPanel extends Panel {

    JLabel name;

    JLabel description;

    JLabel price;

    JButton image;

    static final int IMAGE_WIDTH = 100;

    static final int IMAGE_HEIGHT = 100;

    public ItemPanel(String dishName, String dishDescription, String dishprice, Image picture) {

    super();

    image = new JButton(new ImageIcon(picture));

    name = new JLabel(dishName);

    price = new JLabel(dishprice);

    description = new JLabel(dishDescription);

    image.setBackground(this.getBackground());

    image.setPreferredSize(new Dimension(IMAGE_WIDTH, IMAGE_HEIGHT));

    this.setLayout(new BorderLayout());

    this.add(name, BorderLayout.NORTH);

    this.add(image, BorderLayout.WEST);

    this.add(description, BorderLayout.CENTER);

    this.add(price, BorderLayout.EAST);

    }

    }

    This holds information about the actual items on the menu and how they should be laid out. It takes a dish name, a dish description, a dish price, and a picture, in that order. You can see how this is called in step two of the Applet class.

  • Save your work.

  • Add the following code to your web-page's HTML file:

    <APPLET code="Main.class" width=600 height=200></APPLET>

References

  • Photo Credit Maria Toutoudaki/Photodisc/Getty Images
Promoted By Zergnet

Comments

Resources

You May Also Like

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!