package ch03;
import java.awt.Color;
public class Activity1 extends BaseActivity{
CallbackCheckPosition callbackCheckPosition = new CallbackCheckPosition() {
@Override
public void checkPosition(int x, int y) {
System.out.println("x :" + x);
System.out.println("y :" + y);
}
};
public Activity1(String name) {
super(name);
}
protected void setInitLayout() {
super.setInitLayout();
super.panel.setBackground(Color.blue);
}
}
package ch03;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Activity2 extends BaseActivity {
CallbackCheckPosition callbackCheckPosition;
public void setCallbackCheckPosition(CallbackCheckPosition callbackCheckPosition) {
this.callbackCheckPosition = callbackCheckPosition;
}
public Activity2(String name) {
super(name);
addEventListener();
}
private void addEventListener() {
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
callbackCheckPosition.checkPosition(x, y);
}
});
}
protected void setInitLayout() {
super.setInitLayout();
super.panel.setBackground(Color.yellow);
}
}
package ch03;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class BaseActivity extends JFrame{
String name;
JPanel panel;
public BaseActivity(String name) {
this.name = name;
initData();
setInitLayout();
}
private void initData() {
setTitle(name);
setSize(500, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
panel = new JPanel();
}
protected void setInitLayout() {
setVisible(true);
add(panel);
// panel.setBackground(Color.yellow);
}
}
package ch03;
public interface CallbackCheckPosition {
public abstract void checkPosition (int x, int y);
}
package ch03;
public class MainTest {
public static void main(String[] args) {
Activity1 activity1 = new Activity1("첫번째 화면입니다.");
Activity2 activity2 = new Activity2("두번째 화면입니다.");
activity2.setCallbackCheckPosition(activity1.callbackCheckPosition);
}
}
'Java > Excercise' 카테고리의 다른 글
callback ch04 News (0) | 2021.09.09 |
---|---|
callback ch04 CalendarMainTest (0) | 2021.09.09 |
callback 02 Activity 메서드를 이용한 콜백 (0) | 2021.09.09 |
callback ch01 CallbackBtnAction (0) | 2021.09.09 |
callback ch00 Main,SubActivity (0) | 2021.09.09 |