mojo's Blog

KeyListener / MouseListener 본문

Java

KeyListener / MouseListener

_mojo_ 2021. 7. 28. 00:56

key Listener code

import javax.swing.*; // JFrame
import java.awt.*; // Container
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.*;

public class KeyListenerEx extends JFrame{
	private JLabel [] keyMessage;

	public KeyListenerEx(){
		setTitle("Action 이벤트 리스너 예제");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		Container c=getContentPane();
		c.setLayout(new FlowLayout());
		c.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				int keyCode=e.getKeyCode();
				char keyChar=e.getKeyChar();
				keyMessage[0].setText(Integer.toString(keyCode)); // 16진수 값으로 표현
				keyMessage[1].setText(Character.toString(keyChar)); // 키 문자 표현
				keyMessage[2].setText(KeyEvent.getKeyText(keyCode)); // 키 이름 문자열 출력
				
				System.out.println("Completed");
			}
		});
		
		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);
		}
		
		setSize(300,150);
		setVisible(true);
		
		// contentPane이 키 입력을 받을 수 있도록 포커스를 강제로 지정한다
		c.setFocusable(true);
		c.requestFocus();
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new KeyListenerEx();
	}

}

 

Mouse Listener code

import javax.swing.*; // JFrame
import java.awt.*; // Container
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.*;

public class MouseListenerAllex extends JFrame {
	private JLabel la=new JLabel("No Mouse Event");
	
	class MyMouseListener implements MouseListener, MouseMotionListener {

		@Override
		public void mouseDragged(MouseEvent e) { // mouse가 drag 하는 경우
			// TODO Auto-generated method stub
			la.setText("mouseDragged ("+e.getX()+","+e.getY()+")");
		}

		@Override
		public void mouseMoved(MouseEvent e) { // mouse가 움직이는 경우(안 누른 상태에서)
			// TODO Auto-generated method stub
			la.setText("mouseMoved ("+e.getX()+","+e.getY()+")");
		}

		@Override
		public void mouseClicked(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseEntered(MouseEvent e) { // mouse가 component 안에 있는 경우
			// TODO Auto-generated method stub
			Component c=(Component)e.getSource();
			c.setBackground(Color.CYAN);
		}

		@Override
		public void mouseExited(MouseEvent e) { // mouse가 component 밖에 있는 경우
			// TODO Auto-generated method stub
			Component c=(Component)e.getSource();
			c.setBackground(Color.YELLOW);
		}

		@Override
		public void mousePressed(MouseEvent e) { // mouse를 누른 경우
			// TODO Auto-generated method stub
			la.setText("mousePressed ("+e.getX()+","+e.getY()+")");
		}

		@Override
		public void mouseReleased(MouseEvent e) { // mouse를 놓은 상태
			// TODO Auto-generated method stub
			la.setText("mouseReleased ("+e.getX()+","+e.getY()+")");
		}
		
	}
	
	public MouseListenerAllex() {
		setTitle("Action 이벤트 리스너 예제");
		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);
		
		// contentPane이 키 입력을 받을 수 있도록 포커스를 강제로 지정한다
		c.setFocusable(true);
		c.requestFocus();
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new MouseListenerAllex();
	}

}
Comments