In: Computer Science
import javax.swing.*;
public class MenuFrame extends JFrame {
public MenuFrame() {
setTitle("Menu Frame");
setSize(500, 500);
MenuListenerExample myMenu = new
MenuListenerExample();
setJMenuBar(myMenu);
setLayout(null);
add(myMenu.textArea);
}
public static void main(String[] args) {
MenuFrame frame = new MenuFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
import javax.swing.*;
import java.awt.event.*;
public class MenuListenerExample extends JMenuBar
{
JMenu fileMenu, editMenu, helpMenu;
JMenuItem cut, copy, paste, selectAll;
JTextArea textArea;
public MenuListenerExample() {
cut = new JMenuItem("cut");
copy = new JMenuItem("copy");
paste = new JMenuItem("paste");
selectAll = new JMenuItem("selectAll");
textArea = new JTextArea();
cut.addActionListener(new MenuAction());
copy.addActionListener(new MenuAction());
paste.addActionListener(new MenuAction());
selectAll.addActionListener(new
MenuAction());
fileMenu = new JMenu("File");
editMenu = new JMenu("Edit");
helpMenu = new JMenu("Help");
editMenu.add(cut);
editMenu.add(copy);
editMenu.add(paste);
editMenu.add(selectAll);
add(fileMenu);
add(editMenu);
add(helpMenu);
textArea.setBounds(30, 30, 430, 400);
}
private class MenuAction implements ActionListener
{
public void actionPerformed(ActionEvent e) {
if (e.getSource() == cut) {
textArea.cut();
}
if (e.getSource() == paste) {
textArea.paste();
}
if (e.getSource() == copy) {
textArea.copy();
}
if (e.getSource() == selectAll) {
textArea.selectAll();
}
}
}
}
modify the above Java program to include the following: - When the
user clicks on the help menu, a drop-down list will appear with an
item called About, then when the user clicks on it, the window will
show some instructions about the functionality of the menu, e.g,
what the edit menu does, etc. - When the user clicks on the File
menu, a drop-down list will appear with one item called Show
Picture, and when the user clicks on it, a picture of your choice
will appear.
Please see following code snippet which I have added
first code snippet is for MenuFrame.java file and second is for MenuListenerExample.java file.
1. MenuFrame.java
import javax.swing.*;
public class MenuFrame extends JFrame {
public MenuFrame() {
setTitle("Menu Frame");
setSize(500, 500);
MenuListenerExample myMenu = new MenuListenerExample();
setJMenuBar(myMenu);
setLayout(null);
add(myMenu.textArea);
}
public static void main(String[] args) {
MenuFrame frame = new MenuFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
2. MenuListenerExample.java
import javax.swing.*;
import java.awt.event.*;
public class MenuListenerExample extends JMenuBar {
JMenu fileMenu, editMenu, helpMenu;
// added variable for about and showPicture menu
JMenuItem cut, copy, paste, selectAll, about, showPicture;
JTextArea textArea;
public MenuListenerExample() {
cut = new JMenuItem("cut");
copy = new JMenuItem("copy");
paste = new JMenuItem("paste");
selectAll = new JMenuItem("selectAll");
textArea = new JTextArea();
// Initialize both showPicture and about vars wit Show Picture and About text
about = new JMenuItem("About");
showPicture = new JMenuItem("Show Picture");
cut.addActionListener(new MenuAction());
copy.addActionListener(new MenuAction());
paste.addActionListener(new MenuAction());
selectAll.addActionListener(new MenuAction());
// Also add Action Listner to both of them
about.addActionListener(new MenuAction());
showPicture.addActionListener(new MenuAction());
fileMenu = new JMenu("File");
editMenu = new JMenu("Edit");
helpMenu = new JMenu("Help");
// add showPicture to file menu
fileMenu.add(showPicture);
editMenu.add(cut);
editMenu.add(copy);
editMenu.add(paste);
editMenu.add(selectAll);
// add about to help menu
helpMenu.add(about);
add(fileMenu);
add(editMenu);
add(helpMenu);
textArea.setBounds(30, 30, 430, 400);
}
private class MenuAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == cut) {
textArea.cut();
}
if (e.getSource() == paste) {
textArea.paste();
}
if (e.getSource() == copy) {
textArea.copy();
}
if (e.getSource() == selectAll) {
textArea.selectAll();
}
if (e.getSource() == about) {
JFrame frame = new JFrame("About");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextArea textArea1 = new JTextArea();
// Set the text to the Any instruction that we want.
textArea1.setText("This is a simple Notepad Application where we have given following menus.\n"
+ " 1. File : It has Show picture option to show image.\n"
+ " 2. Edit : It has three options Cut, Copy, Paste, Select All\n"
+ " There operation allow user to Cut, Copy, Paste or Select All\n"
+ " in text in the given text area.\n"
+ " 3. Help : It has About option which will show functionality of the menu\n");
textArea1.setEditable(false);
frame.add(textArea1);
frame.pack();
frame.setVisible(true);
}
if (e.getSource() == showPicture) {
JFrame frame = new JFrame("Show Picture");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// You need to give path of file like C:\\Users\\Desktop\\Java\\output.JPG
ImageIcon image = new ImageIcon("C:\\Users\\Desktop\\Java\\output.JPG");
JLabel imageLabel = new JLabel(image);
imageLabel.setBounds(10, 10, 400, 400);
imageLabel.setVisible(true);
frame.add(imageLabel);
frame.pack();
frame.setVisible(true);
}
}
}
}