Question

MyMenuFrame.java, MyMenuFrameTest.java, a. The title of the frame is “MyNotepad”. b. Create and add border layout....

MyMenuFrame.java,
MyMenuFrameTest.java,

a. The title of the frame is “MyNotepad”.
b. Create and add border layout.
c. Create a text area to display contents. Add the text area to the center of the border layout.
d. Create a menu bar.
e. Create a file menu. Set mnemonic for file menu. It is “F”. File menu includes three menu items.
f. Add a separator between each menu item in the file menu.
i. Open
   1. Add a short cut for the menu item. It is ctrl+O.
       a. Use this syntax:
       MenuItem.setAccelerator(KeyStroke.getKeyStr oke('O', CTRL_DOWN_MASK));
   2. When a user clicks it (an action event occurs) a file
   chooser is created, and an open dialog box is opened.
   The user selects a text file to open it and then clicks
   “Open” or the user can click on “Cancel”. Use try block
   with resources. (Hint: add catch block automatically, click add catch clause).
ii. Save
   1. Add a short cut for the menu item. It is ctrl+S.
   2. When a user clicks it (an action event occurs) a file
    chooser is created, and a save dialog box is opened. The
   user writes the name of text file to save it and then clicks
   “Save” or the user can click on “Cancel”. User try block with
   resources. (Hint: add catch block automatically, click add catch clause).
iii. Exit
   1. Add a short cut for the menu item. It is ctrl+X.
   2. When a user clicks it (an action event occurs), it terminates the application.

g. Create edit menu. Set mnemonic for file menu. It is “D”. Edit menu
    includes two menus: Color and Font.

h. Add a separator between each sub menu

i. Create color menu. Set mnemonic for Color menu. It is “C”.
   i. Create change color menu item. Add a short cut for the menu
   item. It is ctrl+C.

ii. When a user clicks change color, a color chooser is opened,
and the user selects a color. By default, red color is selected.
Then, the color of the text is changed.

j. Create font menu. Set mnemonic for Font menu. It is “F”.
   i. This menu includes three radio button menu items: Times New Roman,
   Arial, Serif. (Hint: you also need a button group).
   When a user selects one of these fonts (an action event occurs),
   the font of the text will change. Set font size as 20.   
  
   ii. This menu includes also two check box menu items: Bold,
   Italic. A User can select both, only bold one, italic one,
   or unselect any of them (an item event occurs). Based on
   the user selection, set the font. (Hint: if one of them is
   unselected, set the font as plain). Set font size as 20.
   iii. Add a separator between radio button menu items and check box menu items

k. Create Print menu. Set mnemonic for print menu. It is “P”.
   i. Printer menu includes one menu item: Send to Printer.
   Add a short cut for the menu item. It is ctrl+P.

ii. When a user clicks it (an action event occurs), display
an Option Dialog. Display the message in the figure. If
the user clicks “Ok”, display a message dialog box (set information icon).
Display the message in the figure. If the user clicks
cancel, make the current frame as visible.

l. Create a help menu. Set mnemonic for help menu. It is “H”.
m. Help menu includes two menu items: About, Visit Homepage. Add
a separator between these menu items.

i. Create a menu item which is about. Add a short cut for the menu item. It is ctrl+A.

1. When a user clicks it (an action event occurs), display
a show message dialog box. Display the message
shown in the figure. Display information icon.


ii. Create a menu item which is visit homepage. Add a short cut
for the menu item. It is ctrl+V.
   1. When a user clicks it (an action event occurs), the user
   will be navigated to http://www.microsoft.com.
   2. For the navigation, create a static method (copy it):

   public static void openWebpage (String urlString) {
   try {
Desktop.getDesktop().browse(new
URL(urlString).toURI());
   }
    catch (Exception e) {
e.printStackTrace();
    }
   }

Then in the action performed method, call this static method
and provide the url String.

n. When it is necessary, import the necessary classes and interfaces.
o. Set frame as 600*400.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.io.*;
import javax.swing.*;

class MyMenuFrame extends JFrame implements ActionListener {
   JMenu menuEdit = new JMenu("Edit");
   JMenu menuPrint = new JMenu("Print");
   JMenu mnFile = new JMenu("File");
   JMenu menuHelp = new JMenu("Help");
   JRadioButton subMenuItem1 = new JRadioButton("Times New Roman");
   JRadioButton subMenuItem2 = new JRadioButton("Arial");
   JRadioButton subMenuItem3 = new JRadioButton("Serif");
   JCheckBox subMenuItem4 = new JCheckBox("Bold");
   JCheckBox subMenuItem5 = new JCheckBox("Italic");
   JScrollPane scrollPane;
   JTextArea notePadArea;
   public MyMenuFrame() {
       this.setLayout(new BorderLayout());
       JMenuBar menuBar = new JMenuBar();
       this.setJMenuBar(menuBar);
       mnFile.setMnemonic(KeyEvent.VK_F);
       menuBar.add(mnFile);
       JMenuItem mntmOpen = new JMenuItem("Open");
       mntmOpen.setMnemonic(KeyEvent.VK_O);
       mntmOpen.setActionCommand("open");
       mntmOpen.setAccelerator(KeyStroke.getKeyStroke('O', KeyEvent.CTRL_DOWN_MASK));
       mntmOpen.addActionListener(this);
       JMenuItem mntmSave = new JMenuItem("Save");
       mntmSave.setMnemonic(KeyEvent.VK_S);
       mntmSave.setActionCommand("save");
       mntmSave.setAccelerator(KeyStroke.getKeyStroke('S', KeyEvent.CTRL_DOWN_MASK));
       mntmSave.addActionListener(this);
       JMenuItem mntmExit = new JMenuItem("Exit");
       mntmExit.setMnemonic(KeyEvent.VK_X);
       mntmExit.setActionCommand("exit");
       mntmExit.setAccelerator(KeyStroke.getKeyStroke('X', KeyEvent.CTRL_DOWN_MASK));
       mntmExit.addActionListener(this);
       mnFile.add(mntmOpen);
       mnFile.addSeparator();
       mnFile.add(mntmSave);
       mnFile.addSeparator();
       mnFile.add(mntmExit);
       menuEdit.setMnemonic(KeyEvent.VK_E);
       menuBar.add(menuEdit);
       JMenu submenu1 = new JMenu("Color");
       submenu1.setMnemonic(KeyEvent.VK_C);
       JMenuItem menuItem0 = new JMenuItem("Change Color");
       menuItem0.setAccelerator(KeyStroke.getKeyStroke('C', KeyEvent.CTRL_DOWN_MASK));
       menuItem0.setActionCommand("color");
       menuItem0.addActionListener(this);
       submenu1.add(menuItem0);
       menuEdit.add(submenu1);
       menuEdit.addSeparator();
       ActionListener sm1 = new ActionListener() {
           public void actionPerformed(ActionEvent actionEvent) {
               subMenuItem1.setSelected(true);
               subMenuItem2.setSelected(false);
               subMenuItem3.setSelected(false);
               notePadArea.setFont(new Font("Times New Roman", Font.PLAIN, 20));
           }
       };
       ActionListener sm2 = new ActionListener() {
           public void actionPerformed(ActionEvent actionEvent) {
               subMenuItem2.setSelected(true);
               subMenuItem1.setSelected(false);
               subMenuItem3.setSelected(false);
               notePadArea.setFont(new Font("Arial", Font.PLAIN, 20));

           }
       };

       ActionListener sm3 = new ActionListener() {
           public void actionPerformed(ActionEvent actionEvent) {
               subMenuItem3.setSelected(true);
               subMenuItem2.setSelected(false);
               subMenuItem1.setSelected(false);
               notePadArea.setFont(new Font("Serif", Font.PLAIN, 20));

           }
       };

       JMenu submenu = new JMenu("Font"); //creating the new menu which comes under Edit
       submenu.setMnemonic(KeyEvent.VK_F); //creating shortcut menu when user press Alt+F
       subMenuItem1.setMnemonic(KeyEvent.VK_T); //creating shortcut menu when user press Alt+T for Times New Roman
       subMenuItem1.setActionCommand("times_new_roman"); //setting the command used to call the correcponding action when user click this
       subMenuItem1.addActionListener(sm1); //adding actionlistener
       submenu.add(subMenuItem1); //adding to the submenu

       subMenuItem2.setMnemonic(KeyEvent.VK_A); //creating shortcut key Alt+A
       subMenuItem2.setActionCommand("arial"); //respond when the command equals to arial
       subMenuItem2.addActionListener(sm2); //adding action listener
       submenu.add(subMenuItem2); //adding it to the submenu

       subMenuItem3.setMnemonic(KeyEvent.VK_S);
       subMenuItem3.setActionCommand("serif");
       subMenuItem3.addActionListener(sm3);
       submenu.add(subMenuItem3);

       submenu.addSeparator();

       subMenuItem4.setMnemonic(KeyEvent.VK_B);
       subMenuItem4.setActionCommand("bold");
       subMenuItem4.addActionListener(this);
       submenu.add(subMenuItem4);

       subMenuItem5.setMnemonic(KeyEvent.VK_I);
       subMenuItem5.setActionCommand("italic");
       subMenuItem5.addActionListener(this);
       submenu.add(subMenuItem5);

       menuEdit.add(submenu);

       // create Print menu

       menuPrint.setMnemonic(KeyEvent.VK_P);

       menuBar.add(menuPrint);

       JMenuItem menuItemPrint = new JMenuItem("Send To Printer");

       menuItemPrint.setAccelerator(KeyStroke.getKeyStroke('P', KeyEvent.CTRL_DOWN_MASK));

       menuItemPrint.setActionCommand("print");

       menuItemPrint.addActionListener(this);

       menuPrint.add(menuItemPrint);

       // create Help menu

       menuHelp.setMnemonic(KeyEvent.VK_H);

       menuBar.add(menuHelp);

       JMenuItem menuItemHelp = new JMenuItem("About");

       menuItemHelp.setAccelerator(KeyStroke.getKeyStroke('A', KeyEvent.CTRL_DOWN_MASK));

       menuItemHelp.setActionCommand("about");
       menuItemHelp.addActionListener(this);

       JMenuItem menuItemVisitHomePage = new JMenuItem("Visit Home Page");

       menuItemVisitHomePage.setAccelerator(KeyStroke.getKeyStroke('V', KeyEvent.CTRL_DOWN_MASK));

       menuItemVisitHomePage.setActionCommand("visithomepage");
       menuItemVisitHomePage.addActionListener(this);

       menuHelp.add(menuItemHelp);

       menuHelp.addSeparator();

       menuHelp.add(menuItemVisitHomePage);

       notePadArea = new JTextArea();

       // set no word wrap

       notePadArea.setWrapStyleWord(false);

       // create scrollable pane

       scrollPane = new JScrollPane(notePadArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

       this.add(scrollPane, BorderLayout.CENTER);

   }

   @Override

   public void actionPerformed(ActionEvent e) {

       if (e.getActionCommand().equals("exit")) {

           System.exit(0);

       } else if (e.getActionCommand().equals("open")) {

           JFileChooser file = new JFileChooser();

           String fileName = ""; //initial filename was empty

           // show open file dialog

           if (file.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {

               fileName = file.getSelectedFile().getAbsolutePath();

           } else {

               return;

           }

           try (BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName));) {

               // load file content into text area

               StringBuffer stringBuffer = new StringBuffer(); //creating a string buffer for reading data from file

               String lines = ""; //for reading the lines from the selecting file

               while ((lines = bufferedReader.readLine()) != null) { //it'll read untill the file ends

                   stringBuffer.append(lines).append("\n"); //for every line read insert new line in stringBuffer

               }

               bufferedReader.close(); //after reading of file done, the bufferedReader will be close

               notePadArea.setText(stringBuffer.toString()); //converting the read text to string and inserting this text into textArea

           } catch (Exception error1) { //if any exception occures

               System.out.println(error1.toString()); //convert the expection into string and print it

           }

       } else if (e.getActionCommand().equals("save")) { //if the user click the save command then the file will gonna saved

           JFileChooser file = new JFileChooser(); //creating the file chooser

           String fileName = ""; //initial file name is empty

           // show open file dialog

           if (file.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) { //if the user select file and clicks OK button

               fileName = file.getSelectedFile().getAbsolutePath();

           } else { //other wise will be closed
               return;
           }

           try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(fileName));) {

               // write editor's content to selected file.

               bufferedWriter.write(notePadArea.getText()); //get the text entered in textarea
               bufferedWriter.flush(); //clear the writer
           } catch (Exception ex1) {}

       } else if (e.getActionCommand().equals("color")) {

           Color select_color = JColorChooser.showDialog(this, "Select a color", Color.RED);
           notePadArea.setForeground(select_color);

       } else if (e.getActionCommand().equals("times_new_roman")) {
           if (subMenuItem1.isSelected())
               notePadArea.setFont(new Font("Times New Roman", Font.PLAIN, 20));

       } else if (e.getActionCommand().equals("arial")) {
           if (subMenuItem2.isSelected())
               notePadArea.setFont(new Font("Arial", Font.PLAIN, 20));

       } else if (e.getActionCommand().equals("serif")) {
           if (subMenuItem3.isSelected())
               notePadArea.setFont(new Font("Serif", Font.PLAIN, 20));

       } else if (e.getActionCommand().equals("bold")) {
           if (subMenuItem4.isSelected()) {
               if (subMenuItem5.isSelected()) {
                   notePadArea.setFont(new Font("Serif", Font.BOLD + Font.ITALIC, 12));
               } else {
                   notePadArea.setFont(new Font("Serif", Font.BOLD, 12));
               }
           } else {
               if (!subMenuItem5.isSelected())
                   notePadArea.setFont(new Font("Serif", Font.PLAIN, 12));
           }

       } else if (e.getActionCommand().equals("italic")) {

           if (subMenuItem5.isSelected()) {
               if (subMenuItem4.isSelected()) {
                   notePadArea.setFont(new Font("Serif", Font.BOLD + Font.ITALIC, 12));
               } else {
                   notePadArea.setFont(new Font("Serif", Font.ITALIC, 12));
               }
           } else {
               if (!subMenuItem4.isSelected())
                   notePadArea.setFont(new Font("Serif", Font.PLAIN, 12));
           }

       } else if (e.getActionCommand().equals("print")) {

           int output = JOptionPane.showConfirmDialog(this, "Do you want to print the File", "Confirmation", JOptionPane.YES_NO_OPTION);
           if (output == 0) {
               JOptionPane.showMessageDialog(this, "The file is successfully printed", "Confirmation", JOptionPane.INFORMATION_MESSAGE);
           }
       } else if (e.getActionCommand().equals("changecolor")) {
           System.out.println("Color clicked");
       } else if (e.getActionCommand().equals("about")) {
           JOptionPane.showMessageDialog(this, "This software is developed in 2019\nVersion is 1.0", "About", JOptionPane.INFORMATION_MESSAGE);
       } else if (e.getActionCommand().equals("visithomepage")) {
           openWebpage("http://www.microsoft.com");
       }
   }
   private void openWebpage(String urlString) {
       try {
           Desktop.getDesktop().browse(new URL(urlString).toURI());
       } catch (Exception e) {
           e.printStackTrace();
       }
   }
}

class MyMenuFrameTest {
   public static void main(String[] args) {
       MyMenuFrame frame = new MyMenuFrame();
       frame.setTitle("MyNotepad");
       //for the title of the box
       frame.setSize(600, 400);
       //for the size of the box
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       frame.setVisible(true);
   }
}

Add a comment
Know the answer?
Add Answer to:
MyMenuFrame.java, MyMenuFrameTest.java, a. The title of the frame is “MyNotepad”. b. Create and add border layout....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Simple Java Notepad application using Swing GUI Create a help menu with shortcut ctrl+H: -Help me...

    Simple Java Notepad application using Swing GUI Create a help menu with shortcut ctrl+H: -Help menu includes two menu items: 1. About 2. Visit Homepage. Add a separator between these menu items. 1. Create a menu item which is About. Add a short cut for the menu item. It is ctrl+A. -When a user clicks it (an action event occurs), display a show message dialog box. Display the message shown in the figure. Display information icon. 2. Create a menu...

  • • Add a menu bar to the program with a File menu. • In the File...

    • Add a menu bar to the program with a File menu. • In the File menu, add a submenu (JMenuItem) called About. • When the user clicks on the About menu item, display a JOptionPane message dialog that contains your name, your course, the section number, and ID. Add comments to the top of each source code file that includes your name, course number, section number, and ID. 2. In Chapter 5, you created a lottery game application. Create...

  • Subject: Advance application development Visual studio exercise The project is to create a simple Notepad style Tex...

    Subject: Advance application development Visual studio exercise The project is to create a simple Notepad style Text Editor. This will demonstrate the C# language basics. user interface controls and how to handle user events. Controls can be found in the Toolbox pane in its default location on the left side of the IDE, with the Control Properties pane on the right side of the IDE. Include the items on the following list in the program Create a C# Windows Forms...

  • Help to add .css file to this code that will add title color, table border color,...

    Help to add .css file to this code that will add title color, table border color, blue color to top (row1) blue color, (row2,3, and 4) light grey color; column 1 and column 3 row 1 blue color; column 1 and column 3 rows 2,3,and 4 red color. text inside table and caption is white color. <!-- *** Table Start *** --> <table border="1" width="400" align = "center" style="margin-top: 40px;" cellpadding="5" cellspacing="0" summary="A description of the specialty coffee menu items...

  • Need help on following Java GUI problem: Write a program that lets a user display and...

    Need help on following Java GUI problem: Write a program that lets a user display and modify pictures. Create a window. Add four buttons so that clicking a particular button will shift the image by a small amount in the north, south, east or west direction inside the window. Add a menu bar with two menus: File and Image. The File menu should contain an Open menu item that the user can select to display JPEG and PNG files from...

  • could you please help me with this problem, also I need a little text so I...

    could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...

  • Java language (a) Create a class HexEditor with a constructor which creates a 5x10 text area...

    Java language (a) Create a class HexEditor with a constructor which creates a 5x10 text area in a Frame. Also add a pull-down menu with a menu item "Load". Copy the class as the answer to this part. (b) Create another class TestHexEditor with a main() method which creates an object anEditor of the class HexEditor and displays the frame in part (a) using setVisible(true). Copy the class as the answer to this part. (c) Make changes to the class...

  • JAVA Hello I am trying to add a menu to my Java code if someone can...

    JAVA Hello I am trying to add a menu to my Java code if someone can help me I would really appreacite it thank you. I found a java menu code but I dont know how to incorporate it to my code this is the java menu code that i found. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ButtonGroup; import javax.swing.JCheckBoxMenuItem; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JRadioButtonMenuItem; public class MenuExp extends JFrame { public MenuExp() { setTitle("Menu Example");...

  • *Use Java to create this program* For this assignment, you will be building a Favorite Songs...

    *Use Java to create this program* For this assignment, you will be building a Favorite Songs application. The application should have a list with the items displayed, a textbox for adding new items to the list, and four buttons: Add, Remove, Load, and Save. The Add button takes the contents of the text field (textbox) and adds the item in it to the list. Your code must trim the whitespace in front of or at the end of the input...

  • Python Subject Output in Thonny ot IDLE Description Create a program that keeps track of the...

    Python Subject Output in Thonny ot IDLE Description Create a program that keeps track of the items that a wizard can carry. Sample Output (Your output should be similar to the text in the following box) The Wizard Inventory program COMMAND MENU walk - Walk down the path show - Show all items drop - Drop an item exit - Exit program Command: walk While walking down a path, you see a scroll of uncursing. Do you want to grab...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT