본문 바로가기

기록/자바_국비

[배운내용정리] 1209 자바 국비교육

728x90

2020년 12월 09일 9시 ~ 15시 30분 zoom으로 수업 진행

 

오늘로 자바는 끝!


 

package com.event.test01;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class A_Mouse extends JFrame implements MouseListener, MouseMotionListener{
	
	public A_Mouse() {
		this.setTitle("mouse Event");
		this.setSize(300,300);
		
		JPanel panel = new JPanel();
		panel.addMouseListener(this);
		panel.addMouseMotionListener(this);
		//이벤트 처리할 때 this
		//이벤트를 implemets 한 클래스를 의미함!
		this.add(panel);
		//this는 객체의 주소값
		//배치시킬 프레임이 this인 것 현재 클래스에서 JFrame을 상속받아 사용하기 때문에 this로 사용가능
		//만약 다른 클래스에서 JFrame을 상속받아 사용하는 상태에서 그 JFrame에 배치하고 싶다면 그 클래스이름 쓰면 된다.
		
		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		
	}
	
	
	public static void main(String[] args) {
		new A_Mouse();

	}

	public void display(String str, MouseEvent e) {
		System.out.println(str + " X = " + e.getX() + " , Y = " + e.getY());
	}

//=========================================================
	@Override
	public void mousePressed(MouseEvent e) {
		//마우스 눌릴 때 발생!
		this.display("Mouse Pressed (# of clicks : " + e.getClickCount() + ")", e);
		
	}
	@Override
	public void mouseDragged(MouseEvent e) {
		//드래그
		this.display("Mouse Dragged (# of clicks : " + e.getClickCount() + ")", e);		
	}
	@Override
	public void mouseMoved(MouseEvent e) {
		//마우스 움직이면 발생
		this.display("Mouse Moved (# of clicks : " + e.getClickCount() + ")", e);				
	}
	@Override
	public void mouseClicked(MouseEvent e) {
		//마우스 눌렸다 놓고 나서 발생@!
		this.display("Mouse Clicked (# of clicks : " + e.getClickCount() + ")", e);		
	}
	@Override
	public void mouseReleased(MouseEvent e) {
		//마우스 눌렸다가 놓을 때 발생!
		this.display("Mouse Released (# of clicks : " + e.getClickCount() + ")", e);
	}
	@Override
	public void mouseEntered(MouseEvent e) {
		//이벤트를 설정해준 객체의 범위에 마우스가 들어갔을 때 발생
		this.display("Mouse Entered (# of clicks : " + e.getClickCount() + ")", e);
	}
	@Override
	public void mouseExited(MouseEvent e) {
		//이벤트를 설정해준 객체의 범위에서 마우스가 나갈 때 발생
		this.display("Mouse Exited (# of clicks : " + e.getClickCount() + ")", e);
		
	}

}

 


 

package com.event.test02;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class A_Anonymous {

	public static void main(String[] args) {
		//익명클래스
		//클래스 생성과 동시에 객체 생성, 일회용
		
		JFrame mf = new JFrame("익명클래스");
		mf.setSize(300, 200);
		
		JPanel panel = new JPanel();
		JButton btn = new JButton("버튼을 눌러보세요");
		JLabel lbl = new JLabel("아직 버튼이 눌려지지 않았습니다.");
		
		btn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() == btn) {
					lbl.setText("드디어 버튼이 눌렸다");
				}
				
			}
		});
		
		
		panel.add(btn);
		panel.add(lbl);
		
		mf.add(panel);
		
		mf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mf.setVisible(true);
	}

}

 


 

package com.event.test02;

public class B_MTest {

	public static void main(String[] args) {
		new B_OtherClass();
	}

}

 

package com.event.test02;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class B_OtherClass extends JFrame{
	
	private JButton btn;
	private JLabel lbl;

	public B_OtherClass() {
		this.setSize(300, 200);
		this.setTitle("Other Class");
		
		JPanel panel = new JPanel();
		btn = new JButton("버튼을 눌러보세요");
		lbl = new JLabel("아직 버튼이 눌리지 않았습니다.");
		
		btn.addActionListener(new MyEvent(btn, lbl));
		//다른 클래스의 객체를 이용해서 이벤트 등록
		
		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
}

class MyEvent implements ActionListener {

	private JLabel label;
	
	public MyEvent(JButton btn, JLabel lbl) {
		btn.addActionListener(this);//여기서 this는 현재 클래스를 가리킴
		// ActionListener가 implements 되어있는 현재 클래스 this
		//음.. ActionListener에 선언되어있는 추상메소드를 구현한것을 실행할것임
		
		label = lbl;
		//메인에서 넘겨받은 라벨 객체를 현재 클래스에 저장해서! 사용할것임 -> 주소가 저장되는거
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		label.setText("버튼이 눌렸다");
		//메인에서 넘겨받은 라벨 객체를 저장한 label의 텍스트를 변경
		//주소가 메인의 lbl을 가리키고 있으니 변경되는 것은 lbl
	}
	
	
}


 


 

package com.event.test02;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class C_InnerClass extends JFrame{

	private JButton btn;
	private JLabel  lbl;
	
	public C_InnerClass() {
		this.setSize(300, 200);
		this.setTitle("내부 클래스");
		
		JPanel panel = new JPanel();
		btn = new JButton("버튼을 눌러보세요");
		lbl = new JLabel("아직 버튼이 눌리지 않았습니다.");
		
		btn.addActionListener(new MyEvent());
		//addActionListener를 implements 한 클래스, 사용 할 이벤트가 선언되어있는 클래스를 넣어주는거@
		
		panel.add(btn);
		panel.add(lbl);
		
		this.add(panel);
		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
	
	//내부클래스
	//내부에 선언된 클래스라
	//전역변수 사용 가능
	public class MyEvent implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource() == btn) {
				lbl.setText("버튼이 눌렸다!");
			}
			
		}
		
	}
	
	
	public static void main(String[] args) {
		new C_InnerClass();
	}
	
}

 


 

package com.event.test02;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class D_Method extends JFrame implements ActionListener{
	
	private JButton btn;
	private JLabel  lbl;
	
	public D_Method() {
		this.setSize(300, 200);
		this.setTitle("Method");
		
		//9일  12시 파일에서 19분부터 보면된다
		
		JPanel panel = new JPanel();
		btn = new JButton("버튼을 눌러보세요");
		lbl = new JLabel("아직 버튼이 눌리지 않았습니다.");
		
		btn.addActionListener(this);
		
		panel.add(btn);
		panel.add(lbl);
		
		this.add(panel);
		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
	
	public static void main(String[] args) {
		new D_Method();

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btn) {
			lbl.setText("버튼 눌림쓰!");
		}
		
	}

}

 


 

package com.event.test03;

public class MTest {

	public static void main(String[] args) {
		new MainFrame();
	}
	
}

 

package com.event.test03;

import java.awt.Color;

import javax.swing.JPanel;

public class Panel1 extends JPanel{

	public Panel1() {
		this.setSize(300, 200);
		this.setBackground(Color.blue);
		
		
		
	}
	
}

 

package com.event.test03;

import java.awt.Color;

import javax.swing.JPanel;

public class Panel2 extends JPanel{

	public Panel2() {
		this.setSize(300, 200);
		this.setBackground(Color.yellow);
		
		
		
	}
	
}

 

package com.event.test03;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainFrame extends JFrame{

	private JPanel panel;
	

	public MainFrame() {
		this.setSize(300, 200);

		panel = this.callPanel1();

		panel.addMouseListener(new MyMouseAdapter());
		
		this.add(panel);
		
		
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	//내부 클래스
	class MyMouseAdapter extends MouseAdapter {
		//보통 extends 하면 추상메소드를 전부 선언해주어야 하는데,
		//Adapter를 이용하면 원하는 메소드만 오버라이딩 하여 사용 할 수 있다.
		@Override
		public void mouseClicked(MouseEvent e) {
			replace();
		}		
		
	}
	
	
	public JPanel callPanel1() {
		panel = new Panel1();
		return panel;
	}
	
	public JPanel callPanel2() {
		panel = new Panel2();
		return panel;
	}
	
	public void replace() {
		this.remove(panel);
		this.panel = callPanel2();
		this.add(panel);
		this.repaint();
		
	}
	
	

}