/*import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main extends JFrame {
public Main() {
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);
setSize(400, 400);
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
}
class MyActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JButton b = (JButton)e.getSource();
if(b.getText().equals("Action"))
b.setText("액션");
else
b.setText("Action");
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main extends JFrame {
public Main() {
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);
setSize(400, 400);
setVisible(true);
}
private class MyActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JButton b = (JButton)e.getSource();
if(b.getText().equals("Action"))
b.setText("액션");
else
b.setText("Action");
Main.this.setTitle(b.getText());
}
}
public static void main(String[] args) {
new Main();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main extends JFrame {
private JLabel la = new JLabel("hello");
public Main() {
setTitle("Mouse 이벤트 예제");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.addMouseListener(new MML());
c.setLayout(null);
la.setSize(50, 20);
la.setLocation(30, 30);
c.add(la);
setSize(250, 250);
setVisible(true);
}
class MML implements MouseListener {
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
la.setLocation(x, y);
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
public static void main(String[] args) {
new Main();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main extends JFrame {
private JLabel la = new JLabel("Hello");
private JLabel ba = new JLabel("Heyhey");
public Main() {
setTitle("Mouse 이벤트 예제");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.addMouseListener(new MMA());
c.setLayout(null);
ba.setSize(50, 20);
ba.setLocation(50, 50);
c.add(ba);
la.setSize(50, 20);
la.setLocation(30, 30);
c.add(la);
setSize(500, 500);
setVisible(true);
}
class MMA extends MouseAdapter {
public void mouseReleased(MouseEvent e) {
int x = e.getX();
int y = e.getY();
la.setLocation(x, y);
}
public void mouseClicked(MouseEvent e) {
int p = e.getX();
int q = e.getY();
ba.setLocation(p, q);
}
}
public static void main(String[] args) {
new Main();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main extends JFrame {
private JLabel [] keyM;
public Main() {
setTitle("KeyListener 예제");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.addKeyListener(new MKL());
keyM = new JLabel [3];
keyM[0] = new JLabel(" getkeyCode() ");
keyM[1] = new JLabel(" getkeyChar() ");
keyM[2] = new JLabel(" getkeyText() ");
for(int i=0; i<keyM.length; i++) {
c.add(keyM[i]);
keyM[i].setOpaque(true);
keyM[i].setBackground(Color.yellow);
}
setSize(300, 150);
setVisible(true);
c.setFocusable(true);
c.requestFocus();
}
class MKL extends KeyAdapter {
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
char keyChar = e.getKeyChar();
keyM[0].setText(Integer.toString(keyCode));
keyM[1].setText(Character.toString(keyChar));
keyM[2].setText(KeyEvent.getKeyText(keyCode));
System.out.println("KeyPressed");
}
public void keyReleased(KeyEvent e) {
System.out.println("KeyReleased");
}
public void keyTyped(KeyEvent e) {
System.out.println("KeyTyped");
}
}
public static void main(String[] args) {
new Main();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main extends JFrame {
private final int FLYUNIT = 10;
private JLabel la = new JLabel("*");
public Main() {
setTitle(":)");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(null);
c.addKeyListener(new MKL());
la.setLocation(50, 50);
la.setSize(100, 100);
c.add(la);
setSize(300, 300);
setVisible(true);
c.setFocusable(true);
c.requestFocus();
c.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
Component com = (Component)e.getSource();
com.setFocusable(true);
com.requestFocus();
}
});
}
class MKL extends KeyAdapter {
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
switch(keyCode) {
case KeyEvent.VK_UP:
la.setLocation(la.getX(), la.getY()-FLYUNIT); break;
case KeyEvent.VK_DOWN:
la.setLocation(la.getX(), la.getY()+FLYUNIT); break;
case KeyEvent.VK_LEFT:
la.setLocation(la.getX()-FLYUNIT, la.getY()); break;
case KeyEvent.VK_RIGHT:
la.setLocation(la.getX()+FLYUNIT, la.getY()); break;
}
}
}
public static void main(String[] args) {
new Main();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main extends JFrame {
private JLabel la = new JLabel("No Mouse Event");
public Main() {
setTitle(":)");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
MML l = new MML();
c.addMouseListener(l);
c.addMouseMotionListener(l);
c.add(la);
setSize(300, 200);
setVisible(true);
}
class MML implements MouseListener, MouseMotionListener {
@Override
public void mouseDragged(MouseEvent e) {
la.setText("MouseDragged (" + e.getX()+"," + e.getY() + ")");
}
@Override
public void mouseMoved(MouseEvent e) {
la.setText("MouseMoved (" + e.getX() +"," + e.getY() + ")");
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
la.setText("MousePressed (" + e.getX() + "," + e.getY() + ")");
}
@Override
public void mouseReleased(MouseEvent e) {
la.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);
}
}
public static void main(String[] args) {
new Main();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main extends JFrame {
public Main() {
setTitle("C&D");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.addMouseListener(new MML());
setSize(300, 200);
setVisible(true);
}
class MML 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 Main();
}
}*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main extends JFrame {
private JLabel I = new JLabel("0");
private JLabel O = new JLabel("0");
private JLabel P = new JLabel("0");
public Main() {
setTitle("Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 400);
setVisible(true);
Container c = getContentPane();
c.setLayout(null);
c.add(I);
I.setLocation(50, 50);
I.setSize(50, 50);
I.setOpaque(true);
I.setBackground(Color.magenta);
c.add(O);
O.setLocation(150, 50);
O.setSize(50, 50);
O.setOpaque(true);
O.setBackground(Color.magenta);
c.add(P);
P.setLocation(250, 50);
P.setSize(50, 50);
c.setFocusable(true);
c.requestFocus();
c.addMouseListener(new MML());
}
class MML extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
if(e.getClickCount()==2) {
int x = (int)(Math.random()*5);
I.setText(Integer.toString(x));
int y = (int)(Math.random()*5);
O.setText(Integer.toString(y));
int z = (int)(Math.random()*5);
P.setText(Integer.toString(z));
}
}
}
public static void main(String[] args) {
new Main();
}
}