Question

In: Computer Science

import javax.swing.*; public class MenuFrame extends JFrame { public MenuFrame() { setTitle("Menu Frame"); setSize(500, 500); MenuListenerExample...

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.

Solutions

Expert Solution

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);
                        }
                }
        }
}

Related Solutions

public class Mammal extends SeaCreature { public void method1() { System.out.println("warm-blooded"); } } public class SeaCreature...
public class Mammal extends SeaCreature { public void method1() { System.out.println("warm-blooded"); } } public class SeaCreature { public void method1() { System.out.println("creature 1"); } public void method2() { System.out.println("creature 2"); } public String toString() { return "ocean-dwelling"; } } public class Whale extends Mammal { public void method1() { System.out.println("spout"); } public String toString() { return "BIG!"; } } public class Squid extends SeaCreature { public void method2() { System.out.println("tentacles"); } public String toString() { return "squid"; } } What...
import java.util.LinkedList; import java.util.Queue; import java.time.*; public class CheckOutLine { /** This is the main function...
import java.util.LinkedList; import java.util.Queue; import java.time.*; public class CheckOutLine { /** This is the main function for the program */ public static void main() { System.out.println("\n\tBegin CheckOutLine Demo\n"); System.out.println("\nCreating a Queue called \"register\" based on a LinkedList"); Queue<Customer> register = new LinkedList<Customer>();    System.out.println("\nAdding Customers to the Register queue"); // 1. TO DO: add five or more customers to the register line (5 Pts) // format: register.add(new Customer(name, checkout time));                System.out.printf("Register line has %d customers\n",...
import java.util.ArrayList; import java.util.Collections; import java.lang.Exception; public class ItemList { /** This is the main process...
import java.util.ArrayList; import java.util.Collections; import java.lang.Exception; public class ItemList { /** This is the main process for the project */ public static void main () { System.out.println("\n\tBegin Item List Demo\n"); System.out.println("Declare an ArrayList to hold Item objects"); ArrayList<Item> list = new ArrayList<Item>(); try { System.out.println("\n Add several Items to the list"); list.add(new Item(123, "Statue")); list.add(new Item(332, "Painting")); list.add(new Item(241, "Figurine")); list.add(new Item(126, "Chair")); list.add(new Item(411, "Model")); list.add(new Item(55, "Watch")); System.out.println("\nDisplay original Items list:"); listItems(list); int result = -1; // 1....
import java.util.ArrayList; import java.util.Collections; import java.lang.Exception; public class ItemList { /** This is the main process...
import java.util.ArrayList; import java.util.Collections; import java.lang.Exception; public class ItemList { /** This is the main process for the project */ public static void main () { System.out.println("\n\tBegin Item List Demo\n"); System.out.println("Declare an ArrayList to hold Item objects"); ArrayList list = new ArrayList(); try { System.out.println("\n Add several Items to the list"); list.add(new Item(123, "Statue")); list.add(new Item(332, "Painting")); list.add(new Item(241, "Figurine")); list.add(new Item(126, "Chair")); list.add(new Item(411, "Model")); list.add(new Item(55, "Watch")); System.out.println("\nDisplay original Items list:"); listItems(list); int result = -1; // 1....
import java.util.LinkedList; import java.util.ListIterator; public class ProjectList { /** This is the main function for the...
import java.util.LinkedList; import java.util.ListIterator; public class ProjectList { /** This is the main function for the program */ public static void main() { System.out.println("\n\tBegin ProjectList demo program");    // 1. TO DO: Describe your project (1 Pt) System.out.println("Project: . . . . . . \n"); System.out.println("Create a LinkedList to store the tasks"); // 2. TO DO: Declare a LinkedList of String objects called "tasks" (1 Pt)       System.out.println("Add initial tasks into the list"); // 3. TO DO: Add initial...
import java.util.ArrayList; import java.util.Collections; public class BirthdayList { /** * This is the main process for...
import java.util.ArrayList; import java.util.Collections; public class BirthdayList { /** * This is the main process for class BirthdayList */ public static void main() { System.out.println("\n\tBegin Birthday List Program\n");    System.out.println("Declare an ArrayList for Birthday objects"); // 1. TO DO: Declare an ArrayList to store Birthday objects (3 Pts)       // 2. TO DO: Replace the xxx.xxxx() with the appropriate method (2 Pts) System.out.printf("\nBirthday List is empty: %s\n", xxx.xxxx() ? "True" : "False");    System.out.println("Add at least five Birthdays to...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) {...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) { Scanner input=new Scanner(System.in); int[] WordsCharsLetters = {0,1,2}; while(input.hasNext()) { String sentence=input.nextLine(); if(sentence!=null&&sentence.length()>0){ WordsCharsLetters[0] += calculateAndPrintChars(sentence)[0]; WordsCharsLetters[1] += calculateAndPrintChars(sentence)[1]; WordsCharsLetters[2] += calculateAndPrintChars(sentence)[2]; } else break; } input.close(); System.out.println("Words: " + WordsCharsLetters[0]); System.out.println("Characters: " + WordsCharsLetters[1]); System.out.println("Letters: " + WordsCharsLetters[2]); } static int[] calculateAndPrintChars(String sentence) { int[] WCL = new int[3]; String[] sentenceArray=sentence.split(" "); WCL[0] = sentenceArray.length; int letterCount=0; for(int i=0;i<sentence.length();i++) { if(Character.isLetter(sentence.charAt(i))) letterCount++; } WCL[1]...
import java.util.Random; import java.util.Scanner; public class Compass { public Random r; public Compass(long seed){ r =...
import java.util.Random; import java.util.Scanner; public class Compass { public Random r; public Compass(long seed){ r = new Random(seed); }    public static String numberToDirection(int a){ if(a==0) return "North";    if(a==1) return "NorthEast"; if(a==2) return "East"; if(a==3) return "Southeast"; if(a==4) return "South"; if(a==5) return "Southwest"; if(a==6) return "West";    if(a==7) return "Northwest";    return "Invalid Direction" ; } public String randomDirection(){ return numberToDirection(r.nextInt()% 4 + 1); } public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter seed: ");...
TASK: Based upon the following code: import java.util.Scanner; // Import the Scanner class public class Main...
TASK: Based upon the following code: import java.util.Scanner; // Import the Scanner class public class Main {   public static void main( String[] args ) {     Scanner myInput = new Scanner(System.in); // Create a Scanner object     System.out.println("Enter (3) digits: ");     int W = myInput.nextInt();     int X = myInput.nextInt();     int Y = myInput.nextInt();      } } Use the tools described thus far to create additional code that will sort the integers in either monotonic ascending or descending order. Copy your code and...
Explain the java class below, how it make: import java.util.*; import java.util.concurrent.TimeUnit; public class RacingGame {...
Explain the java class below, how it make: import java.util.*; import java.util.concurrent.TimeUnit; public class RacingGame {       ArrayList<Driver> player = new ArrayList<Driver>();    CarType car = new CarType("Ferrari","2018",280,90,15);    Formula formula = new Formula(5);// number of laps    long startTime = System.currentTimeMillis();    long currentTime = System.currentTimeMillis();    int totalDis = 0;       public static void main(String[] args)    {        RacingGame formulaRace = new RacingGame();        formulaRace.player.add(new Driver("Saleh",10,3));        formulaRace.formula.setDistance(20);//lap distance        formulaRace.totalDis...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT