ActionEvent
๋ฒํผ์ ํด๋ฆญํด ํ์, ์ํ ์ ํ์ ์ํค๋ ์ด๋ฒคํธ๋ฅผ ๋ง๋ค์ด๋ณด์.
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
//๋
๋ฆฝํด๋์ค๋ก ActionListener ๋ง๋ค๊ธฐ
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
JButton b = (JButton)e.getSource(); //์บ์คํ
ํ์
if(b.getText().equals("Action"))
b.setText("์ก์
");
else
b.setText("Action");
}
}
public class IndepActionListener extends JFrame{
IndepActionListener(){
setTitle("๋
๋ฆฝํด๋์ค๋ก Action Listener ๋ง๋ค๊ธฐ");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
JButton b = new JButton("Action");
c.add(b);
b.addActionListener(new MyActionListener()); //๋ฆฌ์ค๋ ๋ฑ๋ก
setSize(300,300);
setVisible(true);
}
public static void main(String[] args) {
new IndepActionListener();
}
}
c๋ฅผ ๋นผ์์ ๋๋ฅผ ๋ ์์ ๋ฐ๋๊ฒ ํ๊ธฐ
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
//๋
๋ฆฝํด๋์ค๋ก ActionListener ๋ง๋ค๊ธฐ
class MyActionListener implements ActionListener{
Container c;
public MyActionListener(Container c) {
this.c = c;
}
@Override
public void actionPerformed(ActionEvent e) {
JButton b = (JButton)e.getSource(); //์บ์คํ
ํ์
if(b.getText().equals("Action")) {
b.setText("์ก์
");
c.setBackground(Color.ORANGE);
}
else {
b.setText("Action");
c.setBackground(Color.CYAN);
}
}
}
public class IndepActionListener extends JFrame{
IndepActionListener(){
setTitle("๋
๋ฆฝํด๋์ค๋ก Action Listener ๋ง๋ค๊ธฐ");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
JButton b = new JButton("Action");
c.add(b);
b.addActionListener(new MyActionListener(c));
setSize(300,300);
setVisible(true);
}
public static void main(String[] args) {
new IndepActionListener();
}
}
์ต๋ช ํด๋์ค
ํด๋์ค ์ ์+์ธ์คํด์ค ์์ฑ์ ํ๋ฒ์ ์์ฑ: ์ด๋ฆ์ด ์๊ธฐ ๋๋ฌธ์ 1ํ์ฉ
์์ ํด๋์ค ์์๋ค๊ฐ ํจ์ ๋ณธ๋ฌธ์ ์์ฑ
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class AnnoyListener extends JFrame {
AnnoyListener(){
setTitle("๋
๋ฆฝํด๋์ค๋ก Action Listener ๋ง๋ค๊ธฐ");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
JButton b = new JButton("Action");
c.add(b);
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JButton b = (JButton)e.getSource(); //์บ์คํ
ํ์
if(b.getText().equals("Action")) {
b.setText("์ก์
");
c.setBackground(Color.ORANGE);
}
else {
b.setText("Action");
c.setBackground(Color.CYAN);
}
}
});
setSize(300,300);
setVisible(true);
}
public static void main(String[] args) {
new AnnoyListener();
}
}
๋ง์ฐ์ค ์ด๋ฒคํธ
import java.awt.Container;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MouseListenerEx extends JFrame {
JLabel la;
class MyMouseListener implements MouseListener{
@Override
public void mouseClicked(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
la.setLocation(x, y);
}
@Override
public void mouseReleased(MouseEvent e) {}
}
MouseListenerEx(){
setTitle("๋ง์ฐ์ค ์ด๋ฒคํธ");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(null);
la = new JLabel("Hello");
la.setSize(100,30);
la.setLocation(30,30);
c.add(la);
c.addMouseListener(new MyMouseListener());
setSize(500,500);
setVisible(true);
}
public static void main(String[] args) {
new MouseListenerEx();
}
}
์ด๋ํฐ(Adapter) ํด๋์ค
์์น ์๋ ์ถ์ ๋ฉ์๋๋ฅผ ๋ชจ๋ ๊ตฌํํ์ง ์๊ธฐ ์ํด ์กด์ฌ.
์ก์ , ์์ดํ ์ 1๊ฐ ๋ฟ์ด๋ผ ํ์์์.
extends MouseAdapter๋ฅผ ๋ฃ์ด์ฃผ๋ฉด ์ค๋ฒ๋ผ์ด๋ฉ์ ๊ตณ์ด ์ ํด๋ ์ค๋ฅ๊ฐ ๋์ง ์์.
์ํ๋ ๊ฒ๋ง ์ค๋ฒ๋ผ์ด๋ฉํด์ ๋ฐ๊พธ๊ธฐ ๊ฐ๋ฅ.
์์ ์ฝ๋๋ฅผ ๊ธฐ๋ณธ์ผ๋ก
import java.awt.Container;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MouseAdapterEx extends JFrame{
JLabel la = new JLabel("hello");
MouseAdapterEx(){
setTitle("mouse adapter");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(null);
la.setSize(80, 50);
la.setLocation(100, 100);
c.add(la);
c.addMouseListener(new MyMouseListener());
setSize(300,300);
setVisible(true);
}
class MyMouseListener extends MouseAdapter{
@Override
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();
}
}
๊ฐ๋จํ๊ฒ ์ฝ๋๊ฐ ์ ๋ฆฌ๋์๋ค.
728x90
๋ฐ์ํ
0