I'm trying to write code so that when a user clicks the 'File' tab and selects 'Exit', it exits the whole window I've built.
I am trying to use the dispose();
method, but it gives me the error
"The method dispose() is undefined for the type new ActionListener()"
Heres the code
public static void addLayouts(){
frame = new JFrame();
frame.setSize(600, 600);
frame.setTitle("IPFinder");
GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGap(0, 384, Short.MAX_VALUE)
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGap(0, 362, Short.MAX_VALUE)
);
frame.getContentPane().setLayout(groupLayout);
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu mnFile = new JMenu("File");
menuBar.add(mnFile);
JMenu mnEdit = new JMenu("Edit");
menuBar.add(mnEdit);
JMenuItem mntmCopyResults = new JMenuItem("Copy Results");
mnEdit.add(mntmCopyResults);
JMenu mnAbout = new JMenu("About");
menuBar.add(mnAbout);
JMenuItem mntmAboutIpfinder = new JMenuItem("About IPFinder");
mnAbout.add(mntmAboutIpfinder);
frame.setVisible(true);
JMenuItem mntmExit = new JMenuItem("Exit");
mntmExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
mnFile.add(mntmExit);
}
I speant about 2 hours on google trying to figure it but i can't get it to work.
I've exhausted all my options at this point so I came here. Any help is apreciated.
dispose()
should be called on JFrame
or JDialog
, and not on ActionListener
.
but it gives me the error "The method dispose() is undefined for the type new ActionListener()"
Exactly. ActionListener
has only one method actionPerformed()
.
http://docs.oracle.com/javase/6/docs/api/java/awt/event/ActionListener.html