imported>healu1423 |
imported>healu1423 |
| Line 35: |
Line 35: |
| | | |
| === 김용준 === | | === 김용준 === |
| package Calculator_140728;
| |
| | | |
| import java.awt.BorderLayout;
| |
| import java.awt.Container;
| |
| import java.awt.Dimension;
| |
| import java.awt.Font;
| |
| import java.awt.GridLayout;
| |
| import java.awt.Insets;
| |
| 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;
| |
| import Calculator_140728.base;
| |
|
| |
|
| |
| public class gui_v1 {
| |
| public static void main( String [] args){
| |
| new Calculator("계산기");
| |
| }
| |
| }
| |
|
| |
|
| |
| class Calculator extends JFrame{
| |
| String[] strOpt = { "/", "*", "-", "+", "C", "="};
| |
| String strFm = ""; //수식라벨을 위한 스트링
| |
| String strTf = ""; //텍스트 필드를 위한 스트링
| |
| String strCal = ""; //계산을 위한 스트링
| |
| String result = "0"; //결과값을 위한 스트링
| |
|
| |
| //=을 위한 변수
| |
| Boolean countE = false;// 연속 = 기능 비활성화
| |
| int tmpE;
| |
| String strE;
| |
| //연속 연산을 위한 변수
| |
| Boolean countO = false;// 연속 연산 기능 비활성화
| |
| Boolean countP = false;// 연산자 변경
| |
| String tmpO;//이전 입력받은 수
| |
|
| |
|
| |
| JButton[] num = new JButton[10];
| |
| JButton[] opt = new JButton[strOpt.length];
| |
|
| |
| JLabel fm = new JLabel(); //현재 계산식 보는 라벨
| |
| JTextField tf = new JTextField("0");//숫자와 연산
| |
|
| |
| JPanel see = new JPanel();
| |
| JPanel input_num = new JPanel();
| |
| JPanel input_opt = new JPanel();
| |
|
| |
| private void compose() {
| |
| Container container = this.getContentPane();
| |
|
| |
| container.setLayout(new BorderLayout(4,7));
| |
|
| |
| //연산자
| |
| input_opt.setLayout(new GridLayout(4,1,10,10));
| |
| for(int i = 0; i < opt.length; i++)
| |
| {
| |
| opt[i] = new JButton(strOpt[i]);
| |
| opt[i].setFont(new Font("굴림", Font.BOLD, 15));
| |
| if(i < 4) input_opt.add(opt[i]);
| |
| }
| |
| container.add("East",input_opt);
| |
|
| |
| //라벨과 텍스트박스
| |
| see.setLayout(new GridLayout(2,1,0,2));
| |
| see.add(fm);
| |
| see.add(tf);
| |
| fm.setFont(new Font("굴림", 0, 12));
| |
| tf.setFont(new Font("굴림", Font.BOLD, 21));
| |
| container.add("North",see);
| |
|
| |
| //숫자 부분
| |
| input_num.setLayout(new GridLayout(4,3,3,10));
| |
| for(int i = 0 ; i <= 9 ; i++)
| |
| {
| |
| num[i] = new JButton(Integer.toString(i));
| |
| num[i].setFont(new Font("굴림", Font.BOLD, 19));
| |
| }
| |
| for(int i = 2 ; i >= 0 ; i--)
| |
| for(int j = 1 ; j <= 3 ; j++) input_num.add(num[3*i+j]);
| |
| input_num.add(num[0]);
| |
| input_num.add(opt[4]);
| |
| input_num.add(opt[5]);
| |
| container.add("Center",input_num);
| |
|
| |
| }
| |
|
| |
| public Insets getInsets()
| |
| {//여백을 주기 위한 메소드 오보라이딩
| |
| Insets i = new Insets(30,15,15,15);
| |
| return i;
| |
| }
| |
|
| |
| private void setevent() {
| |
| for(JButton e : num) e.addActionListener(new MyAction());
| |
| for(JButton e : opt) e.addActionListener(new MyAction());
| |
| }
| |
|
| |
|
| |
| class MyAction implements ActionListener{
| |
|
| |
| @Override
| |
| public void actionPerformed(ActionEvent e) {
| |
| Object who = e.getSource();
| |
|
| |
| for(int i = 0; i < num.length; i++){
| |
| if(who == num[i]){
| |
| strTf += i;
| |
| tf.setText(strTf);
| |
| countE = false;// 연속 = 기능 비활성화
| |
| countP = false;// 연산자 변경 비활성화
| |
| }
| |
| }
| |
|
| |
| for(int i = 0; i< opt.length; i++)
| |
| if(who == opt[i]) operation(i);
| |
|
| |
| }
| |
| private void operation(int nowOpt){
| |
| switch(nowOpt)
| |
| {
| |
| default: // 연산자들 기능 구현
| |
| if (tf.getText().equals("0"))
| |
| {// 초기 0값
| |
| strFm = "0 "+strOpt[nowOpt]+" ";
| |
| fm.setText(strFm);
| |
| }
| |
| else if(tf.getText().equals(result) && countP)
| |
| {// 이전 값과 같을 때 연산자 변경
| |
| try{
| |
| strFm = fm.getText().substring(0, fm.getText().length()-2) + strOpt[nowOpt]+ " ";
| |
| } catch (StringIndexOutOfBoundsException e)
| |
| {// =을 한 후 연산 결과에서 다시 연산할 때
| |
| strFm = tf.getText() + " " + strOpt[nowOpt] + " ";
| |
| strTf = "";
| |
| }
| |
| fm.setText(strFm);
| |
| }
| |
| else
| |
| {// 연산
| |
| tmpO = tf.getText();
| |
| if(countO){// 연속 연산
| |
| strCal = result + strFm.substring(strFm.length()-3,strFm.length()) + tf.getText();
| |
| result = base.Calculation(strCal);
| |
| tf.setText(result);
| |
| countP = true;
| |
| }
| |
| else {// 첫 연산
| |
| countO = true;// 연속 연산 기능 활성화
| |
| result = tf.getText();
| |
| }
| |
| strFm += tmpO + " " + strOpt[nowOpt] + " ";
| |
|
| |
| fm.setText(strFm);
| |
| strTf = "";//처음 버튼 입력을 받기 위한 텍스트 필드 초기화
| |
| }
| |
| break;
| |
|
| |
|
| |
| case 4: // C 기능 구현
| |
| strFm = "";
| |
| strTf = "";
| |
| result = "0";
| |
| countE = false;// 연속 = 기능 비활성화
| |
| countO = false;// 연속 연산 기능 비활성화
| |
| countP = false;// 연산자 변경 비활성화
| |
| fm.setText(strFm);
| |
| tf.setText("0");
| |
| break;
| |
|
| |
|
| |
| case 5:// = 기능 구현
| |
| if(countE){// 연속 = 기능
| |
| strCal = result + strE + tmpE;
| |
| result = base.Calculation(strCal);
| |
| } else {// 일반 = 기능
| |
| try{
| |
| tmpE = Integer.parseInt(tf.getText());//마지막 연산한 숫자
| |
| strE = strFm.substring(strFm.length()-3,strFm.length());//마지막 연산자
| |
|
| |
| if(strFm.length() == 4) strCal = strFm + tf.getText();// 단순 수식 인 경우
| |
| else strCal = result + strFm.substring(strFm.length()-3,strFm.length()) + tmpE;//복합 수식인경우
| |
|
| |
| result = base.Calculation(strCal);
| |
|
| |
| countE = true;// 연속 = 기능 활성화
| |
| countO = false;// 연속 연산 기능 비활성화
| |
| countP = false;// 연산자 변경 비활성화
| |
|
| |
| } catch (StringIndexOutOfBoundsException e)
| |
| {//아무런 연산하지 않고 숫자 = 했을때 예외처리
| |
| result = tf.getText();
| |
| strTf = "";
| |
| }
| |
| }
| |
| strFm = "";//수식라벨 리셋
| |
|
| |
| fm.setText(strFm);
| |
| tf.setText(result);
| |
| }
| |
|
| |
| }
| |
|
| |
|
| |
| }
| |
| public Calculator (String title){
| |
| super.setVisible(true);
| |
| super.setTitle(title);
| |
| super.setSize(218,313);
| |
| super.setResizable(false);
| |
|
| |
| //화면 중앙 배치
| |
| Dimension d = super.getToolkit().getScreenSize();
| |
| super.setLocation(d.width/2 - super.getWidth()/2, d.height/2 - super.getHeight()/2);
| |
|
| |
| //종료
| |
| this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
| |
|
| |
| this.compose();
| |
| this.setevent();
| |
| }
| |
| }
| |
| === 김정민 === | | === 김정민 === |
| //다들 열심히 하는 모습이 보기 좋네요 굳굳 | | //다들 열심히 하는 모습이 보기 좋네요 굳굳 |