๊ด€๋ฆฌ ๋ฉ”๋‰ด

IT’s Portfolio

[Java] Lecture - Day 13 ๋ณธ๋ฌธ

Development Study/Java

[Java] Lecture - Day 13

f1r3_r41n 2024. 3. 30. 00:05
728x90
๋ฐ˜์‘ํ˜•

๐Ÿ–ฅ Basic Java - Day 13

๋งˆ์šฐ์Šค๋กœ ๋ฌธ์ž์—ด ์ด๋™์‹œํ‚ค๊ธฐ

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class MouseEventEx extends JFrame {

    private JLabel l = new JLabel("hello");
    public MouseEventEx() {
        setTitle("Mouse Event Example");
        setSize(250, 250);

        Container c = getContentPane();
        c.addMouseListener(new MyMouseListener());
        c.setLayout(null);

        l.setSize(50, 20);
        l.setLocation(30, 30);
        c.add(l);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    class MyMouseListener implements MouseListener {
        @Override
        public void mouseClicked(MouseEvent e) {

        }

        @Override
        public void mousePressed(MouseEvent e) {
            int x = e.getX();
            int y = e.getY();
            l.setLocation(x, y);
        }

        @Override
        public void mouseReleased(MouseEvent e) {

        }

        @Override
        public void mouseEntered(MouseEvent e) {

        }

        @Override
        public void mouseExited(MouseEvent e) {

        }
    }

    public static void main(String[] args) {
        new MouseEventEx();
    }
}

์–ด๋Œ‘ํ„ฐ(Adapter) ํด๋ž˜์Šค

  • ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ๊ตฌํ˜„์— ๋”ฐ๋ฅธ ๋ถ€๋‹ด์„ ํ•ด์†Œํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ
    • ๋ฆฌ์Šค๋„ˆ ์ž‘์„ฑ ์‹œ ์ถ”์ƒ ๋ฉ”์†Œ๋“œ๋“ค์„ ๋ชจ๋‘ ๊ตฌํ˜„ํ•ด์•ผ ํ•˜๋Š” ๋ถ€๋‹ด์ด ์žˆ์Œ
  • ์–ด๋Œ‘ํ„ฐ ํด๋ž˜์Šค
    • ๋ฆฌ์Šค๋„ˆ์˜ ๋ชจ๋“  ๋ฉ”์†Œ๋“œ๊ฐ€ ๋‹จ์ˆœ ๋ฆฌํ„ดํ•˜๋„๋ก ๊ตฌํ˜„ํ•ด๋†“์€ ํด๋ž˜์Šค
  • ์ถ”์ƒ ๋ฉ”์†Œ๋“œ๊ฐ€ ํ•˜๋‚˜๋ฟ์ธ ๋ฆฌ์Šค๋„ˆ๋Š” ์–ด๋Œ‘ํ„ฐ ํด๋ž˜์Šค๊ฐ€ ์—†์Œ
    • ActionAdapter, ItemAdapter ํด๋ž˜์Šค๋Š” ์กด์žฌํ•˜์ง€ ์•Š์Œ
  • JDK์—์„œ ์ œ๊ณตํ•˜๋Š” ์–ด๋Œ‘ํ„ฐ ํด๋ž˜์Šค
    • ActionListener: ์—†์Œ
    • ItemListener: ์—†์Œ
    • KeyListener: KeyAdapter
    • MouseListener: MouseAdapter
    • MouseMotionListener: MouseMotionAdapter or MouseAdapter
    • FocusListener: FocusAdapter
    • WindowListener: WindowAdapter
    • AdjustmentListener: ์—†์Œ
    • ComponentListener: ComponentAdapter
    • ContainerListener: ContainerAdapter
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class MouseEventEx extends JFrame {

    private JLabel l = new JLabel("hello");
    public MouseEventEx() {
        setTitle("Mouse Event Example");
        setSize(250, 250);

        Container c = getContentPane();
        c.addMouseListener(new MyMouseListener());
        c.setLayout(null);

        l.setSize(50, 20);
        l.setLocation(30, 30);
        c.add(l);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    class MyMouseListener extends MouseAdapter {
        @Override
        public void mousePressed(MouseEvent e) {
            int x = e.getX();
            int y = e.getY();
            l.setLocation(x, y);
        }
    }

    public static void main(String[] args) {
        new MouseEventEx();
    }
}
  • ์–ด๋Œ‘ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐœ์„ ํ•œ ์ฝ”๋“œ

Key ์ด๋ฒคํŠธ์™€ Focus

  • ํ‚ค ์ž…๋ ฅ ์‹œ, ๋‹ค์Œ ์„ธ ๊ฒฝ์šฐ์— Key ์ด๋ฒคํŠธ ๋ฐœ์ƒ
    • ํ‚ค๋ฅผ ๋ˆ„๋ฅด๋Š” ์ˆœ๊ฐ„
    • ๋ˆ„๋ฅธ ํ‚ค๋ฅผ ๋–ผ๋Š” ์ˆœ๊ฐ„
    • ๋ˆ„๋ฅธ ํ‚ค๋ฅผ ๋–ผ๋Š” ์ˆœ๊ฐ„(Unicode ํ‚ค์˜ ๊ฒฝ์šฐ์—๋งŒ)
  • ํ‚ค ์ด๋ฒคํŠธ๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ์กฐ๊ฑด
    • ๋ชจ๋“  ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, ํ˜„์žฌ ํฌ์ปค์Šค๋ฅผ ๊ฐ€์ง„ ์ปดํฌ๋„ŒํŠธ
  • Focus
    • ์ปดํฌ๋„ŒํŠธ๋‚˜ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์ด ํ‚ค ์ด๋ฒคํŠธ๋ฅผ ๋…์ ํ•˜๋Š” ๊ถŒํ•œ
    • ์ปดํฌ๋„ŒํŠธ์— ํฌ์ปค์Šค ์„ค์ • ๋ฐฉ๋ฒ•
      • component.setFocusable(true); // component๊ฐ€ ํฌ์ปค์Šค๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •
        • ์ž๋ฐ” ํ”Œ๋žซํผ๋งˆ๋‹ค ์‹คํ–‰ ํ™˜๊ฒฝ์˜ ์ดˆ๊ธฐํ™”๊ฐ€ ์„œ๋กœ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๊ธฐ์— ํ•„์š”
      • component.requestFocus(); // component์— ํฌ์ปค์Šค ๊ฐ•์ œ ์ง€์ •
setVisible(true);
component.setFocusable(true);
component.requestFocus();
  • ์Šค์œ™ ํ”„๋ ˆ์ž„์ด ๋งŒ๋“ค์–ด์งˆ ๋•Œ, ํฌ์ปค์Šค๋ฅผ ์ฃผ๊ณ ์ž ํ•˜๋Š” ๊ฒฝ์šฐ
    • JFrame์˜ setVisible(true) ์ดํ›„์— ํฌ์ปค์Šค๋ฅผ ์ค˜์•ผ ํ•จ
component.addMouseListener(new MouseAdapter()) {
    public void mouseClicked(MouseEvent e) {
        Component c = (Component)e.getSource(); // ํด๋ฆญ๋œ ์ปดํฌ๋„ŒํŠธ
        c.setFocusable(true);
        c.requestFocus();
    }
}
  • ๋งˆ์šฐ์Šค๋กœ ์ปดํฌ๋„ŒํŠธ๋ฅผ ํด๋ฆญํ•  ๋•Œ, ํฌ์ปค์Šค ์ง€์ • ๋ฐฉ๋ฒ•
    • ์–ธ์ œ๋“ ์ง€ ํ•„์š”ํ•  ๋•Œ ํฌ์ปค์Šค๋ฅผ ์ค„ ์ˆ˜ ์žˆ์Œ

KeyListener์˜ ๋ฉ”์†Œ๋“œ์™€ ํ‚ค

  • ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์—์„œ KeyListener๋ฅผ ์ƒ์†๋ฐ›์•„ ํ‚ค ๋ฆฌ์Šค๋„ˆ๋ฅผ ๊ตฌํ˜„
  • KeyListener์˜ 3๊ฐœ ๋ฉ”์†Œ๋“œ
    • keyPressed : ํ‚ค๋ฅผ ๋ˆ„๋ฅด๋Š” ์ˆœ๊ฐ„
    • keyReleased : ๋ˆ„๋ฅธ ํ‚ค๋ฅผ ๋–ผ๋Š” ์ˆœ๊ฐ„
    • keyTyped : ๋ˆ„๋ฅธ ํ‚ค๋ฅผ ๋–ผ๋Š” ์ˆœ๊ฐ„(Unicode)
  • component.addKeyListener(myKeyListener);
    • ์ปดํฌ๋„ŒํŠธ์— ํ‚ค ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ๋“ฑ๋ก
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class MyKeyEventEx extends JFrame {
    private JLabel l = new JLabel("<Enter> ํ‚ค๋กœ ๋ฐฐ๊ฒฝ์ƒ‰์ด ๋ฐ”๋€๋‹ˆ๋‹ค.");
    public MyKeyEventEx() {
        setTitle("KeyListener Example");

        Container c = getContentPane();
        c.setLayout(new FlowLayout());
        c.add(l);
        c.addKeyListener(new MyKeyListener());
        setSize(450, 250);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        c.setFocusable(true);
        c.requestFocus();
    }

    public class MyKeyListener extends KeyAdapter {
        @Override
        public void keyPressed(KeyEvent e) {
            int r = (int)(Math.random()*256);
            int g = (int)(Math.random()*256);
            int b = (int)(Math.random()*256);
            if(e.getKeyChar() == '\n') {
                l.setText("r="+r+", g="+g+", b="+b);
                getContentPane().setBackground(new Color(r, g, b));
            } else if(e.getKeyChar() == 'q') {
                System.exit(0);
            }
        }
    }

    public static void main(String[] args) {
        new MyKeyEventEx();
    }
}
  • ์ž…๋ ฅ๋œ ๋ฌธ์ž ํ‚ค ํŒ๋ณ„
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class MyKeyEventEx2 extends JFrame {
    private JLabel l = new JLabel("HELLO");
    private final int FLYING_UNIT = 10;
    public MyKeyEventEx2() {
        super("Moving the Text");
        Container c = getContentPane();
        c.setLayout(null);
        c.addKeyListener(new MyKeyListener());
        l.setSize(50, 20);
        l.setLocation(50, 50);
        c.add(l);
        setSize(300, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        c.setFocusable(true);
        c.requestFocus();
    }

    public class MyKeyListener extends KeyAdapter {
        @Override
        public void keyPressed(KeyEvent e) {
            switch(e.getKeyCode()) {
                case KeyEvent.VK_UP -> l.setLocation(l.getX(), l.getY()-FLYING_UNIT);
                case KeyEvent.VK_DOWN -> l.setLocation(l.getX(), l.getY()+FLYING_UNIT);
                case KeyEvent.VK_RIGHT -> l.setLocation(l.getX()+FLYING_UNIT, l.getY());
                case KeyEvent.VK_LEFT -> l.setLocation(l.getX()-FLYING_UNIT, l.getY());
            }
        }
    }

    public static void main(String[] args) {
        new MyKeyEventEx2();
    }
}
  • ์ƒ, ํ•˜, ์ขŒ, ์šฐ ํ‚ค๋กœ ํ…์ŠคํŠธ ์›€์ง์ด๊ธฐ

Unicode

  • ์œ ๋‹ˆ์ฝ”๋“œ ํ‚ค์˜ ํŠน์ง•
    • ๊ตญ์ œ ์‚ฐ์—… ํ‘œ์ค€
    • ์ „ ์„ธ๊ณ„์˜ ๋ฌธ์ž๋ฅผ ์ปดํ“จํ„ฐ์—์„œ ์ผ๊ด€๋˜๊ฒŒ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ ์ฒด๊ณ„
    • ๋ฌธ์ž๋“ค์— ๋Œ€ํ•ด์„œ๋งŒ ํ‚ค ์ฝ”๋“œ ๊ฐ’ ์ •์˜
    • ๋ฌธ์ž๊ฐ€ ์•„๋‹Œ ํ‚ค ๊ฒฝ์šฐ์—๋Š” ํ‘œ์ค€ํ™”๋œ ํ‚ค ์ฝ”๋“œ ๊ฐ’ ์—†์Œ(์ œ์–ดํ‚ค)
  • ์œ ๋‹ˆ์ฝ”๋“œ ํ‚ค๊ฐ€ ์ž…๋ ฅ๋˜๋Š” ๊ฒฝ์šฐ
    • keyPressed(), keyTyped(), keyReleased() ๊ฐ€ ์ˆœ์„œ๋Œ€๋กœ ํ˜ธ์ถœ
  • ์œ ๋‹ˆ์ฝ”๋“œ ํ‚ค๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ
    • keyPressed(), keyReleased() ๋งŒ ํ˜ธ์ถœ๋จ
  • ๊ฐ€์ƒ ํ‚ค(Virtual Key)
    • ์œ ๋‹ˆ์ฝ”๋“œ ํ‚ค์™€ ์ƒ๊ด€์—†์ด, ๋ชจ๋“  ํ‚ค์— ์ž๋ฐ”์˜ ๊ฐ€์ƒ ํ‚ค ์ฝ”๋“œ๊ฐ€ ์ •์˜๋˜์–ด ์žˆ์Œ
    • ๊ฐ€์ƒ ํ‚ค ์ฝ”๋“œ๋Š” KeyEvent ํด๋ž˜์Šค์— ์ƒ์ˆ˜๋กœ ์„ ์–ธ
    • VK_0 ~ VK_9: 0์—์„œ 9๊นŒ์ง€์˜ ํ‚ค(ํ•ด๋‹น ํ‚ค์˜ ์œ ๋‹ˆ์ฝ”๋“œ ๊ฐ’๊ณผ ๋™์ผ)
    • VK_A ~ VK_Z: A์—์„œ Z๊นŒ์ง€์˜ ํ‚ค(ํ•ด๋‹น ์œ ๋‹ˆ์ฝ”๋“œ ๊ฐ’๊ณผ ๋™์ผ)
    • VK_F1 ~ VK_F24
    • VK_HOME
    • VK_END
    • VK_PGUP
    • VK_PGDN
    • VK_UNDEFINED: ์ž…๋ ฅ๋œ ํ‚ค์˜ ์ฝ”๋“œ ๊ฐ’์„ ์•Œ ์ˆ˜ ์—†์Œ
    • VK_LEFT
    • VK_RIGHT
    • VK_UP
    • VK_DOWN
    • VK_CONTROL
    • VK_SHIFT
    • VK_ALT
    • VK_TAB
  • ๊ฐ€์ƒ ํ‚ค์™€ ์ž…๋ ฅ๋œ ํ‚ค ํŒ๋ณ„
    • ํ‚ค ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์ž…๋ ฅ๋œ ํ‚ค ์ •๋ณด๊ฐ€ KeyEvent ๊ฐ์ฒด์— ๋‹ด๊ฒจ ๋ฆฌ์Šค๋„ˆ์— ์ „๋‹ฌ๋จ
    • KeyEvent ๊ฐ์ฒด
      • ์ž…๋ ฅ๋œ ํ‚ค ์ •๋ณด๋ฅผ ๊ฐ€์ง„ ์ด๋ฒคํŠธ ๊ฐ์ฒด
    • KeyEvent ๊ฐ์ฒด์˜ ๋ฉ”์†Œ๋“œ๋กœ ์ž…๋ ฅ๋œ ํ‚ค ํŒ๋ณ„
      • char getKeyChar()
        • ํ‚ค์˜ ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž ๊ฐ’ ๋ฆฌํ„ด
        • Unicode ๋ฌธ์ž ํ‚ค์ธ ๊ฒฝ์šฐ์—๋งŒ ์˜๋ฏธ๊ฐ€ ์žˆ์Œ
        • ์ž…๋ ฅ๋œ ํ‚ค๋ฅผ ํŒ๋ณ„ํ•˜๊ธฐ ์œ„ํ•ด ๋ฌธ์ž ๊ฐ’๊ณผ ๋น„๊ตํ•˜๋ฉด ๋จ
      • int getKeyCode()
        • ์œ ๋‹ˆ์ฝ”๋“œ ํ‚ค ํฌํ•จ
        • ๋ชจ๋“  ํ‚ค์— ๋Œ€ํ•œ ์ •์ˆ˜ํ˜• ํ‚ค ์ฝ”๋“œ ๋ฆฌํ„ด
        • ์ž…๋ ฅ๋œ ํ‚ค๋ฅผ ํŒ๋ณ„ํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ€์ƒ ํ‚ค ๊ฐ’๊ณผ ๋น„๊ตํ•ด์•ผ ํ•จ
        • ๊ฐ€์ƒ ํ‚ค ๊ฐ’์€ KeyEvent ํด๋ž˜์Šค์— ์ƒ์ˆ˜๋กœ ์„ ์–ธ๋จ
    • KeyEvent ๊ฐ์ฒด์˜ ๋ฉ”์†Œ๋“œ๋กœ ์ž…๋ ฅ๋œ ํ‚ค ํŒ๋ณ„
      • String getKeyText(int keyCode)
        • Static ๋ฉ”์†Œ๋“œ
        • ํ‚ค ์ด๋ฆ„ ๋ฌธ์ž์—ด ๋ฆฌํ„ด
        • ๋งค๊ฐœ๋ณ€์ˆ˜ keyCode์˜ ์ฝ”๋“œ ๊ฐ’(๊ฐ€์ƒ ํ‚ค)์— ํ•ด๋‹นํ•˜๋Š” ํ‚ค์˜ ์ด๋ฆ„ ๋ฌธ์ž์—ด ๋ฆฌํ„ด
          • F1 ํ‚ค์˜ ๊ฒฝ์šฐ "F1", Shift ํ‚ค์˜ ๊ฒฝ์šฐ "SHIFT" ๋“ฑ์˜ ๋ฌธ์ž์—ด ๋ฆฌํ„ด

๋งˆ์šฐ์Šค ์ด๋ฒคํŠธ์™€ ๋งˆ์šฐ์Šค ๊ด€๋ จ ๋ฆฌ์Šค๋„ˆ

  • ๋งˆ์šฐ์Šค ์ด๋ฒคํŠธ
    • ์‚ฌ์šฉ์ž์˜ ๋งˆ์šฐ์Šค ์กฐ์ž‘์— ๋”ฐ๋ผ ๋ฐœ์ƒํ•˜๋Š” ์ด๋ฒคํŠธ
      • MouseListener
        • ๋งˆ์šฐ์Šค๊ฐ€ ์ปดํฌ๋„ŒํŠธ ์œ„์— ์˜ฌ๋ผ๊ฐˆ ๋•Œ: void mouseEntered(MouseEvent e)
        • ๋งˆ์šฐ์Šค๊ฐ€ ์ปดํฌ๋„ŒํŠธ์—์„œ ๋‚ด๋ ค์˜ฌ ๋•Œ: void mouseExited(MouseEvent e)
        • ๋งˆ์šฐ์Šค ๋ฒ„ํŠผ์ด ๋ˆŒ๋Ÿฌ์กŒ์„ ๋•Œ: void mousePressed(MouseEvent e)
        • ๋ˆŒ๋Ÿฌ์ง„ ๋ฒ„ํŠผ์ด ๋–ผ์–ด์งˆ ๋•Œ: void mouseReleased(MouseEvent e)
        • ๋งˆ์šฐ์Šค๋กœ ์ปดํฌ๋„ŒํŠธ๋ฅผ ํด๋ฆญํ•˜์˜€์„ ๋•Œ: void mouseClicked(MouseEvent e)
      • MouseMotionListener
        • ๋งˆ์šฐ์Šค๊ฐ€ ๋“œ๋ž˜๊ทธ๋˜๋Š” ๋™์•ˆ: void mouseDragged(MouseEvent e)
        • ๋งˆ์šฐ์Šค๊ฐ€ ์›€์ง์ด๋Š” ๋™์•ˆ: void mouseMoved(MouseEvent e)
      • MouseWheelListener
        • ๋งˆ์šฐ์Šค ํœ ์ด ๊ตฌ๋ฅด๋Š” ๋™์•ˆ: void mouseWheelMoved(MouseEvent e)
    • ๋งˆ์šฐ์Šค๊ฐ€ ๋ˆŒ๋Ÿฌ์ง„ ์œ„์น˜์—์„œ ๋–ผ์–ด์ง€๋Š” ๊ฒฝ์šฐ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ ์ˆœ์„œ
      • mousePressed(), mouseReleased(), mouseClicked()
    • ๋งˆ์šฐ์Šค๊ฐ€ ๋“œ๋ž˜๊ทธ๋  ๋•Œ ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ ์ˆœ์„œ
      • mousePressed(), mouseDragged(), mouseDragged(), ..., mouseDragged(), mouseReleased()
  • ๋งˆ์šฐ์Šค ๋ฆฌ์Šค๋„ˆ ๋‹ฌ๊ธฐ
    • component.addMouseListener(myMouseListener);
      • ๋งˆ์šฐ์Šค ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋งˆ์šฐ์Šค ๋ฆฌ์Šค๋„ˆ๋ฅผ ์ปดํฌ๋„ŒํŠธ์— ๋“ฑ๋ก
    • component.addMouseMotionListener(myMouseMotionListener);
      • ์ปดํฌ๋„ŒํŠธ๊ฐ€ MouseMotion ์ด๋ฒคํŠธ๋ฅผ ํ•จ๊ป˜ ์ฒ˜๋ฆฌํ•˜๊ณ ์ž ํ•  ๋•Œ, ๋ฆฌ์Šค๋„ˆ ๋”ฐ๋กœ ๋“ฑ๋ก
  • MouseEvent ๊ฐ์ฒด๋กœ๋ถ€ํ„ฐ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์ •๋ณด
    • ๋งˆ์šฐ์Šค ํฌ์ธํ„ฐ์˜ ์œ„์น˜: int getX(), int getY(), Point getPoint()
    • ์ž…๋ ฅ๋œ ๋งˆ์šฐ์Šค ๋ฒ„ํŠผ: int getButton()
    • ๋งˆ์šฐ์Šค ํด๋ฆญ ํšŸ์ˆ˜: int getClickCount()
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class MyMouseEvent01 extends JFrame {
    private JLabel l = new JLabel("Move me");
    public MyMouseEvent01() {
        setTitle("Click and Dragg");

        Container c = getContentPane();
        c.setLayout(null);
        l.setSize(80, 20);
        l.setLocation(80, 50);
        MyMouseListener li = new MyMouseListener();
        c.addMouseListener(li);
        c.addMouseMotionListener(li);
        c.add(l);

        setSize(500, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public class MyMouseListener implements MouseListener, MouseMotionListener {

        @Override
        public void mouseClicked(MouseEvent e) {

        }

        @Override
        public void mousePressed(MouseEvent e) {
            l.setLocation(e.getX(), e.getY());
            setTitle("mousePressed("+e.getX()+", "+e.getY()+")");
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            l.setLocation(e.getX(), e.getY());
            setTitle("mouseReleased("+e.getX()+", "+e.getY()+")");
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            Component c = (Component)e.getSource();
            c.setBackground(Color.CYAN);
        }

        @Override
        public void mouseExited(MouseEvent e) {
            Component c = (Component)e.getSource();
            c.setBackground(Color.YELLOW);
            setTitle("mouseExited("+e.getX()+", "+e.getY()+")");
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            l.setLocation(e.getX(), e.getY());
            setTitle("mouseDragged("+e.getX()+", "+e.getY()+")");
        }

        @Override
        public void mouseMoved(MouseEvent e) {
            l.setLocation(e.getX(), e.getY());
            setTitle("mouseMoved("+e.getX()+", "+e.getY()+")");
        }
    }

    public static void main(String[] args) {
        new MyMouseEvent01();
    }
}
  • ๋งˆ์šฐ์Šค์™€ ๋งˆ์šฐ์Šค ๋ชจ์…˜ ์ด๋ฒคํŠธ ํ™œ์šฉ ์ฝ”๋“œ 1
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class MyMouseEvent02 extends JFrame {
    private JLabel l = new JLabel("No Mouse Event");
    public MyMouseEvent02() {
        super("Mouse Event Example");
        Container c = getContentPane();
        c.setLayout(new FlowLayout());
        MyMouseListener ml = new MyMouseListener();
        c.addMouseListener(ml);
        c.addMouseMotionListener(ml);
        c.add(l);
        setSize(500, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public class MyMouseListener implements MouseListener, MouseMotionListener {
        @Override
        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, g, b));
            }
        }

        @Override
        public void mousePressed(MouseEvent e) {
            l.setText("mousePressed("+e.getX()+", "+e.getY()+")");
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            l.setText("mouseReleased("+e.getX()+", "+e.getY()+")");
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            Component c = (Component)e.getSource();
            c.setBackground(Color.CYAN);
        }

        @Override
        public void mouseExited(MouseEvent e) {
            Component c = (Component)e.getSource();
            c.setBackground(Color.YELLOW);
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            l.setText("mouseDragged("+e.getX()+", "+e.getY()+")");
        }

        @Override
        public void mouseMoved(MouseEvent e) {
            l.setText("mouseMoved("+e.getX()+", "+e.getY()+")");
        }
    }

    public static void main(String[] args) {
        new MyMouseEvent02();
    }
}
  • ๋งˆ์šฐ์Šค์™€ ๋งˆ์šฐ์Šค ๋ชจ์…˜ ์ด๋ฒคํŠธ ํ™œ์šฉ ์ฝ”๋“œ 2
728x90
๋ฐ˜์‘ํ˜•

'Development Study > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Java] Lecture - Day 15  (0) 2024.04.26
[Java] Lecture - Day 14  (0) 2024.04.05
[Java] Lecture - Day 12  (0) 2024.03.22
[Java] Lecture - Day 11  (0) 2024.03.15
[Java] Lecture - Day 10  (0) 2023.12.12
Comments