Dev_Study/Java_backup

[JAVA / SWING] 성적관리 프로그램 v0.1

LeeDaniel 2016. 10. 15. 10:14
package com.std.cal;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;

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

public class ScoreCal extends JFrame implements ActionListener{
	
  JPanel jp1 = null;
	
  JLabel lb1 = null;
  JLabel lb2 = null;
  JLabel lb3 = null;
  JLabel lb4 = null;
  JLabel lb5 = null;
  JLabel lb6 = null;
  JLabel lb7 = null;
	 
  JTextField[] tf = null;	   
	 
  JLabel line1 = null;
  JLabel line2 = null;
	 
  JButton bt1 = null;
  JButton bt2 = null;
  JButton bt3 = null;
	 
  public ScoreCal(){
		
//JFrame셋팅
  setLayout(null);
  setTitle("성적관리 프로그램 v0.1");
  setSize(260, 260);
  setResizable(false);
  setLocationRelativeTo(null);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    
//라벨생성
  String[] labelTitle = {"성적관리"
                       , "국어 : ", "영어 : "
                       , "수학 : ", "과학 : "
                       , "사회 : ", "음악 : "
                       , "성적출력"
                       , "총점 : ", "평균 : "};
	    
  JLabel[] label = new JLabel[10];

    for(int i=0 ; i<labelTitle.length ; i++){
      label[i] = new JLabel(labelTitle[i]);
    }
	    
//텍스트필드 생성
  tf = new JTextField[8];
	    
  for(int i=0 ; i<tf.length ; i++){
    tf[i] = new JTextField();
    tf[i].setHorizontalAlignment(JTextField.RIGHT); //텍스트필드값을 오른쪽정렬
    tf[i].setText("0");
    }
	    
//jp1패널 셋팅
  jp1 = new JPanel();
  jp1.setLayout(null);
  jp1.setSize(260, 300);
  jp1.setLocation(0, 0);
  jp1.setBackground(new Color(051, 204, 102));
	     
//라벨 좌표, 크기값
  int lb_x = 5;
  int lb_y = 5;
  int lb_width = 55;
  int lb_height = 15;
	     
//텍스트필드 좌표, 크기값
  int tf_x = 45;
  int tf_y = 38;
  int tf_width = 70;
  int tf_height = 20;
	     
//성적관리 라벨
  label[0].setBounds(lb_x, lb_y, lb_width, lb_height);
  label[0].setOpaque(true);
  label[0].setFont(new Font("돋움체", Font.BOLD, 12));
  label[0].setBackground(new Color(000, 153, 051));
  label[0].setForeground(Color.WHITE);
	     
//과목 라벨 배열로 생성
  int label_One = 1;
  int label_Two = 1;
  for(int i=1 ; i<7 ; i++){
	    	 
    if((i%2) != 0){
      label[i].setBounds(lb_x, lb_y + (34*label_One), lb_width, lb_height);
      label_One++;
     }else{
      label[i].setBounds(lb_x + 120, lb_y + (34*label_Two), lb_width, lb_height);
      label_Two++;
    }
  }
//과목 라벨 생성
//label[1].setBounds(lb_x, lb_y + 35, lb_width, lb_height);
//label[3].setBounds(lb_x, lb_y + 70, lb_width, lb_height);
//label[5].setBounds(lb_x, lb_y + 105, lb_width, lb_height);
//	     
//label[2].setBounds(lb_x + 120, lb_y + 35, lb_width, lb_height);
//label[4].setBounds(lb_x + 120, lb_y + 70, lb_width, lb_height);
//label[6].setBounds(lb_x + 120, lb_y + 105, lb_width, lb_height);
	     
//jp1패널에 라벨 추가
  jp1.add(label[0]);
  jp1.add(label[1]);
  jp1.add(label[3]);
  jp1.add(label[5]);
  jp1.add(label[2]);
  jp1.add(label[4]);
  jp1.add(label[6]);
	     
//과목 텍스트필드 배열로 생성
  int Text_One = 0;
  int Text_Two = 0;
	     
  for(int i=0 ; i<6 ; i++){
    if((i%2) != 0){
      tf[i].setBounds(tf_x + 120, tf_y + (30 *Text_One), tf_width, tf_height);
      tf[i].setOpaque(true);
      tf[i].setBackground(new Color(204, 255, 153));
      Text_One++;
    }else{
      tf[i].setBounds(tf_x, tf_y + (30 *Text_Two), tf_width, tf_height);
      tf[i].setOpaque(true);
      tf[i].setBackground(new Color(204, 255, 153));
      Text_Two++;
    }
  }
//과목 텍스트필드 생성
//tf[1].setBounds(tf_x, tf_y, tf_width, tf_height);
//tf[1].setOpaque(true);
//tf[1].setBackground(new Color(204, 255, 153));
//	     
//tf[3].setBounds(tf_x, tf_y + 34, tf_width, tf_height);
//tf[3].setOpaque(true);
//tf[3].setBackground(new Color(204, 255, 153));
//	     
//tf[5].setBounds(tf_x, tf_y + 68, tf_width, tf_height);
//tf[5].setOpaque(true);
//tf[5].setBackground(new Color(204, 255, 153));
//	     
//tf[0].setBounds(tf_x + 120, tf_y, tf_width, tf_height);
//tf[0].setOpaque(true);
//tf[0].setBackground(new Color(204, 255, 153));
//	     
//tf[2].setBounds(tf_x + 120, tf_y + 34, tf_width, tf_height);
//tf[2].setOpaque(true);
//tf[2].setBackground(new Color(204, 255, 153));
//	     
//tf[4].setBounds(tf_x + 120, tf_y + 68, tf_width, tf_height);
//tf[4].setOpaque(true);
//tf[4].setBackground(new Color(204, 255, 153));
	     
//jp1패널에 텍스트필드 추가
  jp1.add(tf[1]);
  jp1.add(tf[3]);
  jp1.add(tf[5]);
  jp1.add(tf[0]);
  jp1.add(tf[2]);
  jp1.add(tf[4]);
	     
//점선1
  line1 = new JLabel("----------------------------------------------------------");
  line1.setBounds(lb_x, lb_y, 300, lb_y+250);
  jp1.add(line1);
	    
//성적출력 라벨
  label[7].setBounds(lb_x, lb_y + 140, lb_width, lb_height);
  label[7].setOpaque(true);
  label[7].setFont(new Font("돋움체", Font.BOLD, 12));
  label[7].setBackground(new Color(000, 153, 051));
  label[7].setForeground(Color.WHITE);
	    
//총점 라벨, 텍스트필드
  label[8].setBounds(lb_x, lb_y + 163, lb_width, lb_height);
  tf[6].setBounds(tf_x, lb_y + 160, tf_width, tf_height);
  tf[6].setOpaque(true);
  tf[6].setBackground(new Color(204, 255, 153));
	    
//평균 라벨, 텍스트필드
  label[9].setBounds(lb_x + 120, lb_y + 163, lb_width, lb_height);
  tf[7].setBounds(tf_x + 120, lb_y + 160, tf_width, tf_height);
  tf[7].setOpaque(true);
  tf[7].setBackground(new Color(204, 255, 153));
	    
//jp1패널에 총점,평균 라벨, 텍스트필드 추가
  jp1.add(label[7]);
  jp1.add(label[8]);
  jp1.add(label[9]);
  jp1.add(tf[6]);
  jp1.add(tf[7]);
	    
//점선2
  line2 = new JLabel("----------------------------------------------------------");
  line2.setBounds(lb_x, lb_y, 300, lb_y+370);
  jp1.add(line2);
	      
//버튼생성 - 총점, 평균, 리셋
  bt1 = new JButton("총점");
  bt1.addActionListener(this);
  bt1.setBackground(new Color(153, 255, 204));
	      
  bt2 = new JButton("평균");
  bt2.addActionListener(this);
  bt2.setBackground(new Color(153, 255, 204));
	      
  bt3 = new JButton("리셋");
  bt3.addActionListener(this);
  bt3.setBackground(new Color(153, 255, 204));
	      
  bt1.setBounds(lb_x - 20 + 20, lb_y + 200, 60, 20);
  bt2.setBounds(lb_x + 85, lb_y + 200, 60, 20);
  bt3.setBounds(lb_x + 170, lb_y + 200, 60, 20);
	      
//jp1패널에 버튼 추가
  jp1.add(bt1);
  jp1.add(bt2);
  jp1.add(bt3);
	      
//JFrame에 jp1패널 추가
  add(jp1);
	    
  setVisible(true);
	    
  }//end of ScoreCal constructor
	
//합계 연산
  public BigDecimal bSum(){

    BigDecimal bKor = new BigDecimal(tf[0].getText());
    BigDecimal bEng = new BigDecimal(tf[1].getText());
    BigDecimal bMath = new BigDecimal(tf[2].getText());
    BigDecimal bScience = new BigDecimal(tf[3].getText());
    BigDecimal bSociety = new BigDecimal(tf[4].getText());
    BigDecimal bmusic = new BigDecimal(tf[5].getText());
    BigDecimal bTo = bKor.add(bEng).add(bMath).add(bScience).add(bSociety).add(bmusic);
    BigDecimal bTotal = bTo.setScale(2, BigDecimal.ROUND_HALF_UP); //소수2번째 자리까지 반올림해서
	      
    return bTotal;
  }//end of bSum method
	
//평균 연산
  public BigDecimal bDivision(){
    BigDecimal bDiv = bSum().divide(new BigDecimal("6"), 2,BigDecimal.ROUND_UP); //소수2번째 자리까지 반올림해서
	  
    return bDiv;
  }//end of bDivision method

  @Override
  public void actionPerformed(ActionEvent e) {
		
//총점 눌렀을때
    if(e.getSource() == bt1){
      tf[6].setText("");
      tf[6].setText(bSum().toString());
    }
	      
//평균 눌렀을때
    if(e.getSource() == bt2){
      tf[7].setText("");
      tf[7].setText(bDivision().toString());
    }
	      
//리셋 눌렀을때
    if(e.getSource() == bt3){
      for(int i=0 ; i<tf.length ; i++){
        tf[i].setText("");
    }
//	         tf[0].setText("");
//	         tf[1].setText("");
//	         tf[2].setText("");
//	         tf[3].setText("");
//	         tf[4].setText("");
//	         tf[5].setText("");
//	         tf[6].setText("");
//	         tf[7].setText("");
    }
  }//end of actionPerformed method

  public static void main(String[] args) {
    ScoreCal sc = new ScoreCal();
  }//end of main method
	
}//end of ScoreCal class


반응형