package test;
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;
import javax.swing.JTextField;
// 독립클래스
class MyActionListener implements ActionListener {
JTextField tf;
JFrame frame;
MyActionListener(JTextField tf) {
this.tf = tf;
}
public void setFrame(JFrame frame) {
this.frame = frame;
}
@Override
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton) e.getSource();
if (btn.getText().equals("Action")) {
btn.setText("액션");
frame.setTitle("액션");
} else {
btn.setText("Action");
frame.setTitle("Action");
}
String str = tf.getText();
System.out.println(str);
} // actionPerformed()
} // MyActionListener
public class Test1 extends JFrame {
public Test1() {
setTitle("Action 이벤트 리스너 연습");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
JButton btn = new JButton("Action");
JTextField tf = new JTextField(10);
tf.setText("안녕하세요");
MyActionListener l = new MyActionListener(tf);
l.setFrame(this);
btn.addActionListener(l);
c.add(tf);
c.add(btn);
setSize(350, 150);
setVisible(true);
}
public static void main(String[] args) {
new Test1();
}
} // Test1
package test;
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;
import javax.swing.JTextField;
public class Test2 extends JFrame {
JButton btn;
JTextField tf;
public Test2() {
this.setTitle("Action 이벤트 리스너 연습");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
btn = new JButton("Action");
tf = new JTextField(10);
tf.setText("안녕하세요");
btn.addActionListener(new MyActionListener());
c.add(tf);
c.add(btn);
setSize(350, 150);
setVisible(true);
} // Test2 생성자
// static이 아닌(non-static)
// 멤버 내부클래스
// static이 아닌 내부클래스는 해당 객체가 생성될 때 메모리에 클래스정보가 올라온다.
class MyActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (btn.getText().equals("Action")) {
btn.setText("액션");
Test2.this.setTitle("액션");
} else {
btn.setText("Action");
setTitle("Action");
}
String str = tf.getText();
System.out.println(str);
}
} // class MyActionListener
public static void main(String[] args) {
new Test2();
}
} // class Test2
package test;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import test.Test2.MyActionListener;
public class Test2_1 extends JFrame {
JButton btn;
JTextField tf;
// List list = new ArrayList<>();
// 익명의 멤버 내부클래스
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (btn.getText().equals("Action")) {
btn.setText("액션");
Test2_1.this.setTitle("액션");
} else {
btn.setText("Action");
setTitle("Action");
}
String str = tf.getText();
System.out.println(str);
}
};
public Test2_1() {
this.setTitle("Action 이벤트 리스너 연습");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
btn = new JButton("Action");
tf = new JTextField(10);
tf.setText("안녕하세요");
btn.addActionListener(listener);
c.add(tf);
c.add(btn);
setSize(350, 150);
setVisible(true);
}
public static void main(String[] args) {
new Test2_1();
}
}
package test;
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;
import javax.swing.JTextField;
public class Test3 extends JFrame {
public Test3() {
this.setTitle("Action 이벤트 리스너 연습");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
JButton btn = new JButton("Action");
JTextField tf = new JTextField(10);
tf.setText("안녕하세요");
// 지역 내부클래스
class MyActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (btn.getText().equals("Action")) {
btn.setText("액션");
Test3.this.setTitle("액션");
} else {
btn.setText("Action");
setTitle("Action");
}
String str = tf.getText();
System.out.println(str);
}
} // class MyActionListener
btn.addActionListener(new MyActionListener());
c.add(tf);
c.add(btn);
setSize(350, 150);
setVisible(true);
}
public static void main(String[] args) {
new Test3();
}
}
package test;
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;
import javax.swing.JTextField;
public class Test3_1 extends JFrame {
public Test3_1() {
this.setTitle("Action 이벤트 리스너 연습");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
JButton btn = new JButton("Action");
JTextField tf = new JTextField(10);
tf.setText("안녕하세요");
// ActionListener listener = new ActionListener() {
// @Override
// public void actionPerformed(ActionEvent e) {
// if (btn.getText().equals("Action")) {
// btn.setText("액션");
// Test3_1.this.setTitle("액션");
// } else {
// btn.setText("Action");
// setTitle("Action");
// }
//
// String str = tf.getText();
// System.out.println(str);
//
// }
// };
// btn.addActionListener(listener);
// 익명의 지역 내부클래스
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (btn.getText().equals("Action")) {
btn.setText("액션");
Test3_1.this.setTitle("액션");
} else {
btn.setText("Action");
setTitle("Action");
}
String str = tf.getText();
System.out.println(str);
}
});
c.add(tf);
c.add(btn);
setSize(350, 150);
setVisible(true);
}
public static void main(String[] args) {
new Test3_1();
}
}