Key ์ด๋ฒคํธ
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class KeyEventEx extends JFrame{
JLabel []keyMsg = new JLabel[3];
KeyEventEx(){
setTitle("KeyEvent");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
String[] names = {"getKeyCode()","getKeyChar()","getKeyText()"};
for(int i=0; i<keyMsg.length; i++) {
keyMsg[i] = new JLabel(names[i]);
keyMsg[i].setOpaque(true);
keyMsg[i].setBackground(Color.YELLOW);
c.add(keyMsg[i]);
}
c.addKeyListener(new MyKeyListener());
setSize(300,300);
setVisible(true);
c.setFocusable(true);
c.requestFocus();
}
class MyKeyListener extends KeyAdapter{
@Override
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
char ch = e.getKeyChar();
String text = e.getKeyText(code);
keyMsg[0].setText(Integer.toString(code));
keyMsg[1].setText(Character.toString(ch));
keyMsg[2].setText(text);
}
}
public static void main(String[] args) {
new KeyEventEx();
}
}
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class KeyEventEx extends JFrame{
JLabel []keyMsg = new JLabel[3];
JLabel colorla = new JLabel("color");
KeyEventEx(){
setTitle("KeyEvent");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
String[] names = {"getKeyCode()","getKeyChar()","getKeyText()"};
for(int i=0; i<keyMsg.length; i++) {
keyMsg[i] = new JLabel(names[i]);
keyMsg[i].setOpaque(true);
keyMsg[i].setBackground(Color.YELLOW);
c.add(keyMsg[i]);
}
c.addKeyListener(new MyKeyListener());
setSize(300,300);
setVisible(true);
c.setFocusable(true);
c.requestFocus();
}
class MyKeyListener extends KeyAdapter{
@Override
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
char ch = e.getKeyChar();
String text = e.getKeyText(code);
Container c = getContentPane();
keyMsg[0].setText(Integer.toString(code));
keyMsg[1].setText(Character.toString(ch));
keyMsg[2].setText(text);
if(ch == '%') {
colorla.setText("%");
c.setBackground(Color.YELLOW);
}
else if(text.equals("F1")) {
colorla.setText("F1");
c.setBackground(Color.GREEN);
}
}
}
public static void main(String[] args) {
new KeyEventEx();
}
}
๋ง์ฐ์ค ๋ชจ์ ์ด๋ฒคํธ
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MouseMotionEx extends JFrame{
JLabel la = new JLabel("No mouse event");
MouseMotionEx(){
setTitle("MouseMotionEx");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add(la);
MyMouseListener l = new MyMouseListener();
c.addMouseListener(l);
c.addMouseMotionListener(l);
setSize(300,300);
setVisible(true);
}
class MyMouseListener extends MouseAdapter{
@Override
public void mouseDragged(MouseEvent e) {
la.setText("Mouse Dragged("+e.getX()+", "+e.getY()+")");
}
@Override
public void mouseMoved(MouseEvent e) {
la.setText("Mouse Moved("+e.getX()+", "+e.getY()+")");
}
@Override
public void mouseEntered(MouseEvent e) {
la.setText("Mouse Entered("+e.getX()+", "+e.getY()+")");
Container c = (Container)e.getSource();
c.setBackground(Color.CYAN);
}
@Override
public void mouseExited(MouseEvent e) {
la.setText("Mouse Exited("+e.getX()+", "+e.getY()+")");
Container c = (Container)e.getSource();
c.setBackground(Color.ORANGE);
}
@Override
public void mousePressed(MouseEvent e) {
la.setText("Mouse Pressed("+e.getX()+", "+e.getY()+")");
}
@Override
public void mouseReleased(MouseEvent e) {
la.setText("Mouse Released("+e.getX()+", "+e.getY()+")");
}
}
public static void main(String[] args) {
new MouseMotionEx();
}
}
๋๋ค์ ๋ฃ์ด๋ณด๊ธฐ
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MouseMotionEx extends JFrame{
JLabel la = new JLabel("No mouse event");
MouseMotionEx(){
setTitle("MouseMotionEx");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add(la);
MyMouseListener l = new MyMouseListener();
c.addMouseListener(l);
c.addMouseMotionListener(l);
setSize(300,300);
setVisible(true);
}
class MyMouseListener extends MouseAdapter{
@Override
public void mouseClicked(MouseEvent e) {
int n = e.getClickCount();
if(n == 2) {
Container c = (Container)e.getSource();
int r = (int)(Math.random() * 256);
int g = (int)(Math.random() * 256);
int b = (int)(Math.random() * 256);
c.setBackground(new Color(r,g,b));
}
}
@Override
public void mouseDragged(MouseEvent e) {
la.setText("Mouse Dragged("+e.getX()+", "+e.getY()+")");
}
@Override
public void mouseMoved(MouseEvent e) {
la.setText("Mouse Moved("+e.getX()+", "+e.getY()+")");
}
@Override
public void mouseEntered(MouseEvent e) {
la.setText("Mouse Entered("+e.getX()+", "+e.getY()+")");
Container c = (Container)e.getSource();
c.setBackground(Color.CYAN);
}
@Override
public void mouseExited(MouseEvent e) {
la.setText("Mouse Exited("+e.getX()+", "+e.getY()+")");
Container c = (Container)e.getSource();
c.setBackground(Color.ORANGE);
}
@Override
public void mousePressed(MouseEvent e) {
la.setText("Mouse Pressed("+e.getX()+", "+e.getY()+")");
}
@Override
public void mouseReleased(MouseEvent e) {
la.setText("Mouse Released("+e.getX()+", "+e.getY()+")");
}
}
public static void main(String[] args) {
new MouseMotionEx();
}
}
728x90
๋ฐ์ํ
0