--This article was written by * Qiita beginners *. --Maybe it's common among advanced programmers ... ――There is a slight sense of memorandum
setDefaultCloseOperation(int operation) This function allows you to set how the window is closed. (When you press the x button in the window) You can change the processing pattern by putting the JFrame constant in the argument.
DO_NOTHING_ON_CLOSE
--Nothing is done, the termination process is done by WindowClosing () of WindowListener class registered in JFrame.HIDE_ON_CLOSE
--Hide the frame after executing WindowClosing () above.DISPOSE_ON_CLOSE
--Hide and discard frames after running WindowClosing () above.EXIT_ON_CLOSE
--Exit using the java standard library System.exit ().This program takes DO_NOTHING_ON_CLOSE as an argument and describes the termination process by itself. Using this method, you can describe the process of saving game data. ← ~~ Then you can use DISPOSE_ON_CLOSE ~~
WindowTest.java
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
public class WindowTest extends JFrame{
public WindowTest(String title,int width,int height){
super(title);
setSize(width,height);
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new MyWindowListener());
setResizable(false);
requestFocus();
setVisible(true);
}
public static void main(String args[]){
new WindowTest("test",200,300);
}
}
class MyWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.out.println("Execute the termination process");
System.exit(0);
}
}
Recommended Posts