본문 바로가기
IT/Java

awt 기본(화면 띄우기)

by Jeami 2013. 8. 7.
반응형



awt 를 이용한 팝업창 띄우기

package day06;


import java.awt.*;

import java.awt.event.*;



public class Note extends Frame implements WindowListener{

public Note(){

this.setBackground(Color.green);

this.setBounds(300,300,500,450);

this.setVisible(true);

/* 아래의 this.addWindowListener(this);

앞의 this는 생략가능(현재 클래스를 의미). 

뒤의 this는 윈도우리스너를 상속받는 Note를 인자로 받아야 하는데 

Note가 윈도우리스너를 상속받으므로 현재 클래스인 this를 인자값으로 넣어준 것.

앞의 this와 구별하기 용이하도록 표시해둔 것이기에 생략 가능 vs 뒤에 this는 필수 인자값이므로

  반드시 필요한 것입니다.

<<<<<<<<< 꼭 구분해주세요. >>>>>>>>>>

*/

this.addWindowListener(this);

}//end

public class MyExit extends WindowAdapter{

}//end


public static void main(String[] args){

Note ob = new Note() ;

try{ Thread.sleep(2000) ; }catch(Exception ex){}

WindowEvent we = null ;

ob.windowClosing(we) ;

}//end

public void windowActivated(WindowEvent arg0) { }

public void windowClosed(WindowEvent arg0) { }

public void windowClosing(WindowEvent arg0) { System.exit(1) ;}

public void windowDeactivated(WindowEvent arg0) { }

public void windowDeiconified(WindowEvent arg0) { }

public void windowIconified(WindowEvent arg0) { }

public void windowOpened(WindowEvent arg0) { }

}//class END





추천은 블로거에게 큰 힘이 됩니다(로그인 불필요) 양질의 정보로 꼭 보답할게요^^





또 다른 방법으로 내부클래스를 이용한 방법입니다.

WindowsListener 를 implements 하지 않아도 실행시킬 수 있는 방법인데요.

이렇게 하는 편이 코드는 간단합니다. 하지만 내부 클래스나 익명의 객체라는 등의 개념을 알아야 하기 때문에 조금더 까다로울 수 있겠죠..

package day06;

import java.awt.*;

import java.awt.event.*;


public class Note extends Frame implements WindowListener{

public Note(){

this.setBackground(Color.green);

this.setBounds(300,300,500,450);

this.setVisible(true);

this.addWindowListener( new MyExit() ); //AnonymoseObject : 익명의 객체

}//end


public class MyExit extends WindowAdapter{  //WindowAdapter 는 WindoListener 를 상속한다.

public void windowClosing(WindowEvent arg0) { System.exit(1) ;}

}//내부 클래스

public static void main(String[] args){

new Note() ;

}//end

}//class END






3번째 방법인데요. 코드상으로 가장 간단한 방법아긴 한데, 이 역시 기본 개념을 이해하고 있어야만 활용할 수 있는 로직인 것 같습니다. 

this.addWindowListener(); 의 인자값으로 WIndowAdapter(){ } 메소드를 가져오는 방법입니다.

메소드 구성은 아래와 같습니다.

WindowsAdapter(){

public void windowClosing(WindowEvent arg0) {

System.exit(1);

}


최종적으로 완성된 코드는 아래와 같습니다.

package day06;

import java.awt.*;

import java.awt.event.*;


public class Note extends Frame implements WindowListener{

public Note(){

this.setBackground(Color.green);

this.setBounds(300,300,500,450);

this.setVisible(true);

this.addWindowListener( new WindowAdapter() {

public void windowClosing(WindowEvent arg0) { System.exit(1) ;}

} );

}//end


public static void main(String[] args){

new Note() ;

}//end

}//class END



반응형

'IT > Java' 카테고리의 다른 글

두더지 게임  (0) 2013.08.16
Thread(스레드) - 시간 출력  (0) 2013.08.07
Interface(인터페이스)  (0) 2013.07.01
Java의 Generic(제너릭) 원리(간단정리)  (0) 2013.06.25
Generic 제너릭  (0) 2013.06.25

loading