M.E.
M.E.

Reputation: 5495

How to handle close button in AWT Frame

I am starting with some basic Java2D examples -specifically under AWT-, following first examples of book "Introduction to Computer Graphics Java2D/Java3D" I have written the following two classes, one as "main" class:

package com.example.test;

public class Test {

    public static void main( String[] args ) {

            MyChart terminal = new MyChart();
            terminal.setTitle("The first Java 2D program");
            terminal.setSize(350,80);
            terminal.setVisible(true);

    }

}

And another one which extends AWT Frame:

package com.example.test;

import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class MyChart extends Frame {


        public void MyChart() {

            this.addWindowListener(new WindowAdapter(){  
                public void windowClosing(WindowEvent e) {  
                    dispose();  
                }  
            });

        }

        public void paint(Graphics g) {
            Graphics2D g2d =  (Graphics2D) g;
            g2d.drawString("Hello world!", 30, 50);
        };


}

This generates the expected "Hello World" window on the screen, but when I click on close button (Windows 10), nothing happens. I am trying to figure out what I did wrong while adding the listener without success.

Any tip or suggestion even to further debug is welcomed.

I am under Java: 1.8.0_191 and Windows 10 as mentioned.

Upvotes: 0

Views: 625

Answers (2)

M.E.
M.E.

Reputation: 5495

Constructor definition was wrong, void was present and must be removed.

Original code (WRONG constructor, uses void):

    public void MyChart() {

        this.addWindowListener(new WindowAdapter(){  
            public void windowClosing(WindowEvent e) {  
                dispose();  
            }  
        });

    }

Corrected code (without void):

public MyChart() {

        this.addWindowListener(new WindowAdapter(){  
            public void windowClosing(WindowEvent e) {  
                dispose();  
            }  
        });

}

Upvotes: 1

Talha Baig
Talha Baig

Reputation: 522

You weren't calling the MyChart function in your main class. below code is working fine.

public class Test {

        public static void main( String[] args ) {
            System.out.println("Terminal Group");
            MyChart terminal = new MyChart();
            terminal.MyChart();//I added this.
            terminal.setTitle("The first Java 2D program");
            terminal.setSize(350,80);
            terminal.setVisible(true);
    }

}

public class MyChart extends Frame {
        public void MyChart() {
            this.addWindowListener(new WindowAdapter(){  
                public void windowClosing(WindowEvent e) {  
               dispose();
               }  
            });

        }

        public void paint(Graphics g) {
            Graphics2D g2d =  (Graphics2D) g;
            g2d.drawString("Hello world!", 30, 50);
        };
}

Upvotes: 1

Related Questions