Java(GUI)_Event_32~33

2024. 11. 7. 18:00·Java/기초 내용 정리

2024-11-07

 

 

 

 

 

 

 

package basic;

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

import javax.swing.*;

public class Ex32_Event extends JFrame {

	public Ex32_Event() {
			
			setTitle("간단한 계산기");
			
			//  1. 컨테이너 3개를 만들자.
			JPanel container1 = new JPanel();
			JPanel container2 = new JPanel();
			JPanel container3 = new JPanel();
			
			//  2. 컴포넌트를 만들자
			//  2-1. 상단에 들어갈 컨포넌트를 만들자.
			JLabel jl1 = new JLabel("수 1 : ");
			JTextField jtf1 = new JTextField(5);
			
			JLabel jl2 = new JLabel("수 2 : ");
			JTextField jtf2 = new JTextField(5);
			
			JLabel jl3 = new JLabel("연산자 : ");
			JTextField jtf3 = new JTextField(1);
			
			//  2-2. 중앙에 들어갈 컨포넌트를 만들자.
			JTextArea jta = new JTextArea(5, 20);
			
			JScrollPane jsp = new JScrollPane(
					jta,
					ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
					ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
			
			//  2-3. 하단에 들어갈 컴포넌트를 만들자.
			JButton button1 = new JButton("계 산");
			JButton button2 = new JButton("종 료");
			JButton button3 = new JButton("취 소");
			
			//  3. 컨포넌트를 컨테이너에 올려주어야 한다.
			//  3-1. 상단 컨테이너에 들어갈 컨포넌트들을 올려 주자.
			container1.add(jl1); container1.add(jtf1);
			container1.add(jl2); container1.add(jtf2);
			container1.add(jl3); container1.add(jtf3);
			
			//  3-2. 중앙 컨테이너에 들어갈 컨포넌트들을 올려 주자.
			container2.add(jsp);
			
			//  3-3. 하단 컨테이너에 들어갈 컨포넌트들을 올려 주자.
			container3.add(button1);
			container3.add(button2);
			container3.add(button3);
			
			//  4. 컨테이너를 프레임에 올려주어야 한다.
			//  컨테이너를 올릴 떄 배치를 하여 올려주면 됨.
			add(container1, BorderLayout.NORTH);
			add(container2, BorderLayout.CENTER);
			add(container3, BorderLayout.SOUTH);
			
			setBounds(100, 100, 100, 300);
			
			//  pack(); : 프레임의 크기를 조절해 주는 메서드.
			pack();
			
			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			
			setVisible(true);
			
			//  5. 이벤트 처리 작업
			//  5-1. 계산버튼(button1)을 클릭했을 때 이벤트 발생.
			button1.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					
					int su1 = Integer.parseInt(jtf1.getText());
					int su2 = Integer.parseInt(jtf2.getText());
					String str = jtf3.getText();
					
					String result = "";
					
					switch(str) {
						case "+" :
							result = "결과 : " + su1 + " + " + su2 + " = " + (su1 + su2);
							break;
						case "-" :
							result = "결과 : " + su1 + " - " + su2 + " = " + (su1 - su2);
							break;
						case "*" :
							result = "결과 : " + su1 + " * " + su2 + " = " + (su1 * su2);
							break;
						case "/" :
							result = "결과 : " + su1 + " / " + su2 + " = " + (su1 / su2);
							break;
					}	//  switch ~ case 문 end
					
					jta.append(result + "\n");
					
					jtf1.setText(""); jtf2.setText("");
					jtf3.setText(null);
					
					jtf1.requestFocus();
				}
			});
		
			//  5-2. 종료버튼(button2)을 클릭했을 때 이벤트 발생.
			button2.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					
					System.exit(0);
				}
			});			
			
			//  5-3. 취소버튼(button3)을 클릭했을 때 이벤트 발생.
			button3.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
			
					//  전체 컴포넌트의 값을 clear 시키는 작업
					jtf1.setText(""); jtf2.setText("");
					jtf3.setText(null); jta.setText(null);
					
					jtf1.requestFocus();
				}
			});
	}
	
	public static void main(String[] args) {
		
		new Ex32_Event();
	}
}

 

package basic;

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

import javax.swing.*;

public class Ex33_Event extends JFrame {

	public Ex33_Event() {
	
setTitle("간단한 계산기");
		
		//  1. 컨테이너 3개를 만들자.
		JPanel container1 = new JPanel();
		JPanel container2 = new JPanel();
		JPanel container3 = new JPanel();
		
		//  2. 컴포넌트를 만들자
		//  2-1. 상단에 들어갈 컨포넌트를 만들자.
		JLabel jl1 = new JLabel("수 1 : ");
		JTextField jtf1 = new JTextField(5);
		
		JLabel jl2 = new JLabel("수 2 : ");
		JTextField jtf2 = new JTextField(5);
		
		//  연산자 라디오버튼
		JLabel jl3 = new JLabel("연산자 : ");
		JRadioButton plus = new JRadioButton("+");
		JRadioButton minus = new JRadioButton("-");
		JRadioButton multi = new JRadioButton("*");
		JRadioButton div = new JRadioButton("/");
		
		ButtonGroup group = new ButtonGroup();
		
		group.add(plus); group.add(minus);
		group.add(multi); group.add(div);
		
		//  2-2. 중앙에 들어갈 컨포넌트를 만들자.
		JTextArea jta = new JTextArea(5, 20);
		
		JScrollPane jsp = new JScrollPane(
				jta,
				ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		
		//  2-3. 하단에 들어갈 컴포넌트를 만들자.
		JButton button1 = new JButton("계 산");
		JButton button2 = new JButton("종 료");
		JButton button3 = new JButton("취 소");
		
		//  3. 컨포넌트를 컨테이너에 올려주어야 한다.
		//  3-1. 상단 컨테이너에 들어갈 컨포넌트들을 올려 주자.
		container1.add(jl1); container1.add(jtf1);
		container1.add(jl2); container1.add(jtf2);
		container1.add(jl3); 
		
		//  연산자 라디오버튼
		container1.add(plus); container1.add(minus);		
		container1.add(multi); container1.add(div);		
		
		//  3-2. 중앙 컨테이너에 들어갈 컨포넌트들을 올려 주자.
		container2.add(jsp);
		
		//  3-3. 하단 컨테이너에 들어갈 컨포넌트들을 올려 주자.
		container3.add(button1);
		container3.add(button2);
		container3.add(button3);
		
		//  4. 컨테이너를 프레임에 올려주어야 한다.
		//  컨테이너를 올릴 떄 배치를 하여 올려주면 됨.
		add(container1, BorderLayout.NORTH);
		add(container2, BorderLayout.CENTER);
		add(container3, BorderLayout.SOUTH);
		
		setBounds(100, 100, 100, 300);
		
		//  pack(); : 프레임의 크기를 조절해 주는 메서드.
		pack();
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		setVisible(true);
		
		//  5. 이벤트 처리 작업
		//  5-1. 계산버튼(button1)을 클릭했을 때 이벤트 발생.
		button1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
		
				int su1 = Integer.parseInt(jtf1.getText());
				int su2 = Integer.parseInt(jtf2.getText());
				
				String result = "";
				
				if(plus.isSelected()) {
					result = "결과 : " + su1 + " + " + su2 + " = " + (su1 + su2);
				}else if (minus.isSelected()) {
					result = "결과 : " + su1 + " - " + su2 + " = " + (su1 - su2);
				}else if (multi.isSelected()) {
					result = "결과 : " + su1 + " * " + su2 + " = " + (su1 * su2);
				}else if (div.isSelected()) {
					result = "결과 : " + su1 + " / " + su2 + " = " + (su1 / su2);
				}
				
				jta.append(result + "\n");
				
				//  전체 컴포넌트의 값을 clear 시키는 작업
				jtf1.setText(""); jtf2.setText("");
				
				//  라디오 버튼도 초기화가 되어야 한다.
				group.clearSelection();
				
				jtf1.requestFocus();
				}
			});
		
		//  5-2. 종료버튼(button2)을 클릭했을 때 이벤트 발생.
		button2.addActionListener(new ActionListener() {
					
				@Override
				public void actionPerformed(ActionEvent e) {
						
					System.exit(0);
				}
			});			
				
		//  5-3. 취소버튼(button3)을 클릭했을 때 이벤트 발생.
		button3.addActionListener(new ActionListener() {
					
				@Override
				public void actionPerformed(ActionEvent e) {
				
						//  전체 컴포넌트의 값을 clear 시키는 작업
						jtf1.setText(""); jtf2.setText("");
						group.clearSelection();
						
						jta.setText(null);
						
						jtf1.requestFocus();
				}
			});
	}
	
	public static void main(String[] args) {
		
		new Ex33_Event();
	}
}

'Java > 기초 내용 정리' 카테고리의 다른 글

Java(Collection)_06  (0) 2024.11.08
Java(Collection)_05  (0) 2024.11.08
Java(GUI)_Event_31  (0) 2024.11.07
Java(GUI)_Event_30  (0) 2024.11.07
Java(GUI)_Event_29  (0) 2024.11.07
'Java/기초 내용 정리' 카테고리의 다른 글
  • Java(Collection)_06
  • Java(Collection)_05
  • Java(GUI)_Event_31
  • Java(GUI)_Event_30
mw41817
mw41817
일생의 개발 기록 저장소
  • mw41817
    IT 개발 일지
    mw41817
    • Index (487)
      • HTML (36)
        • 기초 내용 정리 (36)
      • CSS (29)
        • 기초 내용 정리 (29)
      • JavaScript (60)
        • 기초 내용 정리 (60)
      • JQuery (38)
        • 기초 내용 정리 (38)
      • Java (232)
        • 기초 내용 정리 (232)
      • JSP (46)
        • 기초 내용 정리 (46)
      • Spring, Boot (31)
        • 기초 내용 정리 (31)
      • DB (5)
        • Oracle SQL (5)
      • Code WorkBook (6)
        • programmers (6)
        • Baekjoon (0)
      • 기타 (1)
        • 유용한 사이트 (3)
  • 전체
    오늘
    어제
  • 글쓰기 관리
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
    • 카테고리
    • 주인장 GitHub
  • 공지사항

  • 인기 글

  • 태그

    html #코딩 #프로그래밍 #기초
  • 최근 댓글

  • hELLO· Designed By정상우.v4.10.0
mw41817
Java(GUI)_Event_32~33
상단으로

티스토리툴바