티스토리 뷰

JAVA

190711 GUI, EventHandler 예제

猫猫 2019. 7. 11. 12:01
반응형
package GUITEST;

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

import javax.swing.JFrame;

public class MouseSample extends Frame  {
	int i =0; 
	Button button;
	Label label;
	
	public MouseSample() {
		//컴포넌트 생성
		button = new Button("클릭");
		label = new Label("0"); //컴포넌트를 프레임에 배치
		this.setLayout(new FlowLayout()); //layout 클래스에 set 하는것  == setter
		this.add(button); //버튼 추가
		this.add(label); //label 추가
//		setDefalutCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//버튼 콤포넌트에 이벤트 핸들러 장착
		button.addMouseListener(new MouseAdapter() {//클래스 이름없이 어댑터 클래스 생성(익명) 생성과 동시에 구현
			@Override
			public void mouseClicked(MouseEvent e) { //마우스가 눌렸을때 동장할 내용 재정의(오버라이드)
				label.setText(Integer.toString(i+=1)); //클릭시 값이 계속 증가해야 함, 레이블에 문자형태로 표시되도록 형 변환 지정. 
			}
		});

		//마우스 해당 프레임 영역안에 위치시키면 배경색이 녹색으로 변경
		//마우스가 프레임 영역 밖으로 나가면 빨간색이 되도록
		//프레임에 이벤트 핸들러 장착		
		this.addMouseListener(new MouseAdapter() {//어댑터클래스(익명) 생성하자마자 바로 구현
			@Override
			public void mouseEntered(MouseEvent e) {
				setBackground(new Color(60, 70, 0)); //RGB
				System.out.println(this); //어떤지 보려고 확인차
				System.out.println(MouseSample.this); //확인차
			}
			@Override
			public void mouseExited(MouseEvent e) {
				setBackground(new Color(1, 50, 60)); //RGB
			}
		});		

	}
	
		public static void main(String[] args) {
			MouseSample test = new MouseSample(); //객체생성
			test.setSize(600, 400); //객체 사이즈 width, height 생성
			test.setVisible(true); //보이게 할건지? 

	}

}

 

 

와! GUI!

와 API! 아시는구나!

 

 

버튼으로 백그라운드 RGB색 -5씩 색변경하기. 

초기 값은 흰색으로 

 

package GUITEST;

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

import javax.swing.JFrame;

public class MouseSample extends Frame  {
	int i =0; 
	int R = 255;
	int G = 255;
	int B = 255;
	Button button;
	Button Rbutton;
	Button Gbutton;
	Button Bbutton;
	Label label;
	
	public MouseSample() {
		
		//컴포넌트 생성
		button = new Button("클릭");
		Rbutton = new Button("-5 R 색변경");
		Gbutton = new Button("-5 G 색변경");
		Bbutton = new Button("-5 B 색변경");
		label = new Label("0"); //컴포넌트를 프레임에 배치
		this.setLayout(new FlowLayout()); //layout 클래스에 set 하는것  == setter
		this.add(button); //버튼 추가
		this.add(Rbutton);
		this.add(Gbutton);
		this.add(Bbutton);
		this.add(label); //label 추가
		this.setBackground(new Color(255,255,255));
//		setDefalutCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//버튼 콤포넌트에 이벤트 핸들러 장착
		button.addMouseListener(new MouseAdapter() {//클래스 이름없이 어댑터 클래스 생성(익명) 생성과 동시에 구현
			@Override
			public void mouseClicked(MouseEvent e) { //마우스가 눌렸을때 동장할 내용 재정의(오버라이드)
				label.setText(Integer.toString(i+=1)); //클릭시 값이 계속 증가해야 함, 레이블에 문자형태로 표시되도록 형 변환 지정. 
			}
		});
		
		//버튼 콤포넌트에 이벤트 핸들러 장착
		Rbutton.addMouseListener(new MouseAdapter() {//클래스 이름없이 어댑터 클래스 생성(익명) 생성과 동시에 구현
			@Override
			public void mouseClicked(MouseEvent e) { //마우스가 눌렸을때 동장할 내용 재정의(오버라이드)
				setBackground(new Color((R-=5), G, B)); //RGB
			}
		});
		Gbutton.addMouseListener(new MouseAdapter() {//클래스 이름없이 어댑터 클래스 생성(익명) 생성과 동시에 구현
			@Override
			public void mouseClicked(MouseEvent e) { //마우스가 눌렸을때 동장할 내용 재정의(오버라이드)
				setBackground(new Color(R,(G-=5), B)); //RGB
			}
		});
		Bbutton.addMouseListener(new MouseAdapter() {//클래스 이름없이 어댑터 클래스 생성(익명) 생성과 동시에 구현
			@Override
			public void mouseClicked(MouseEvent e) { //마우스가 눌렸을때 동장할 내용 재정의(오버라이드)
				setBackground(new Color(R, G, (B-=5))); //RGB
			}
		});
}
	
		public static void main(String[] args) {
			MouseSample test = new MouseSample(); //객체생성
			test.setSize(600, 400); //객체 사이즈 width, height 생성
			test.setVisible(true); //보이게 할건지? 

	}

}
반응형