본문 바로가기

이것저것 배운것/JAVA 공부중

JButton으로 종료시키기

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class BtnExitEx extends JFrame {

	private JButton btnExit;
	
	BtnExitEx()
	{
		setTitle("Button Exit Example");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 타이틀바에 X버튼 누르면 프로그램을 종료
		
		btnExit = new JButton("EXIT");
		btnExit.addActionListener(new ActionListener() {    // 버튼에 대한 이벤트 생성
			public void actionPerformed(ActionEvent e)        // 실행될 이벤트
			{
				System.exit(0);
			}
		});
	
	add(btnExit);
	
	setSize(300, 200);
	setVisible(true);
	setResizable(false);
	setLocationRelativeTo(null);
	}
	
	public static void main(String[] args)
	{
		new BtnExitEx();
	}
}