관리 메뉴

IT창고

자바(JAVA)실습 - GUI 계산기 본문

프로그래밍/JAVA

자바(JAVA)실습 - GUI 계산기

방구석여포 2017. 9. 12. 00:52


조건

1. GUI 계산기 소스만들때 클래스를 사용

2. Label에 숫자를 누르면 치환되는 부분을 숫자가 이어서 나오도록 수정

3. =을 누르면 계산값 보이기


소스코드

main

public class Main {

public static void main(String[] args) {

new Control();

}

}

------------------------------------------------

Control

public class Control {

CalculFrame calFrame = new CalculFrame();

public Control(){

BaseFrame myFrame = new BaseFrame(300, 500, 200, 200);

myFrame.setCenter();

myFrame.setVisible(true);

calFrame.setVisible(true);

}

}

------------------------------------------------

CalFrame

import java.awt.Button;

import java.awt.Color;

import java.awt.Frame;

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import java.awt.Label;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;


public class CalculFrame extends Frame {

int curValue = 0;

String rule = "";


public CalculFrame() {

setSize(200, 300);

setLocation(200, 200);

setResizable(false);


init();


addWindowListener(new WindowAdapter() {

@Override

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

}


private void init() {

// 레이아웃 만들기

setLayout(new GridBagLayout());


GridBagConstraints gbc = new GridBagConstraints();

gbc.gridx = 1;

gbc.gridy = 1;

gbc.gridwidth = 1;

gbc.gridheight = 1;

gbc.weightx = 1.0;

gbc.weighty = 1.0;

gbc.fill = GridBagConstraints.BOTH;


Label show = new Label("0");


ActionListener act = new ActionListener() {


@Override

public void actionPerformed(ActionEvent e) {

String str = ((Button) e.getSource()).getLabel();


String curStr = show.getText();


if (curStr.equals("0")) {

show.setText(str);

} else {

show.setText(curStr + str);

}

}

};


Button btn1 = new Button("1");

Button btn2 = new Button("2");

Button btn3 = new Button("3");

Button btn4 = new Button("4");

Button btn5 = new Button("5");

Button btn6 = new Button("6");

Button btn7 = new Button("7");

Button btn8 = new Button("8");

Button btn9 = new Button("9");

Button btn0 = new Button("0");

Button btnEqual = new Button("=");

Button btnClear = new Button("C");

Button btnPlus = new Button("+");

Button btnMinus = new Button("-");


btnEqual.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

if(rule.equals("+") && curValue != 0){

String curStr = show.getText();

int tempNewNumber = Integer.parseInt(curStr);

tempNewNumber = curValue + tempNewNumber;

show.setText(tempNewNumber+"");

curValue = 0;

rule ="";

}else if(rule.equals("-") && curValue != 0 ){

String curStr = show.getText();

int tempNewNumber = Integer.parseInt(curStr);

tempNewNumber = curValue - tempNewNumber;

show.setText(tempNewNumber+"");

curValue = 0;

rule ="";

}

}

});

btnClear.addActionListener(new ActionListener() {


@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

show.setText("0");

curValue = 0;

rule = "";

}

});


btnPlus.addActionListener(new ActionListener() {


@Override

public void actionPerformed(ActionEvent e) {

String curStr = show.getText();

if (!curStr.equals("0")) {

rule = "+";

curValue = Integer.parseInt(curStr);

show.setText("0");

}


}

});


btnMinus.addActionListener(new ActionListener() {


@Override

public void actionPerformed(ActionEvent e) {

String curStr = show.getText();

if (!curStr.equals("0")) {

rule = "-";

curValue = Integer.parseInt(curStr);

show.setText("0");

}

}

});


gbc.gridx = 0;

gbc.gridy = 1;

add(btn7, gbc);


gbc.gridx = 1;

gbc.gridy = 1;

add(btn8, gbc);


gbc.gridx = 2;

gbc.gridy = 1;

add(btn9, gbc);


gbc.gridx = 0;

gbc.gridy = 2;

add(btn4, gbc);


gbc.gridx = 1;

gbc.gridy = 2;

add(btn5, gbc);


gbc.gridx = 2;

gbc.gridy = 2;

add(btn6, gbc);


gbc.gridx = 0;

gbc.gridy = 3;

add(btn1, gbc);


gbc.gridx = 1;

gbc.gridy = 3;

add(btn2, gbc);


gbc.gridx = 2;

gbc.gridy = 3;

add(btn3, gbc);


gbc.gridx = 0;

gbc.gridy = 4;

add(btn0, gbc);


gbc.gridx = 1;

gbc.gridy = 4;

gbc.gridwidth = 1;

add(btnEqual, gbc);


gbc.gridx = 2;

gbc.gridy = 4;

gbc.gridwidth = 1;

add(btnClear, gbc);


gbc.gridx = 3;

gbc.gridy = 1;

gbc.gridwidth = 2;

gbc.gridheight = 2;

add(btnPlus, gbc);


gbc.gridx = 3;

gbc.gridy = 3;

gbc.gridwidth = 2;

gbc.gridheight = 2;

add(btnMinus, gbc);


show.setAlignment(Label.RIGHT);

show.setBackground(Color.YELLOW);

gbc.gridx = 0;

gbc.gridy = 0;

gbc.gridwidth = 5;

gbc.gridheight = 1;

add(show, gbc);


btn1.addActionListener(act);

btn2.addActionListener(act);

btn3.addActionListener(act);

btn4.addActionListener(act);

btn5.addActionListener(act);

btn6.addActionListener(act);

btn7.addActionListener(act);

btn8.addActionListener(act);

btn9.addActionListener(act);

btn0.addActionListener(act);

}

}

결과화면

클래스에 레이아웃을 설정해서 소스길이를 줄이고 addActionListener으로 동작을 설정해서 실제 계산이 되도록 연동시켰습니다. 초기화버튼을 추가해서 리셋을 시키고 합이나 뺄셈으로 얻은 값을 그대로 합이나 뺄셈하여 연속으로 계산할수있게 만들었습니다.


Tip 

Frame을 사용할때마다 항상 선언하고 소스를 입력해야하는데 기본Frame클래스를 만들어 필요할때마다 가져오는 클래스를 따로 소스코딩

import java.awt.Button;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Frame;

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import java.awt.Label;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;


public class BaseFrame extends Frame {

int width;

int height;


public BaseFrame(int width, int height,int locX, int locY) {

this.width = width;

this.height = height;

setSize(width, height);

setLocation(locX, locY);

setResizable(false);

addWindowListener(new WindowAdapter() {

@Override

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

}

public void setCenter(){

Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();


int width = (int) scr.getWidth();

int height = (int) scr.getHeight();

setLocation((width / 2) - (width / 2), (height / 2) - (height / 2));


}


}


Comments