JAVA Programming 1
2 (Event Driven Programming)! :,,,! ( )! : (batch programming)!! ( : )!!!!
3 (Mouse Event, Action Event) (Mouse Event, Action Event) (Mouse Event, Container Event) (Key Event) (Key Event, Action Event)
4 GUI
5! GUI! ),,,,!!!,!
6
7?!,!
8!!!!!!!!!, ActionEvent : MouseEvent :,, ItemEvent :! Object EventObject.getSource() Object
9
10
(Event Listener) 11?!, JDK (interface)!! ) ActionListener interface ActionListener { // public void actionperformed(actionevent e); // Action! ) MouseListener interface MouseListener { // 5 public void mousepressed(mouseevent e); // public void mousereleased(mouseevent e); // public void mouseclicked(mouseevent e); // public void mouseentered(mouseevent e); // public void mouseexited(mouseevent e); //
12
Tip : addxxxlistener? 13!. JButton.addActionListener(); // Action JButton.addKeyListener(); // Key JButton.addFocusListener(); // Focus!. JButton.addActionListener(new MyButtonListener1()); JButton.addActionListener(new MyButtonListener2()); JButton.addActionListener(new MyButtonListener3());,.
14 3 1. 2. (inner class) 3. (anonymous class)
10-1 : Action import javax.swing.*; import java.awt.event.*; import java.awt.*; 15 MyActionListener.java public class IndepClassListener extends JFrame { public IndepClassListener() { settitle( Action ); setdefaultcloseoperation(jframe.exit_on_close); Container c = getcontentpane(); c.setlayout(new FlowLayout()); Jbutton btn = new Jbutton( Action ); btn.addactionlistener(new MyActionListener()); // Action c.add(btn); setsize(350, 150); setvisible(true); public static void main(string [] args) { new IndepClassListener(); class MyActionListener implements ActionListener { public void actionperformed(actionevent e) { JButton b = (JButton)e.getSource(); if(b.gettext().equals("action")) b.settext(""); else b.settext("action");
10-2 : Action import javax.swing.*; import java.awt.event.*; import java.awt.*; public class InnerClassListener extends JFrame { InnerClassListener() { settitle("action "); setdefaultcloseoperation(jframe.exit_on_close); Container c = getcontentpane(); c.setlayout(new FlowLayout()); JButton btn = new JButton("Action"); btn.addactionlistener(new MyActionListener()); c.add(btn); 16 Action private InnerClassListener InnerClassListener setsize(350, 150); setvisible(true); private class MyActionListener implements ActionListener { private public class void actionperformed(actionevent MyActionListener implements e) { ActionListener { public JButton void b actionperformed(actionevent = (JButton)e.getSource(); e) { JButton if(b.gettext().equals("action")) b = (JButton)e.getSource(); if(b.gettext().equals("action")) b.settext(""); else b.settext(""); elseb.settext("action"); // InnerClassListener b.settext("action"); JFrame // InnerClassListener.this.setTitle(b.getText()); InnerClassListener JFrame // InnerClassListener.this.setTitle(b.getText()); //.. public static void main(string [] args) { new InnerClassListener();
17?! ( + ) new ( ) {...... ;! ActionListener
10-3 : Action import java.awt.*; import java.awt.event.*; import javax.swing.*; public class AnonymousClassListener extends JFrame { public AnonymousClassListener() { settitle("action "); setdefaultcloseoperation(jframe.exit_on_close); Container c = getcontentpane(); c.setlayout(new FlowLayout()); JButton btn = new JButton("Action"); c.add(btn); Action AnonymousClassListener JFrame btn.addactionlistener(new ActionListener() { btn.addactionlistener(new public void actionperformed(actionevent ActionListener() e) { { public JButton void b = actionperformed(actionevent (JButton)e.getSource(); e) { if(b.gettext().equals("action")) JButton b = (JButton)e.getSource(); if(b.gettext().equals("action")) b.settext(""); else b.settext(""); else b.settext("action"); settitle(b.gettext()); b.settext("action"); settitle(b.gettext()); ); ); setsize(350, 150); setvisible(true); public static void main(string [] args) { new AnonymousClassListener(); 18
10-4 : 19 "hello".
10-4 20 import javax.swing.*; import java.awt.event.*; import java.awt.*; public class MouseListenerEx extends JFrame { private JLabel la = new JLabel("Hello"); public MouseListenerEx() { settitle("mouse "); setdefaultcloseoperation(jframe.exit_on_close); Container c = getcontentpane(); c.addmouselistener(new MyMouseListener()); c.setlayout(null); la.setsize(50, 20); la.setlocation(30, 30); c.add(la); la("hello" ). class MyMouseListener implements MouseListener { public void mousepressed(mouseevent e) { int x = e.getx(); int y = e.gety(); la.setlocation(x, y); public void mousereleased(mouseevent e) { public void mouseclicked(mouseevent e) { public void mouseentered(mouseevent e) { public void mouseexited(mouseevent e) { public static void main(string [] args) { new MouseListenerEx(); setsize(250, 250); setvisible(true);
(Adapter) 21! (mousepressed()) 4! MouseAdapter class MouseAdapter implements MouseListener, MouseMotionListener, MouseWheelListener { public void mousepressed(mouseevent e) { public void mousereleased(mouseevent e) { public void mouseclicked(mouseevent e) { public void mouseentered(mouseevent e) { public void mouseexited(mouseevent e) { public void mousedragged(mouseevent e) { public void mousemoved(mouseevent e) { public void mousewheelmoved(mousewheelevent e) {! ActionAdapter, ItemAdapter
22 JDK
(MouseAdapter) 23 JLabel la; contentpane.addmouselistener(new MyMouseListener());.. class MyMouseListener implements MouseListener { public void mousepressed(mouseevent e) { int x = e.getx(); int y = e.gety(); la.setlocation(x, y); public void mousereleased(mouseevent e) { public void mouseclicked(mouseevent e) { public void mouseentered(mouseevent e) { public void mouseexited(mouseevent e) { JLabel la; contentpane.addmouselistener(new MyMouseAdapter());.. class MyMouseAdapter extends MouseAdapter { public void mousepressed(mouseevent e) { int x = e.getx(); int y = e.gety(); la.setlocation(x, y); MouseAdapter MouseListener
10-5 : MouseAdapter 24 MouseAdapter 10-4. import java.awt.*; import java.awt.event.*; import javax.swing.*;] public class MouseAdapterEx extends JFrame { private JLabel la = new JLabel("Hello"); public MouseAdapterEx() { settitle("mouse "); setdefaultcloseoperation(jframe.exit_on_close); Container c = getcontentpane(); c.addmouselistener(new MyMouseAdapter()); class MyMouseAdapter extends MouseAdapter { public void mousepressed(mouseevent e) { int x = e.getx(); int y = e.gety(); la.setlocation(x, y); public static void main(string [] args) { new MouseAdapterEx(); c.setlayout(null); la.setsize(50, 20); la.setlocation(30, 30); c.add(la); setsize(250, 250); setvisible(true);
Key 25, Key!!! (Unicode )!, (focus) (focus)!! : 2 component.setfocusable(true); // component component.requestfocus(); // componen component.setfocusable(true);
26! JFrame setvisible(true) setvisible(true); // component.setfocusable(true); component.requestfocus();! component.addmouselistener(new MouseAdapter() { public void mouseclicked(mouseevent e) { Component c = (Component)e.getSource(); // c.setfocusable(true); c.requestfocus(); ); // 10-8
KeyListener 27 KeyListener 3 ❶ (KeyListener) void keypressed(keyevent e) { //, Unicode ❸ ❷ void keyreleased(keyevent e) { // void keytyped(keyevent e) { // KeyListener ❶ ❷ ❸ component.addkeylistener(mykeylistener);
2 28 1.!! A~Z, a~z, 0~9,!, @, &! keypressed(), keytyped(), keyreleased() 2.! ( ) <Function>, <Home>, <Up>, <Delete>, <Control>, <Shift>, <Alt>!! ( )! keypressed(), keyreleased()!
(Virtual Key) 29 KeyEvent
30 : KeyEvent KeyEvent 1. (), char KeyEvent.getKeyChar()! ()! 2., int KeyEvent.getKeyCode()!! (Virtual Key) KeyEvent 3. String KeyEvent.getKeyText(int keycode)! Static! keycode ( ) F1 "F1", Shift "SHIFT"
31 KeyEvent getkeychar() getkeycode()
10-6 : KeyEvent KeyListener 32 import javax.swing.*; import java.awt.event.*; import java.awt.*; public class KeyListenerEx extends JFrame { private JLabel [] keymessage; setsize(300,150); setvisible(true); c.setfocusable(true); c.requestfocus(); public KeyListenerEx() { settitle("keylistener "); setdefaultcloseoperation(jframe.exit_on_close); Container c = getcontentpane(); c.setlayout(new FlowLayout()); c.addkeylistener(new MyKeyListener()); keymessage = new JLabel [3]; keymessage[0] = new JLabel(" getkeycode() "); keymessage[1] = new JLabel(" getkeychar() "); keymessage[2] = new JLabel(" getkeytext() "); for(int i=0; i<keymessage.length; i++) { c.add(keymessage[i]); keymessage[i].setopaque(true); keymessage[i].setbackground(color.yellow); class MyKeyListener extends KeyAdapter { public void keypressed(keyevent e) { int keycode = e.getkeycode(); char keychar = e.getkeychar(); keymessage[0].settext(integer.tostring(keycode)); keymessage[1].settext(character.tostring(keychar)); keymessage[2].settext(e.getkeytext(keycode)); public static void main(string [] args) { new KeyListenerEx();
33 <Control> w <F1> 9 9 9 <Home> <Home> <Home>
10-7 : <F1>, % 34 import javax.swing.*; import java.awt.event.*; import java.awt.*; public class KeyCodeEx extends JFrame { private JLabel la = new JLabel(); public KeyCodeEx() { settitle("key Code : F1:, % "); setdefaultcloseoperation(jframe.exit_on_close); Container c = getcontentpane(); c.addkeylistener(new MyKeyListener()); c.add(la); class MyKeyListener extends KeyAdapter { public void keypressed(keyevent e) { la.settext(e.getkeytext(e.getkeycode())); if(e.getkeychar() == '%') contentpane.setbackground(color.yellow); else if(e.getkeycode() == KeyEvent.VK_F1) contentpane.setbackground(color.green); public static void main(string [] args) { new KeyCodeEx(); setsize(300,150); setvisible(true); c.setfocusable(true); c.requestfocus(); % e.getkeychar(). F1 e.getkeycode() KeyEvent.VK_F1
10-7 35 <F1> % % Shift+5, 5 5. % 5 if(e.getkeychar() == '%')
10-8 : (UP), (DOWN), (LEFT), (RIGHT) "HELLO" 36,,, "HELLO". "HELLO" JLabel,,, 10.. "HELLO" (50, 50). (50,50),,, 10 "HELLO",,,..
:,,, 37 import javax.swing.*; import java.awt.event.*; import java.awt.*; public class FlyingTextEx extends JFrame { private final int FLYING_UNIT = 10; private JLabel la = new JLabel("HELLO"); public FlyingTextEx() { settitle(",,, "); setdefaultcloseoperation(jframe.exit_on_close); Container c = getcontentpane(); c.setlayout(null); c.addkeylistener(new MyKeyListener()); la.setlocation(50,50); la.setsize(100,20); c.add(la); setsize(300,300); setvisible(true); c.setfocusable(true); c.requestfocus(); class MyKeyListener extends KeyAdapter { public void keypressed(keyevent e) { int keycode = e.getkeycode(); switch(keycode) { case KeyEvent.VK_UP: la.setlocation(la.getx(), la.gety()-flying_unit); break; case KeyEvent.VK_DOWN: la.setlocation(la.getx(), la.gety()+flying_unit); break; case KeyEvent.VK_LEFT: la.setlocation(la.getx()-flying_unit, la.gety()); break; case KeyEvent.VK_RIGHT: la.setlocation(la.getx()+flying_unit, la.gety()); break; c.addmouselistener(new MouseAdapter() { public void mouseclicked(mouseevent e) { Component com = (Component)e.getSource(); com.setfocusable(true); com.requestfocus(); ); public static void main(string [] args) { new FlyingTextEx();
38!, 8! mousepressed(), mousereleased(), mouseclicked()! mousepressed(), mousedragged(), mousedragged(),..., mousedragged(), mousereleased()
39 MouseListener 5 mouseentered(), mouseexited(), mousepressed(), mousereleased(), mouseclicked()! component.addmouselistener(mymouselistener); MouseMotionListener mousedragged(), mousemoved()! component.addmousemotionlistener(mymousemotionlistener);
MouseEvent 40! int getx(), int gety(),! Point getpoint() public void mousepressed(mouseevent e) { int x = e.getx(); int y = e.gety();! int getbutton() public void mousepressed(mouseevent e) { if(e.getbutton() == MouseEvent.BUTTON1) System.out.println("Left Button Pressed");! int getclickcount() public void mouseclicked(mouseevent e) { if(e.getclickcount() == 2) { //
10-9 : 41 class MyMouseListener implements MouseListener, MouseMotionListener { import javax.swing.*; import java.awt.event.*; import java.awt.*; public class MouseListenerAllEx extends JFrame { private JLabel la = new JLabel("No Mouse Event"); public MouseListenerAllEx() { settitle("mouselistener MouseMotionListener "); setdefaultcloseoperation(jframe.exit_on_close); Container c = getcontentpane(); c.setlayout(new FlowLayout()); MyMouseListener listener = new MyMouseListener(); c.addmouselistener(listener); c.addmousemotionlistener(listener); c.add(la); setsize(300,200); setvisible(true); public void mousepressed(mouseevent e) { la.settext("mousepressed ("+e.getx()+", "+e.gety ()+")"); public void mousereleased(mouseevent e) { la.settext("mousereleased("+e.getx()+", "+e.gety()+")"); public void mouseclicked(mouseevent e) { public void mouseentered(mouseevent e) { Component c = (Component)e.getSource(); c.setbackground(color.cyan); public void mouseexited(mouseevent e) { Component c = (Component)e.getSource(); c.setbackground(color.yellow); public void mousedragged(mouseevent e) { la.settext("mousedragged ("+e.getx()+","+e.gety()+")"); public void mousemoved(mouseevent e) { la.settext("mousemoved ("+e.getx()+","+e.gety()+")"); public static void main(string [] args) { new MouseListenerAllEx();
42 10-9
10-10 :. import javax.swing.*; import java.awt.event.*; import java.awt.*; public class ClickAndDoubleClickEx extends JFrame { public ClickAndDoubleClickEx() { settitle("click and DoubleClick "); setdefaultcloseoperation(jframe.exit_on_close); Container c = getcontentpane(); c.addmouselistener(new MyMouseListener()); setsize(300,200); setvisible(true); class MyMouseListener extends MouseAdapter { public void mouseclicked(mouseevent e) { if(e.getclickcount() == 2) { int r = (int)(math.random()*256); int g = (int)(math.random()*256); int b = (int)(math.random()*256); Component c = (Component)e.getSource(); c.setbackground(new Color(r,b,g)); public static void main(string [] args) { new ClickAndDoubleClickEx(); 43
MouseWheelEvent MouseWheelListener 44! MouseWheelEvent MouseWheelListener!! Public void mousewheelmoved(mousewheelevent e) component.addmousewheellistener(new MouseWheelListener() { public void mousewheelmoved(mousewheelevent e) { //. ); 10-7
45