Search code examples
javaeventsawtframe

Frame Listener Java


why when I go to call the windowsClosing method (WindowEvent e) I get a "java: cannot find symbol" error?

import java.awt.*;
import java.awt.event.*;

public class Finestra implements WindowListener {

    public void windowClosing(WindowEvent e) {
        e.getWindow().dispose();
    }

    public void windowClosed(WindowEvent e) {
        System.exit(0);
    }

    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) {
    }
}

Class Main:

import java.awt.*;
import java.awt.event.*;

public class main {
    public static void main(String args[]) {
        Grafica g = new Grafica();
        Finestra f1 = new Finestra();
        f1.windowClosing(WindowEvent e);
    }
}

The error is given by the main at the time of execution to the line where the WindowsClosing method is called


Solution

  • I think this will help you.. Finestra.java

    import java.awt.*;
    import java.awt.event.*;
    
    public class Finestra extends Frame implements WindowListener {
    
    Finestra(){
        addWindowListener(this);
    
        setSize(400,400);
        setLayout(null);
        setVisible(true);
    }
    public void windowClosing(WindowEvent e) {
        System.out.println("closing");
        e.getWindow().dispose();
    }
    
    public void windowClosed(WindowEvent e) {
        System.exit(0);
    }
    
    public void windowOpened(WindowEvent e) {
        System.out.println("hello");
    }
    
    public void windowIconified(WindowEvent e) {
    }
    
    public void windowDeiconified(WindowEvent e) {
    }
    
    public void windowActivated(WindowEvent e) {
    }
    
    public void windowDeactivated(WindowEvent e) {
    }
    

    }

    Main.java

    public class Main {
    public static void main(String args[]) {
      new Finestra();
    
    }
    

    }

    I don't understand the Grafica class you used but if you want to use it for frame you can extend frame in Finestra class! I hope it will help you!