How to use setDefaultCloseOperation () of JFrame

at first

--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.

Sample program

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

How to use setDefaultCloseOperation () of JFrame
[Java] [Maven3] Summary of how to use Maven3
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use JUnit 5
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
Output of how to use the slice method
How to use enum (introduction of Japanese notation)
How to use JQuery in js.erb of Rails6
[Java] How to use Map
How to use Chain API
[Java] How to use Map
How to use Priority Queuing
[Rails] How to use enum
How to use java Optional
How to use Ruby return
[Rails] How to use enum
How to use @Builder (Lombok)
How to use java class
How to use Swift UIScrollView
[Java] How to use Optional ②
[Java] How to use removeAll ()
How to use String [] args
[Java] How to use string.format
How to use rails join
How to use Java Map
Ruby: How to use cookies
How to use dependent :: destroy
How to use Eclipse Debug_Shell
How to use Apache POI
[Rails] How to use validation
How to use Java variables
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
How to use GC Viewer
[Java] How to use Optional ①
How to use Lombok now
[Creating] How to use JUnit
[Rails] How to use Scope
[Java] How to use compareTo method of Date class
How to use CommandLineRunner in Spring Batch of Spring Boot
Summary of Java communication API (1) How to use Socket