WindowEvent WindowEvent 윈도우가열리거나 (opened) 닫힐때 (closed) 활성화되거나 (activated) 비활성화될때 (deactivated) 최소화되거나 (iconified) 복귀될때 (deiconified) 윈도우닫힘버튼을누를때 (closing)
WindowEvent 수신자 abstract class WindowListener { abstract void windowactivated(windowevent e); abstract void windowdeactivated(windowevent e); abstract void windowiconified(windowevent e); abstract void windowdeiconified(windowevent e); abstract void windowopened(windowevent e); abstract void windowclosed(windowevent e); abstract void windowclosing(windowevent e);
WindowEvent 수신자코드구현 Class MyListener implements WindowListener { public void windowactivated(windowevent e) { public void windowdeactivated(windowevent e) { public void windowiconified(windowevent e) { public void windowdeiconified(windowevent e) { public void windowopened(windowevent e) { public void windowclosed(windowevent e) { public void windowclosing(windowevent e) {
WindowEvent 수신자등록하기 void addwindowlistener(windowlistener listener) class MyFrame extends JFrame { MyFrame() { addwindowlistener(new MyListener()); class MyListener implements WindowListener {
WindowEvent 예제 : 윈도우닫힘버튼의처리 windowclosing() 메소드에서 System.exit() 사용 JFrame.setDefaultCloseOperation() 메소드사용 (JDK 1.4 이후 ) EXIT_ON_CLOSE DO_NOTHING_ON_CLOSE HIDE_ON_CLOSE (default) DISPOSE_ON_CLOSE 종료전재확인하는방법
WindowListener vs. WindowAdapter WindowListener 인터페이스문제점 7가지의메소드를모두구현해야 windowclosing() 을제외한 6가지는사실상거의사용되지않는다. 사용하지않아도빈코드로구현은해야 불편! 어댑터클래스 인터페이스의모든메소드를빈코드로구현 실제로사용되는메소드는치환 (overriding) 으로코드변경
WindowListener vs. WindowAdapter WindowAdapter 클래스 : 빈코드로메소드구현! class WindowAdapter implements WindowListener { public void windowactivated(windowevent e) { public void windowdeactivated(windowevent e) { public void windowiconified(windowevent e) { public void windowdeiconified(windowevent e) { public void windowopened(windowevent e) { public void windowclosed(windowevent e) { public void windowclosing(windowevent e) {
WindowListener vs. WindowAdapter MyListener WindowListener 를 implement 하든지, (7 개의메소드모두 ) WindowAdapter 를상속받든지 ( 필요한메소드만치환 ) class MyListener implements WindowListener { 7 가지메소드모두코드구현 또는, class MyListener extends WindowAdapter { 필요한메소드만치환
WindowAdapter 사용시자기수신자로만들기어려움 class MyFrame extends JFrame { MyFrame() { add(new MyListener()); class MyListener extends WindowAdapter { WindowAdapter class MyFrame extends JFrame, WindowAdapter { // Error! 내부수신자또는익명의내부수신자로실현!
MouseEvent MouseEvent 마우스를움직이거나클릭할때발생 수신자 (1) abstract class MouseListener { abstract void mousepressed(mouseevent e); abstract void mousereleased(mouseevent e); abstract void mouseclicked(mouseevent e); abstract void mouseentered(mouseevent e); abstract void mouseexited(mouseevent e); 수신자 (1) 등록하기 void addmouselistener(mouselistener listener)
MouseEvent 수신자의구현 abstract class MouseListener { abstract void mousepressed(mouseevent e); abstract void mousereleased(mouseevent e); abstract void mouseclicked(mouseevent e); abstract void mouseentered(mouseevent e); abstract void mouseexited(mouseevent e); 메소드선언만있을뿐코드가없다 (interface) 코드구현 (implement) 하기 class MyListener implements MouseListener { public void mouseclicked(mouseevent e) { ( 코드 ) ( 이하생략 )
MouseEvent MouseEvent Pressed / Released / Clicked / Entered / Exited 예제 : 패널에점찍기
MouseListener vs. MouseAdapter MyListener MouseListener 를 implement 하든지, (5 개의메소드모두 ) MouseAdapter 를상속받든지 ( 필요한메소드만치환 ) class MyListener implements MouseListener { 5 가지메소드모두코드구현 또는, class MyListener extends MouseAdapter { 필요한메소드만치환
MouseEvent 수신자 (2) 개별공부 abstract class MouseMotionListener { abstract void mousedragged(mouseevent e); abstract void mousemoved(mouseevent e); 수신자 (2) 등록하기 void addmousemotionlistener(mousemotionlistener listener) 수신자의구현 class MyListener implements MouseMotionListener { ( 코드 )
메뉴 메뉴관련클래스 JMenuBar: 메뉴바 JMenu: 메뉴 JMenuItem: 메뉴항목
메뉴 JMenuBar 생성자 : JMenuBar() 메뉴바에메뉴붙이기 : JMenuBar.add(JMenu menu) 프레임에메뉴바붙이기 : JFrame.setJMenuBar(JMenuBar mb) JMenu 생성자 : JMenu(String name) 메뉴에메뉴항목붙이기 : JMenu.add(JMenuItem item) 메뉴에구분선붙이기 : JMenu.addSeparator()
메뉴 JMenuItem 생성자 : JMenuItem(String name) 메뉴항목은 ActionEvent 를발생시킨다. 예제 void JMenuItem.addActionListener(ActionListener)