190425_Day14 복습,
버튼
checkbox
GUI => AWT => SWING
package Mission;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
public class CheckboxTest extends Frame
{
Checkbox cb1, cb2, cb3, cb4;
Label la;
TextArea ta;
Panel northp, southp;
CheckboxGroup genderGroup;
public CheckboxTest(String title)
{
super(title);
genderGroup = new CheckboxGroup();
cb1 = new Checkbox("사과");
cb1.setBackground(new Color(10, 200, 30));
cb2 = new Checkbox("딸기");
cb2.setBackground(new Color(10, 200, 30));
cb3 = new Checkbox("남자", genderGroup, true);
cb3.setBackground(new Color(220, 100, 30));
cb4 = new Checkbox("여자", genderGroup, false);
cb4.setBackground(new Color(220, 100, 30));
la = new Label("성별 : ");
ta = new TextArea();
Panel northp = new Panel();
//northp.setLayout( new FlowLayout ( ) ); // 기본이 Flow라 생략 가능
Panel southp = new Panel();
northp.add("사과",cb1);
northp.add("딸기",cb2);
northp.setBackground(new Color(10, 200, 30));
southp.add(la);
southp.add("남자",cb3);
southp.add("여자",cb4);
southp.setBackground(new Color(220, 100, 30));
// setLayout(new BorderLayout()); //기본이라 생략 가능
//보더 레이아웃의 경우에는 컴포넌트를 붙일 위치를 지정.
add("North", northp);
add("Center", ta);
add("South", southp);
//마무리( 프레임사이즈, 보이기 )
setSize(300,300);
setVisible(true);
}
public static void main(String[] args)
{
new CheckboxTest("체크박스테스트");
}
}
package j0425;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//Frame에 FlowLayout을 설정
//Frame에 두개의 버튼을 배치하시오
//두개의 버튼의 변수명 : bt1(라벨 : "안녕"), bt2(라벨 : "잘가")
//bt1클릭시 화면 콘솔에 "안녕"을 출력하시오
//bt2클릭시 화면 콘솔에 "잘가"를 출력하시오
public class ButtonEventTest extends Frame implements ActionListener
{
Button bt1,bt2;
Label la1,la2;
public ButtonEventTest()
{
bt1 = new Button("버튼1");
bt2 = new Button("버튼2");
la1 = new Label("안녕");
la2 = new Label("잘가");
setLayout(new FlowLayout());
add(la1);
add(bt1);
add(la2);
add(bt2);
setSize(300,300);
setVisible(true);
//소스 <= 연결자 => 핸들러
bt1.addActionListener(this);
bt2.addActionListener(new C());
}
/*
* public class Button
* {
* public void addActionListener(ActionListener 1)
* {
* while(true)
* {
* if(버튼이 눌렸다면)
* {
* l.actionPerformed();
* }
* }
* }
* }
*/
//생성자
@Override
public void actionPerformed(ActionEvent e) // 이벤트 핸들러
{//사건 발생시 실행할 기능 정의
System.out.println("안녕~!!");
// System.out.println("잘가~!!");
}
public static void main(String[] args)
{
new ButtonEventTest();
}
}
/===
package j0425;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class C implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
System.out.println("잘가~!!");
}
}
//이렇게도 가능 문자열 비교
package j0425;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//Frame에 FlowLayout을 설정
//Frame에 두개의 버튼을 배치하시오
//두개의 버튼의 변수명 : bt1(라벨 : "안녕"), bt2(라벨 : "잘가")
//bt1클릭시 화면 콘솔에 "안녕"을 출력하시오
//bt2클릭시 화면 콘솔에 "잘가"를 출력하시오
public class ButtonEventTest extends Frame implements ActionListener
{
Button bt1,bt2;
Label la1,la2;
public ButtonEventTest()
{
bt1 = new Button("버튼1");
bt2 = new Button("버튼2");
la1 = new Label("안녕");
la2 = new Label("잘가");
setLayout(new FlowLayout());
add(la1);
add(bt1);
add(la2);
add(bt2);
setSize(300,300);
setVisible(true);
//소스 <= 연결자 => 핸들러
bt1.addActionListener(this);
// bt2.addActionListener(new C());
bt2.addActionListener(this);
}
/*
* public class Button
* {
* public void addActionListener(ActionListener 1)
* {
* while(true)
* {
* if(버튼이 눌렸다면)
* {
* l.actionPerformed();
* }
* }
* }
* }
*/
//생성자
@Override
public void actionPerformed(ActionEvent e) // 이벤트 핸들러
{//사건 발생시 실행할 기능 정의
String str = e.getActionCommand();
if(str.equals("버튼1"))
{
System.out.println("안녕~!!");
}
if(str.equals("버튼2"))
{
System.out.println("잘가~!!");
}
}
public static void main(String[] args)
{
new ButtonEventTest();
}
}
//선생님 추천 코드
package com.encore.j0425;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/*
<이벤트처리> ---> 컴포넌트에 기능을 정의하자!!
1. 이벤트소스(컴포넌트)에 적용할 이벤트 분석
Button bt ------> ActionEvent
2. ActionListener(인터페이스) ------> implements ActionListener
3. public void actionPerformed(ActionEvent e){} ===> 오버라이딩
4. 연결자메소드 등록 [이벤트소스] <------- 연결 -------> [이벤트처리부]
이벤트소스명.add인터페이스명(이벤트처리부의 위치);
bt.addActionListener(this);
<이벤트처리 방법>
1. 기능을 적용할 컴포넌트 찾기(이벤트 소스 찾기!!): bt1, bt2
----------
자료형 Button
2. Button(이벤트소스)클래스의 메소드 중 add~Listener()메소드 찾기!!
==> addActionListener() !!
3. 인터페이스 (add제거) ==> ActionListener : implements 하기!!
===> 메소드 오버라이딩 (이벤트 핸들러:기능정의)
4. 연결자 등록!! (2번의 add~Listener()메소드!! )
*/
public class ButtonEventTest extends Frame implements ActionListener{
Button bt1,bt2;//[이벤트 소스]
public ButtonEventTest() {
setTitle("버튼이벤트");
bt1 = new Button("안녕");//203호
bt2 = new Button("잘가");//303호
setLayout(new FlowLayout());
add(bt1);
add(bt2);
setSize(300,300);
setVisible(true);
// 소스 <----연결----> 핸들러
//이벤트소스명.add~Listener(핸들러위치);
bt1.addActionListener(this);
//C c = new C();
//bt2.addActionListener(new C());//(c);
bt2.addActionListener(this);
/*
public class Button{
public void addActionListener(ActionListener l){
while(true){
if(버튼이 눌렸다면){
l.actionPerformed(이벤트소스의 정보);
}
}
}
}
*/
}//생성자
@Override
public void actionPerformed(ActionEvent e) {//[이벤트 핸들러]
//사건 발생시 실행할 기능 정의.
String str = e.getActionCommand(); //버튼의 라벨 문자열 얻기
//System.out.println("STR="+ str);
Object ob = e.getSource();
//==> 이벤트를 발생시킨 컴포넌트(EventSource)의 주소를 리턴하는 메소드
//'안녕'버튼 클릭시 Object ob = 203호; Object ob = bt1;
//'잘가'버튼 클릭시 Object ob = 303호; Object ob = bt2;
//if(str.equals("안녕"))
if(ob==bt1) //주소를 비교
System.out.println("안녕~!!");//0,1
//if(str.equals("잘가"))
else if(ob==bt2)
System.out.println("잘가~!!");//0,1
}
public static void main(String[] args) {
new ButtonEventTest();
}
}
'클라우드 기반 웹 개발자 과정 공부 > JAVA' 카테고리의 다른 글
190429_Day16 복습, Swing (0) | 2019.04.29 |
---|---|
190426_Day15 복습, 이벤트 처리 (0) | 2019.04.26 |
190424_Day13 복습, 예외처리, 오버라이딩, 내부클래스 (0) | 2019.04.24 |
190423_Day12 복습, Vector실습, 예외처리 (0) | 2019.04.23 |
190422_Day11 복습, String, Vector (0) | 2019.04.22 |