Answer:
See explaination
Step-by-step explanation:
/ Include the required packages.
import java.io.File;
import javax.swing.*;
import java.awt.event.*;
import java.awt.BorderLayout;
// Define a class Mygui and extends the properties of JFrame
public class Mygui extends JFrame
{
// Create an object menu of JMenu, and menuBar of
// JMenuBar.
JMenu menu;
JMenuBar menuBar;
// Create two objects item1 and item2 of JMenuItem
// open and exit menu item.
JMenuItem item1, item2;
// Start a constructor of Mygui class.
Mygui()
{
// Create an object jf of JFrame and set the title
// as "Car GUI".
JFrame jf=new JFrame("Car GUI");
// Create an object bar of JMenuBar.
JMenuBar bar=new JMenuBar();
// Set the JMenu as File.
menu = new JMenu("File");
// Create an object fileMenu of class
// FileMenuHandler to handle the events of file.
FileMenuHandler fileMenu=new FileMenuHandler(this);
// Set the item1 of JMenuItem to Open.
item1=new JMenuItem("Open");
// Call the ActionListener of Open.
item1.addActionListener(fileMenu);
// Add the Open to the menu.
menu.add(item1);
// Set the item2 of JMenuItem to Exit.
item2=new JMenuItem("Exit");
// Call the ActionListener of Exit.
item2.addActionListener(fileMenu);
// Add the Exit to the menu.
menu.add(item2);
// Add the bar to the menu.
bar.add(menu);
// Add the bar to the frame.
jf.setJMenuBar(bar);
// Set the size and layout of the gui window.
jf.setSize(500,500);
jf.setLayout(null);
// setDefaultCloseOperation(EXIT_ON_CLOSE) used to
// quit the window, when the close sign is clicked.
jf.setDefaultCloseOperation(EXIT_ON_CLOSE);
// Set the Visibility of the frame.
jf.setVisible(true);
}
// Define the main() method.
public static void main(String args[])
{
// Declare an object of class Mygui.
Mygui mygui = new Mygui();
mygui.setVisible(true);
}
}
// Define FileMenuHandler class to handle the events of
// Car GUI.
class FileMenuHandler implements ActionListener
{
// Define an object of JFrame as jf.
JFrame jf;
// Define a constructor of class FileMenuHandler.
FileMenuHandler (JFrame jframe)
{
jf = jframe;
}
// Define a actionPerformed method to do some work
// when menu options are being clicked,
public void actionPerformed(ActionEvent event)
{
// Declare a string variable menuname to get the
// command chosen.
String menuname = event.getActionCommand();
//If the menuname is Open then call the open() method.
if (menuname.equals("Open"))
open();
// Otherwise exit the program.
else if (menuname.equals("Exit"))
System.exit(0);
}
void open()
{
int flag;
// Create an object jfc of JFileChooser.
JFileChooser jfc = new JFileChooser( );
JPanel gui = new JPanel(new BorderLayout());
flag = jfc.showOpenDialog(null);
// If chosen file exists then read and display the file.
if (flag == JFileChooser.APPROVE_OPTION)
{
// Display the selected file on the command prompt.
File fileSelected = jfc.getSelectedFile();
System.out.println("File selected: " + fileSelected.getAbsolutePath());
}
else
// If not able to open the file, display file failed to open.
JOptionPane.showMessageDialog(null, "Open File dialog canceled");
}
}