In: Computer Science
3. For this week’s assignment, you are asked to 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
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,about,showPicture;
JTextArea textArea,textArea1;
public MenuListenerExample() {
cut = new JMenuItem("cut");
copy = new JMenuItem("copy");
paste = new JMenuItem("paste");
selectAll = new JMenuItem("selectAll");
about=new JMenuItem("about");
showPicture=new JMenuItem("show");
textArea = new JTextArea();
textArea1 = new JTextArea();
cut.addActionListener(new MenuAction());
copy.addActionListener(new MenuAction());
paste.addActionListener(new MenuAction());
selectAll.addActionListener(new
MenuAction());
about.addActionListener(new MenuAction());
showPicture.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);
helpMenu.add(about);
fileMenu.add(showPicture);
add(fileMenu);
add(editMenu);
add(helpMenu);
textArea.setBounds(30, 30, 430, 400);
textArea1.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");
textArea1.setText("This is simple Notepad
Application\n"+"1.File : It has show picture option\n"
+"Edit : It has three option
cut,copy,selectall\n"+
"help : It has about option which show
function\n");
textArea1.setEditable(false);
frame.add(textArea1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
Replace if statements and use the switch statement
i need output show pictures in file menu
Image path is
C:\\Users\\DGL2\\Desktop\\3840.jpg_wh860.jpg
package menu;
import javax.swing.*;
public class MenuFrame extends JFrame
{
private static final long serialVersionUID = 1L;
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);
frame.setLocationRelativeTo(null);
}
}
-------------------------------------------------------------------------------------------------------------
package menu;
import javax.swing.*;
import java.awt.event.*;
public class MenuListenerExample extends JMenuBar
{
private static final long serialVersionUID = 1L;
JMenu fileMenu, editMenu, helpMenu;
JMenuItem cut, copy, paste,
selectAll,about,showPicture;
JTextArea textArea,textArea1;
JFrame frame;
public MenuListenerExample()
{
cut = new JMenuItem("cut");
copy = new JMenuItem("copy");
paste = new
JMenuItem("paste");
selectAll = new
JMenuItem("selectAll");
about=new JMenuItem("about");
showPicture=new JMenuItem("Show
Picture");
textArea = new JTextArea();
textArea1 = new JTextArea();
cut.addActionListener(new
MenuAction());
copy.addActionListener(new
MenuAction());
paste.addActionListener(new
MenuAction());
selectAll.addActionListener(new
MenuAction());
about.addActionListener(new
MenuAction());
showPicture.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);
helpMenu.add(about);
fileMenu.add(showPicture);
add(fileMenu);
add(editMenu);
add(helpMenu);
textArea.setBounds(30, 30, 430,
400);
textArea1.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)
{
frame = new JFrame("About");
textArea1.setText("This is simple Notepad
Application\n"+
" 1.File : It has show picture option\n"
+" 2. Edit
: It has three option cut,copy,selectall\n"+
" 3. help
: It has about option which show function\n");
textArea1.setEditable(false);
frame.add(textArea1);
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
if
(e.getSource() == showPicture)
{
frame = new JFrame("My Image");
JLabel img = new JLabel();
img.setIcon(new
ImageIcon("F:/OnlinePrograms/tiger.jpg"));
frame.add(img);
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
}
}
}
Note: Highlighted part is the modified code
Sample Output: