Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

An Event Handler Code Question

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
When I run the following code, I get a runtime error saying it can't find main.
However, if I separate these two classes into 2 separate files, with class myCodeToHandleWinClose also having the import java.awt.event.*; at the top, and run FrameDemo, it runs fine and calls the WindowClosing method. Why doesn't it run as it appears below, with both classes in the same FrameDemo.java file?
import javax.swing.*;
import java.awt.event.*;
public class FrameDemo
{
public static void main(String[] args)
{
JFrame jframe = new JFrame("Example");
jframe.setSize(400,100);
jframe.setVisible(true);
myCodeToHandleWinClose m = new myCodeToHandleWinClose();
jframe.addWindowListener(m);
}
}
class myCodeToHandleWinClose implements WindowListener
{
public void windowClosing(WindowEvent e) {System.exit(0);}
public void windowClosed(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
}
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please do not cross post.
Continue the conversation here.
 
    Bookmark Topic Watch Topic
  • New Topic