Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
8.1 kB
1
Indexable
Never
Skip to main content

Expert Q&A

Find solutions to your homework

Your subscription will expire on June 8.

Change your mind? Turn on auto-renew to keep accessing solutions. 

Turn on auto-renew

Question

(0)

Code in Java, please

Question:

FileMenuHandler.java:

SampleGUI.java:

SampleMain.java:

Show transcribed data

Expert Answer

100% (2 ratings)

This solution was written by a subject matter expert. It's designed to help students like you learn core concepts.

Anonymous answered this668 answers

package Packk;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class FileMenuHandler implements ActionListener //define the class FileMenuHanlder that implements ActionListener
{
JFrame jframe; //declare variable of type JFrame
public FileMenuHandler(JFrame jf) //define constructor for that with JFrame type as a parameter
{
jframe=jf; //initialize jframe type variable
}

@Override
public void actionPerformed(ActionEvent evt) //handle action event here
{
String menuName=evt.getActionCommand(); //returns the commandString associated with menuitem
if(menuName.equalsIgnoreCase("Open")) //if open
{
JOptionPane.showMessageDialog(null, "You clicked Open"); //displays message "You clicked open"
}
else if(menuName.equalsIgnoreCase("Quit")) //if quit
{
JOptionPane.showMessageDialog(null, "You clicked Quit"); //displays message "You clicked quit"
}
}
}
Here i have defined FileMenuHandler class that implements ActionListener interface in which i have defined constructor for that class in that i am initializing the JFrame type variable. And i have also provided implementation for the actionPerformed() method in which i am getting the commandString associated with that menu item and i am checking whether it is equal to Find or Replace and displaying the message for the clicked menu item using JOptionPane.showMessageDialog() method.

package Packk;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class EditMenuHandler implements ActionListener //define the class EditMenuHandler that implements ActionListener
{
JFrame jframe; //declare variable of JFrame type
public EditMenuHandler(JFrame jf) //define constructor with JFrame type parameter
{
jframe=jf; //assign jframe type variable
}

@Override
public void actionPerformed(ActionEvent evt) //define actionPerformed() method
{
String menuName=evt.getActionCommand(); //returns the commandString associated with that menu item

if(menuName.equalsIgnoreCase("Find")) //if menuName equals "Find"
{
JOptionPane.showMessageDialog(null, "You clicked Find"); //display the message "You clicked Find"
}
else if(menuName.equalsIgnoreCase("Replace")) //if menuName equals "Replace"
{
JOptionPane.showMessageDialog(null, "You clicked Replace"); //displayes message "You clicked replace"
}
}
}
Here i have defined EditMenuHanlder that implements ActionListener and in the constructor of that i have initialized JFrame type variable. And i have provided implementation for actionPerformed() method in which i am getting the command string associated with that menu item and i am checking whether it is a Find menu item or it is a Replace menu item. And i am displaying the message using JOptionPane.showMessageDialog() method for the selected menu item.

package Packk;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class SampleGUI extends JFrame //define the class that extends JFrame class
{
public SampleGUI(String title,int height,int width) //define the constructor for that class
{
setTitle(title); //set the title
setSize(height,width); //set the size
setLocation(400, 200); //set the location
createFileMenu(); //call the createFileMenu() method
setDefaultCloseOperation(EXIT_ON_CLOSE); //call setDefaultCloseOperation() method
setVisible(true); //make frame visible
}

private void createFileMenu() //define createFileMenu() method
{
JMenuItem item; //declare JMenuItem
JMenuBar menuBar=new JMenuBar(); //create the MenuBar
JMenu fileMenu=new JMenu("File"); //create fileMenu
FileMenuHandler fmh=new FileMenuHandler(this); //create FileMenuHandler object

item=new JMenuItem("Open"); //create Open menuItem
item.addActionListener(fmh); //register actionListener with that menuItem
fileMenu.add(item); //add the item to menu

fileMenu.addSeparator(); //add horizontal seperator line

item=new JMenuItem("Quit"); //create Quit menuitem
item.addActionListener(fmh); //register actionListener
fileMenu.add(item); //add item to File menu

setJMenuBar(menuBar); //set the JMenuBar
menuBar.add(fileMenu); //add the fileMenu


JMenu editMenu=new JMenu("Edit"); //create the editMenu
EditMenuHandler emh=new EditMenuHandler(this); //create EditMenuHanlder

item=new JMenuItem("Find"); //create Find menu item
item.addActionListener(emh); //register actionListener
editMenu.add(item); //add item to editMenu

editMenu.addSeparator(); //add horizontal seperator line

item=new JMenuItem("Replace"); //create menuItem replace
item.addActionListener(emh); //register actionListener
editMenu.add(item); //add the item

menuBar.add(editMenu); //add editMenu to menuBar

}
}
Here i have defined SampleGUI class that extends JFrame class in which i have defined constructor with title,width,height as a parameter and in that i have called setTitle() method by passing title to set the title for frame and called setSize() method by passing height and width as a parameter. And i have also called setLocation() mehod to set the location for frame and i have called createFileMenu() method and called setDefaultCloseOperation() method to make the frame closable when the user closes that frame. And i have also called setVisible() method to make the frame visible.

And in the createFileMenu() in which i have created Menubar and i have created FileMenu and i am creating the FileMenuHandler() constructor by passing current object as a parameter. And i am adding open and quit menu items to that File menu and registered handler with that menu items to handle the action event. And then i have also created EditMenu and added the menu items to that editMenu and registered handler with that menu items Find and replace menu items. and After that i have added both menus to menubar.

package Packk;

public class SampleMain //define the Public class
{
static SampleGUI sampleGUI; //declare the SampleGUI variable
public static void main(String[] args) //define main() method
{
sampleGUI=new SampleGUI("My Sample GUI",500,300); //call the constructor
}
}

Here i have defined class SampleMain in which i have declared SampleGUI type variable sampleGUI and defined main() method in which i have called constructor of SampleGUI by passing "My Sample GUI" text as a parameter and at 500,300 location it displays the frame.

OUTPUT:

Was this answer helpful?

2

0

Post a question

Answers from our experts for your tough homework questions

14 questions left - Expires June 7, 2023

Post a question text

Continue to post

Questions viewed by other students

Q:

Code in Java, pleaseQuestion:FileMenuHandler.java:SampleGUI.java:SampleMain.java:

A:

See answer

100% (2 ratings)

Q:

We are completing this hw assignment using Python, thankyou very much for your help!This assignment is on object oriented programming. You will bedeveloping two classes that are fundamental in Geometry - Point andLine. In main() you will test the various functions that you havewritten for the classes.Here is the skeleton of the code that you will be writing. Youwill have to write the bodies of all the functions that I havelisted. You can always a...

A:

See answer

100% (1 rating)

Show more

My Textbook Solutions

Add textbook

Academic Integrity/Feedback/Help Center/Manage Subscription

© 2003-2023 Chegg Inc. All rights reserved.

Cookie NoticeYour Privacy ChoicesDO NOT SELL MY INFOGeneral PoliciesPrivacy PolicyHonor CodeIP Rights