You are on page 1of 3

Discussion Question # 2- Menus

Write a 200- to 300-word short-answer response for the following


What are advantages of using menus in a GUI application Design a menu structure for a
program you would find useful in your work or hobbies. Write and post the code for the menu
creation.
Menus in any GUI application are much important to let the user choose one of several
options to perform an action in interface instantly. Several actions can be placed in a
single menu and these menus are added to the main interface because a menu cant be
added to any other component rather than the main window (JFrame in case of Java)
and these menus are always visible in the interface.
Menus also save the space on the interface window. Here is an example to illustrate the
menus in GUI application.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SummervalResort extends JFrame implements ActionListener {

private JLabel titleLabel;
private JLabel messageLabel;

//menus for application
private JMenuBar menu_Bar ;
private JMenu file, rooms, dining, activity;
private JMenuItem exit, suite, single, inroom, indininghall,
indoorgames, outdoorgames;


public SummervalResort(){
//setting the title of the frame
super("Summerval Resort");
//closing action for this frame
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//set the layout to BorderLayout
setLayout(new BorderLayout());

titleLabel = new JLabel ("Summerval Resort");
titleLabel.setFont(new Font("vardana", Font.BOLD, 24));
titleLabel.setHorizontalAlignment(JLabel.CENTER);

messageLabel = new JLabel ("Click a menu to get information");
messageLabel.setFont(new Font("", Font.ITALIC, 16));
messageLabel.setHorizontalAlignment(JLabel.CENTER);

JPanel pane = new JPanel();
pane.add(titleLabel);
pane.add(messageLabel);

getContentPane().add(pane);

// Window menu bar
menu_Bar = new JMenuBar();

// Create File menu
file = new JMenu("File");
//adding file menu items
exit = file.add("Exit");

// Add the file menu
menu_Bar.add(file);

// Create rooms menu
rooms = new JMenu("Rooms");
//adding dining menu items
suite = rooms.add("Suite");
single = rooms.add("Single");

// Add the rooms menu
menu_Bar.add(rooms);

// Create dining menu
dining = new JMenu("Dining");
//adding dining menu items
inroom = dining.add("In Room");
indininghall = dining.add("In Dining Hall");

// Add the dining menu
menu_Bar.add(dining);

// Create activity menu
activity = new JMenu("Dining");
//adding activity menu items
indoorgames = activity.add("In-door Games");
outdoorgames = activity.add("Out-door Games");

// Add the activity menu
menu_Bar.add(activity);


// Add the menu bar to the main frame
setJMenuBar(menu_Bar);

exit.addActionListener(this);
suite.addActionListener(this);
single.addActionListener(this);
inroom.addActionListener(this);
indininghall.addActionListener(this);
indoorgames.addActionListener(this);
outdoorgames.addActionListener(this);

//set size of the frame
setSize(435,300);
setVisible(true);
}
public void actionPerformed(ActionEvent ae) {

if(ae.getSource() == exit){
int confirm = JOptionPane.showConfirmDialog(this, "Are you
sure, want to Exit?", "Exit?", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if(confirm == JOptionPane.YES_OPTION)
System.exit(0);
else
return;
}
else
if(ae.getSource() == suite){
messageLabel.setText("Lovely 3-room suites from $189
per night");
}
else
if(ae.getSource() == single){
messageLabel.setText("Lovely 5-room single from
$95 per night");
}
else
if(ae.getSource() == inroom){
messageLabel.setText("You can enjoy
dinner in your room for $50 per night.");
}
else
if(ae.getSource() == indininghall){
messageLabel.setText("You can enjoy
dinner in dining hall for $35 per night.");
}
else
if(ae.getSource() == indoorgames){
messageLabel.setText("You can
play Chess and Carrom Board from $100 per day.");
}
else
if(ae.getSource() ==
outdoorgames){

messageLabel.setText("You can enjoy Rafting from $100 per day.");
}

}

public static void main(String args[]){

new SummervalResort();
}
}

You might also like