0

File is created using Project → New(Right Click) → JFrame Form

I set title as XYZ and I am trying to dispose this frame using a button on the frame using XYZ.dispose();

But actually not working

Suthar
  • 91
  • 2
  • 8

1 Answers1

1

When you create a new JFrame this way (the way you have mentioned), then frame.dispose() will not work as non-static method can't be referenced from a static context.

As you are closing the main frame not any internal frame,

For particularly disposing that frame:

this.dispose();

You can also do the exiting if you want, get the job done via:

System.exit(0);

For specifically disposing that frame, rather add one more instruction to your closing function with the code instruction:

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
/*an import required for this--> import javax.swing.WindowConstants;*/

This closes the main frame (not the same as disposing of internal frames!).

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433