Code Completion 11.103 Add listeners to menu
Add the menu items shown below.
Add a listener to the second menu item that turns the content pane yellow.
Here is a sample program output:
Complete the following code:
import javax.swing.JFrame; import javax.swing.JMenuBar; import javax.swing.JMenu; import javax.swing.JMenuItem; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class MenuFrame extends JFrame { private JMenuBar menuBar; private JMenu fileMenu; private JMenu editMenu; private JMenu aboutMenu; public MenuFrame() { menuBar = new JMenuBar(); fileMenu = new JMenu("File"); fileMenu.setMnemonic('F'); editMenu = new JMenu("Edit"); aboutMenu = new JMenu("About"); // TODO: Add menu items // TODO: Add listener to second menu item setJMenuBar(menuBar); menuBar.add(fileMenu); menuBar.add(editMenu); menuBar.add(aboutMenu); } }
The following class is used to check your work:
import javax.swing.JFrame; public class MenuFrameViewer { public static void main(String[] args) { MenuFrame frame = new MenuFrame(); frame.setBounds(50,50,400,200); frame.setTitle("MenuFrameViewer"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
Expert Answer
import javax.swing.JFrame;
public class MenuFrameViewer { //Begin Class
public static void main(String[] args) { //Begin Main
MenuFrame frame = new MenuFrame();
frame.setBounds(50,50,400,200);
frame.setTitle(“MenuFrameViewer”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
} //End Main
} //End Class
MenuFrame.java
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
public class MenuFrame extends JFrame { //Begin Class
private final JMenuBar menuBar;
private final JMenu fileMenu;
private final JMenu editMenu;
private final JMenu aboutMenu;
public MenuFrame() { //Begin Constructor
menuBar = new JMenuBar();
fileMenu = new JMenu(“File”);
JMenuItem openFile = new JMenuItem(“Open File”);
JMenuItem turnBGYellow = new JMenuItem(“Turn Background Yellow”);
fileMenu.add(openFile);
fileMenu.add(turnBGYellow);
openFile.addActionListener((ActionEvent ae) -> {
JFileChooser openMenu = new JFileChooser();
File fileOpen = new File(“”);
openMenu.setCurrentDirectory(new File(“C:\”));
openMenu.setDialogTitle(“Open a File”);
if (openMenu.showOpenDialog(openFile)
== JFileChooser.APPROVE_OPTION) {
fileOpen = openMenu.getSelectedFile();
}
//prints out the link or path users clicked
System.out.println(openMenu.getSelectedFile().getAbsolutePath());
} //End Method
);
turnBGYellow.addActionListener((ActionEvent ae) -> {
getContentPane().setBackground(Color.YELLOW); //Whatever color
});
fileMenu.setMnemonic(‘F’);
editMenu = new JMenu(“Edit”);
aboutMenu = new JMenu(“About”);
// TODO: Add menu items
// TODO: Add listener to second menu item
setJMenuBar(menuBar);
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(aboutMenu);
} //End Constructor
} //End Class
OUTPUT: