Java - AWT

2025. 2. 13. 13:53·Java

GUI : Window, 웹 브라우저, 아이콘, 버튼, AWT, Swing
CLI : CMD, Scanner, Linux, Database
VUI : 음성 인터페이스 -> 네비게이션, 시리, 지니
NUI : 사용자 인터페이스 -> 음성, 제스처, 시선, 생체인식  
AUI : 소리 사용자 인터페이스 

1. 기초 

package awt;

import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class awt1 {
	public static void main(String[] args) {
		new awt_box().window();
	}
}

//AWT : Frame > Bound > Object
class awt_box{
	public void window() {
		Frame fr = new Frame();	//Window 부분 
		fr.setTitle("AWT 기초화면");	//타이틀 제목 
		fr.setVisible(true);	//화면을 출력 
		fr.setLayout(null);		//제작자가 원하는 위치를 설정 
		//화면 형태 (화면 x축, 화면 y축, 가로 크기, 세로 크기)
		fr.setBounds(150,150,500,500);
		
		//화면에 Object를 생성하는 코드 
		Button btn = new Button("클릭");
		btn.setBounds(40,40,100,30);
		
		Button btn2 = new Button("닫기");
		btn2.setBounds(40, 90, 100, 30);
		
		//사용자가 입력하는 오브젝트 
		TextField mid = new TextField(5);	//파라미터 : 입력가능개수	
		mid.setBounds(40, 200, 100, 30);
		
		TextField mpass = new TextField(5);
		mpass.setBounds(150, 200, 100, 30);
		mpass.setEchoChar('*');		//setEchoChar : 입력한 문자를 다른 문자로 출력해주는 역할 (외따옴표 사용)
		
		Label msg = new Label("고객명 출력!");
		msg.setBounds(170, 40, 100, 30);
		msg.setBackground(Color.pink);	//배경 색상 (RGB코드가 아니라 색상 단어로 써야함)
		msg.setForeground(Color.blue);	//폰트 색상 
		
		fr.add(mpass);
		fr.add(mid);
		fr.add(msg);
		fr.add(btn);
		fr.add(btn2);
		
		/*
		addActionListener = ES (addEventListener)
		actionPerformed : 버튼 클릭시 작동방식 핸들링하는 메소드 
		*/
		//클릭이라는 버튼을 클릭시 작동되는 메소드 
		btn.addActionListener(new ActionListener() {	//addActionListener > ActionListener > 자동생성
			String a = "홍길동";
			
			@Override
			public void actionPerformed(ActionEvent e) {
				msg.setText(a);
				
			}
		});
		
		btn2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
	}
}
  • 윈도우 생성 
    • Frame fr = new Frame();
    • fr.setVisible(true);    화면 출력
    • fr.setBounds(화면 x축, 화면 y축, 가로 크기, 세로 크기);    화면 크기 설정 
  • 윈도우 닫기 활성화
    • 메소드 안에
      • fr.addWindowListener(new event());
    • 외부 클래스 

class event extends WindowAdapter{

@Override

public void windowClosing(WindowEvent e) { //화면에 X 클릭시 활성화

System.exit(0);

}

}

  •  체크박스
    • Checkbox ck = new Checkbox("text");
    • fr.setBounds 크기는 텍스트 + 체크박스의 크기
  • 셀렉트
    • Choice cc = new Choice();
    • cc.add("option"); 옵션 추가
  • 라디오
    • CheckboxGroup radio = new CheckboxGroup();
    • Checkbox c1 = new Checkbox("동의함", false,radio);
      Checkbox c2 = new Checkbox("동의안함", true,radio);
      • true : 미체크 / false : 체크
    • setBounds 로 위치 설정을 하려면 fr.setLayout(null); 을 써줘야함
  • 텍스트 출력
    • Label lb = new Label(); 
  • 텍스트 입력
    • TextField tf = new TextField(); 짧은 글 입력 // 한 줄
      • tf.setEchoChar('*'); 비밀번호 가리기
    • TextArea ta = new TextArea("긴 글 입력"); 긴 글 입력 // 여러 줄
  • 버튼
    • Button btn = new Button("버튼");
    • 버튼 클릭시 작동하는 메소드

btn.addActionListener(new ActionListener() { //addActionListener > ActionListener > 자동생성

String a = "홍길동";

@Override

public void actionPerformed(ActionEvent e) {

msg.setText(a);

}

});

  • 객체 인스턴스화 후 프레임에 add 하기 !

😊응용문제

아이디 및 패스워드를 입력 후 로그인 버튼을 클릭시 다음과 같이 결과가 나오도록 코드를 작성
아이다 : hong 패스워드 : a1234[로그인하셨습니다.]
메시지가 출력되는 곳애 단어가 노출되도록하며,
그 외 사용자 입력시 ["아이디 및 패스워드를 확인하세요"]라고 출력되도록 합니다.
단, 아이디 또는 패스워드를 미입력 후 로그인 버튼을 클릭하게 되면
[아이디를 입력하세요] 또는 [패스워드를 입력하세요]라고 메시지 출력

내 코드 

package awt;
import java.awt.Button;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class awt3 {
	public static void main(String[] args) {
		new awt3_login();
	}
}

class awt3_login{
	public awt3_login() {
		Frame f = new Frame(); // Window 부분
		f.setTitle("로그인"); 
		f.setVisible(true);
		f.setLayout(null); 
		f.setBounds(150, 150, 350, 200);

		Label msg1 = new Label("아이디");
		msg1.setBounds(25, 50, 50, 30);

		TextField mid = new TextField();
		mid.setBounds(100, 50, 100, 30);

		Label msg2 = new Label("패스워드");
		msg2.setBounds(20, 85, 50, 30);

		TextField mpass = new TextField();
		mpass.setBounds(100, 85, 100, 30);
		mpass.setEchoChar('*');

		Button btn = new Button("로그인");
		btn.setBounds(210, 50, 100, 70);

		Label msg3 = new Label("");
		msg3.setBounds(80, 130, 180, 30);
//		msg3.setBackground(Color.pink);

		f.setVisible(true);
		f.setLayout(null);

		f.add(msg3);
		f.add(msg1);
		f.add(msg2);
		f.add(mid);
		f.add(mpass);
		f.add(btn);

		f.addWindowListener(new event()); // 종료

		btn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String userid = mid.getText();
				String userpw = mpass.getText();

				if (userid.equals("")) {
					msg3.setText("아이디를 입력하세요.");
				} else if (userpw.equals("")) {
					msg3.setText("패스워드를 입력하세요.");
				} else if (userid.equals("hong") && userpw.equals("a1234")) {
					msg3.setText("로그인하셨습니다.");
				} else {
					msg3.setText("아이디 및 패스워드를 확인하세요.");
				}

			}
		});

	}
}

선생님 코드

실행파일 

package awt;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

//awt_design.java와 연계 
public class awt3_t {
	public static void main(String[] args) {
		new awt3_login_t();
	}
}

class awt3_login_t extends awt_design{
	public awt3_login_t() {
		this.design(40, 40, 450, 200);
		this.fr.addWindowListener(new CloseFrame());	//닫기 
	}
}

class CloseFrame extends WindowAdapter{
	@Override
	public void windowClosing(WindowEvent e) {
		System.exit(0);
	}
}

디자인파일

package awt;

import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

//awt3_t Frame Design 파트 
public class awt_design {
	Frame fr = new Frame();
	Button btn = null;		//로그인 버튼 
	TextField mid = null;	//아이디를 받는 오브젝트 
	TextField mpass = null;	//패스워드를 받는 오브젝트 
	Label msg = null;		//메세지를 출력하는 오브젝트 
	Label text1 = new Label("아이디");	
	Label text2 = new Label("패스워드");	

	
	public void design(int x, int y, int width, int height) {
		this.fr.setBounds(x,y,width,height);
		this.fr.setLayout(null);
		this.fr.setVisible(true);
		this.btn = new Button("로그인");
		this.mid = new TextField();
		this.mpass = new TextField();
		this.msg = new Label();
		
		//해당 오브젝트 위치 선정 및 타이틀 적용
		this.fr.setTitle("회원 로그인");
		//아이디 
		this.text1.setBounds(20, 40, 100, 30);
		this.mid.setBounds(150, 40, 200, 30);
		//패스워드
		this.text2.setBounds(20, 70, 100, 30);
		this.mpass.setBounds(150, 70, 200, 30);
		this.mpass.setEchoChar('*');
		
		this.btn.setBounds(360,40,80,60);
		this.msg.setBounds(20, 120, 400, 30);
		this.msg.setForeground(Color.red);	//안내 메세지 붉은색으로 처리 
		
		this.fr.add(this.text1);
		this.fr.add(this.text2);
		this.fr.add(this.mid);
		this.fr.add(this.mpass);
		this.fr.add(this.btn);
		this.fr.add(this.msg);
		
		this.clicks();
		
		
	}
	public void clicks() {	//버튼 핸들링만 처리하는 메소드 
		JOptionPane alert = new JOptionPane();	//Swing API용 
		
		//addActionListener => this.사용불가능 
		//버튼 클릭시 해당 메소드 작동 
		this.btn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String id = mid.getText();	//변수명 따로 사용해야함~!
				String pass = mpass.getText();
				System.out.println(mid);
				if(id.equals("")||pass.equals("")) {
//					msg.setText("아이디 및 패스워드를 입력하세요");	//이 영역에서는 this.사용불가능
					alert.showMessageDialog(null, "아이디 및 패스워드를 입력하세요");
				}else if(id.equals("hong")&&pass.equals("a1234")) {
					msg.setText("로그인되었습니다.");
				}else {
					msg.setText("아이디 및 패스워드를 확인하세요.");
				}
			}
		});
	}
}
저작자표시 비영리 변경금지 (새창열림)
'Java' 카테고리의 다른 글
  • 복습11 - Network
  • JAVA - SWING
  • Java - network (UDP)
  • Java - network (FTP)
9na0
9na0
응애
  • 9na0
    구나딩
    9na0
  • 전체
    오늘
    어제
    • 분류 전체보기 (211)
      • Web (118)
      • Java (28)
      • 데이터베이스 (14)
      • 세팅 (12)
      • 과제 (3)
      • 쪽지시험 (2)
      • 정보처리기사 (4)
      • 서버 (25)
  • 블로그 메뉴

    • 링크

      • 포폴
      • 구깃
    • 공지사항

    • 인기 글

    • 태그

      net3
      re_java10
      ab1
      net5~10
      re2
      file25_t
      file25
      net1
      spring-boot
      io_dto
      file24
      net4
      net2
      macbook pro m4
      java_io1~10
      noticewriteok
      Oracle
      exam1_1~10
      datalist
      notice_writer
    • 최근 댓글

    • 최근 글

    • hELLO· Designed By정상우.v4.10.3
    9na0
    Java - AWT
    상단으로

    티스토리툴바