티스토리 뷰
반응형
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); //보이게 할건지?
}
}
반응형
'JAVA' 카테고리의 다른 글
190712 Swing, JFrame 이용 계산기 (무식한버전) (0) | 2019.07.12 |
---|---|
190711 JAVA Frame 상속, 좌표값 팝업+label로 나타내기 (0) | 2019.07.11 |
190711 Interface (0) | 2019.07.11 |
190710 다형성 상속 예제 (0) | 2019.07.10 |
190709 상속, 다향성, 업캐스팅, 다운캐스팅 (0) | 2019.07.09 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 엑셀다운로드
- 정규식 특수문자
- 인텔리제이
- JSON
- js
- selectbox
- IntelliJ #gradle #tomcat #spring #springmvc
- PageNotFound - No mapping for GET
- 정규식
- JSON파싱
- JSON날짜
- SpringXmlModelInspection
- Failed to load resource: the server responded with a status of 404 (Not Found)
- jQuery
- no getter for property named
- poi 엑셀
- poi
- 정규식 숫자만
- spring error #
- select제어
- spring 엑셀
- Regex
- POI EXCEL
- Spring
- ''찾기
- 계좌번호정규식
- 정규식 한글만
- 이메일 정규식
- mybatis
- 공백찾기
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함