Awt window does not close when click close

I implemented a class sample for Virtual KeyBoard and ran this VirtualKeyboardTest. The keyboard appears, but the main problem is that it does not close properly when you press the x button. How can i fix this?

import java.awt.*; import java.awt.event.*; public class VirtualKeyboardTest { public static void main(String args[]) { VirtualKeyboard vk = new VirtualKeyboard(); vk.setSize(500,300); vk.setVisible(true); Frame f1 = new Frame(); f1.addWindowListener( new WindowAdapter() { @Override public void windowClosing(WindowEvent we) { System.exit(0); } } ); } } 
+4
source share
3 answers

Invalid code. Instead

 f1.addWindowListener( new WindowAdapter() { ... 

to try

 vk.addWindowListener( new WindowAdapter() { ... 

This will close your window.

+5
source

Better to use the public void dispose () method

 vk.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { vk.dispose(); // use dispose method } } ); 

AWT - heavyweight, i.e. its components use system resources .

Windows does not block. This means that after creating the code in the code, the code continues to run.

This means that your window probably goes beyond the scope immediately after creation, unless you explicitly saved the link to it somewhere else. The window is still on the screen at this point.

It also means that you need another way to get rid of it when you are done with it. Enter the Window dispose () method, which can be called from one of the Window listeners.

+2
source

Check this:

 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

Basically, the window manager indicates that you are stopping your application when the "X" button is pressed.

0
source

Source: https://habr.com/ru/post/1437981/


All Articles