728x90
반응형
계산기 프로그램 만들기
프로그램 GUI
전체 코드
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
public class Cal extends JFrame implements ActionListener{
JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b10;
JButton b11, b12, b13, b14, b15, b16, b17;
JTextField tf;
Vector<String> v;
Cal(){
super("계산기");
v=new Vector<String>(5);
v.add("0");v.add("0");v.add("0");v.add("0");v.add("T");
tf=new JTextField("0", 20);
tf.setHorizontalAlignment(JTextField.RIGHT);
tf.setSelectionStart(1);
b1=new JButton("1"); b1.addActionListener(this);
b2=new JButton("2"); b2.addActionListener(this);
b3=new JButton("3"); b3.addActionListener(this);
b4=new JButton("4"); b4.addActionListener(this);
b5=new JButton("5"); b5.addActionListener(this);
b6=new JButton("6"); b6.addActionListener(this);
b7=new JButton("7"); b7.addActionListener(this);
b8=new JButton("8"); b8.addActionListener(this);
b9=new JButton("9"); b9.addActionListener(this);
b10=new JButton("0"); b10.addActionListener(this);
b11=new JButton("C"); b11.addActionListener(new C());
b12=new JButton("."); b12.addActionListener(this);
b13=new JButton("+"); b13.addActionListener(new OP());
b14=new JButton("-"); b14.addActionListener(new OP());
b15=new JButton("*"); b15.addActionListener(new OP());
b16=new JButton("/"); b16.addActionListener(new OP());
b17=new JButton("="); b17.addActionListener(new Re());
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(4, 4));
p1.add(b1); p1.add(b2); p1.add(b3); p1.add(b4); p1.add(b5);
p1.add(b6); p1.add(b7); p1.add(b8); p1.add(b9);
p1.add(b10); p1.add(b11); p1.add(b12); p1.add(b13); p1.add(b14);
p1.add(b15); p1.add(b16); p1.add(b17);
this.add("North", tf);
this.add("Center", p1);
this.add("South", b17);
this.setDefaultCloseOperation(3);
this.setSize(300, 300);
this.setVisible(true);
}
public static void main(String[] args) {
new Cal();
}
@Override
public void actionPerformed(ActionEvent arg0) {
JButton jj=(JButton)arg0.getSource();
String st=jj.getText(); //1234567
String flag=v.get(4); //T F
String old=tf.getText();
if(flag.equals("T")) {
if(!(old.equals("0")&st.equals("0"))) {
if(st.equals(".")) {
tf.setText("0.");
v.set(4,"F");
} else {
tf.setText(st);
v.set(4,"F");
}
}
}else {//F
if(!(st.equals(".")&old.contains("."))){
old=old+st;
tf.setText(old);
}
}
} //action
class C implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
tf.setText("0");
v.set(0,"0");v.set(1,"0");v.set(2,"0");v.set(3,"0");
v.set(4,"T");
System.out.println(v);
}
}//C class
class OP implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
JButton jj=(JButton)arg0.getSource();
String st=jj.getText(); //+-*
String n1=tf.getText(); //12
v.set(0, n1); v.set(1, st); v.set(4, "T");
System.out.println("연산 : "+v);
}
}//OP
class Re implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
String n2=tf.getText(); //3
v.set(2, n2);
v.set(4, "T"); //[12,+,3,0,T])
String n1=v.get(0); //12
double num1=Double.parseDouble(n1); //12.0
double num2=Double.parseDouble(n2); //3.0
double sum=0.0;
String op=v.get(1); //*-+/
if(op.equals("+")) sum=num1+num2;
else if(op.equals("-")) sum=num1-num2;
else if(op.equals("*")) sum=num1*num2;
else if(op.equals("/")){ //나눗셈
System.out.println(num2);
if(num2==0.0) {
tf.setText("0으로 나눌 수 없음");
return;
}
else sum=num1/num2;
}
String sum2=""+sum;
String r[]=sum2.split("\\."); //[12,0]
if(r[1].equals("0"))
tf.setText(r[0]);
else
tf.setText(sum2);
v.set(3, sum2);
System.out.println("최종"+v);
}//action
}//Re
}//Cal class
728x90
반응형