What is wring with the following cade? class ExitListener implements ActionListe
ID: 3694483 • Letter: W
Question
What is wring with the following cade?
class ExitListener implements ActionListener
{
public void actionPerformed(AcionEvent event)
{
System.ext(0);
}
}
}
ActionListener exitListener = new ExitListener();
JMenu exitMenu = new JMenu("Exit");
exitMenu.addActionListener(exitListener);
JMenuBar menuBar = new JMenuBar();
menuBar.add(exitMenu);
a) You cannot add a Jmenu to the Jmenu bar
b) The event paramater in the actionPerformed method must be used.
c) The ExitListener class is not public
d) You cannot attach a listener to a Jmenu
Explanation / Answer
a) One is not error because we can add JMenu to JMenuBar
b) yes, this can be error, If we want to do something on specific event, then we need to use event
c) yes, this can be error, if both ExitLisner and JMenu code is not in the same package. When no explicit scope is defined for a class, then that class is available to that package only
d) We can attach listner to JMenu JMenu is extend of JMenuItem, which extends Abstractbutton, which contains addListner method with ActionLister as parameter
--see java doc for referenc------------------https://docs.oracle.com/javase/7/docs/api/javax/swing/JMenu.html
I tested the above code for compilation errors, but it compiled without errorts
package assignment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
public class JMenuActionListnerTesting {
public static void main(String[] args) {
ActionListener exitListener = new ExitListener();
JMenu exitMenu = new JMenu("Exit");
exitMenu.addActionListener(exitListener);
JMenuBar menuBar = new JMenuBar();
menuBar.add(exitMenu);
}
}
class ExitListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.