Demo entry 6418091

111

   

Submitted by 11 on May 25, 2017 at 19:16
Language: Java. Code size: 3.1 kB.

package day18_gui;

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

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/*
	1. 두 숫자를 입력받아 사칙연산(덧셈, 뺄셈, 곱셈, 나눗셈) 해보세요.
*/
class MyFrame14_1 extends JFrame {
	JTextField txtNum1 = new JTextField(20);
	JTextField txtNum2 = new JTextField(20);
	JTextField txtRes  = new JTextField(20);
	
	String op = "?";
	String title = "";
	
	JLabel lblOp  = new JLabel(op);
	JLabel lblRes = new JLabel(" = ");
	
	JPanel pnlNorth  = new JPanel();
	JPanel pnlCenter = new JPanel();
	
	JButton btnAdd = new JButton(" + ");
	JButton btnSub = new JButton(" - ");
	JButton btnMul = new JButton(" * ");
	JButton btnDiv = new JButton(" / ");
	
	public MyFrame14_1() {
		super("계산기");
		
		setLayout(new BorderLayout());
		
		pnlNorth.add(txtNum1);
		pnlNorth.add(lblOp);
		pnlNorth.add(txtNum2);
		pnlNorth.add(lblRes);
		pnlNorth.add(txtRes);
		
		pnlCenter.add(btnAdd);
		pnlCenter.add(btnSub);
		pnlCenter.add(btnMul);
		pnlCenter.add(btnDiv);
		
		add(pnlNorth, BorderLayout.NORTH);
		add(pnlCenter, BorderLayout.CENTER);
		
		class MyListener implements ActionListener {

			@Override
			public void actionPerformed(ActionEvent e) {
				int num1 = 0;
				int num2 = 0;
				int res  = 0;
				
				if(!txtNum1.getText().isEmpty()) {
					num1 = Integer.parseInt(txtNum1.getText());
				}
				
				if(!txtNum2.getText().isEmpty()) {
					num2 = Integer.parseInt(txtNum2.getText());
				}
				
				if(e.getSource() == btnAdd) {
					op = " + ";
					lblOp.setText(op);
					title = "덧셈 연산";
					setTitle(title);
					
					res = num1 + num2;
					txtRes.setText(String.valueOf(res));
				}
				else if(e.getSource() == btnSub) {
					op = " - ";
					lblOp.setText(op);
					title = "뺄셈 연산";
					setTitle(title);
					
					res = num1 - num2;
					txtRes.setText(String.valueOf(res));
				}
				else if(e.getSource() == btnMul) {
					op = " * ";
					lblOp.setText(op);
					title = "곱셈 연산";
					setTitle(title);
					
					res = num1 * num2;
					txtRes.setText(String.valueOf(res));
				}
				else if(e.getSource() == btnDiv) {
					op = " / ";
					lblOp.setText(op);
					title = "나눗셈 연산";
					setTitle(title);
					
					if(num2 != 0) {
						res = num1 / num2;
						txtRes.setText(String.valueOf(res));
					}
					else {
						// JOptionPane.showMessageDialog(MyFrame14_1.this, "0으로 나눌 수 없습니다.");
						txtRes.setText("0으로 나눌 수 없습니다.");
					}
				}
			}
		}
		
		MyListener ml = new MyListener();
		btnAdd.addActionListener(ml);
		btnSub.addActionListener(ml);
		btnMul.addActionListener(ml);
		btnDiv.addActionListener(ml);
		
		// 시스템 메뉴 중 종료버튼을 누르면 프로그램 종료하기
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		setSize(800, 110);
		setVisible(true);
	}
}

public class Homework14_1 {
	public static void main(String[] args) {
		new MyFrame14_1();
	}
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).