자바 공부중인데 애플릿을 프레임으로 변경하려고 하거든요.
계산기 프로그램을 프레임으로 바꿔보려고 합니다.
근데 아무리 바꿔봐도 수식이 뜨는 창이 다 안 뜹니다.
다른 연산자 부분의 텍스트 필드도 안 뜨고요.
이것 저것 바꿔 봤는데 도대체 어떻게 바꿔야 할까요?
아래가 소스입니다.
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import java.lang.String;
public class Calculator extends Applet implements ActionListener {
String op; // +,-,/ 같은 연산자 저장
String temp; // 버튼을 누를때 임시 저장
boolean isConti; // 계산중인지 계산이 끝났는지 저장
double first_num; // 입력된 숫자 저장
private TextField mainText = new TextField(\"0\",30);
private TextField subText = new TextField(\"\",7); // 연산자 표시위해..
private Button num1 = new Button(\" 1 \");
private Button num2 = new Button(\"2\");
private Button num3 = new Button(\"3\");
private Button num4 = new Button(\"4\");
private Button num5 = new Button(\"5\");
private Button num6 = new Button(\"6\");
private Button num7 = new Button(\"7\");
private Button num8 = new Button(\"8\");
private Button num9 = new Button(\"9\");
private Button num0 = new Button(\"0\");
private Button num_dot = new Button(\".\");
private Button plus = new Button(\" + \");
private Button minus = new Button(\"-\");
private Button multi = new Button(\"*\");
private Button divide = new Button(\"/\");
private Button square = new Button(\"^\");
private Button sign = new Button(\" +/- \"); // 더하기,빼기가 아니라 부호임.
private Button clear = new Button(\"CE\"); // 방금 누른것만 지움
private Button clearAll = new Button(\"C\"); // 전체 자료 지움
private Button equal = new Button(\"=\");
private Label name = new Label(\" Calculator \");
public void init() {
first_num=0;
op = \"\";
temp = \"\";
isConti = true; // 디폴트로 계산중으로 설정,
mainText.setEnabled(false); // 사용자 임의로 텍스트필드의 값 수정 불가능하게함.
subText.setEnabled(false);
Panel introPanel = new Panel(); // Calculator 라는 문구가 나오는 패널.
Panel numPanel = new Panel(); // 숫자 패널
Panel arithPanel = new Panel(); // 연산자 패널
Panel otherPanel = new Panel(); // 부호,지움,결과(즉,나머지다른것들)패널
Panel masterPanel = new Panel(); // 위의 3패널을 포함하는 패널
// 보기 좋게 하기 위해 색깔 첨가
setFont(new Font(\"SansSerif\",Font.BOLD,20));
setBackground(Color.gray);
setForeground(Color.black);
introPanel.setBackground(Color.white);
introPanel.setForeground(Color.black);
mainText.setBackground(Color.white);
mainText.setForeground(Color.blue);
subText.setBackground(Color.white);
subText.setForeground(Color.black);
num1.setBackground(Color.yellow);
num1.setForeground(Color.blue);
num2.setBackground(Color.yellow);
num2.setForeground(Color.blue);
num3.setBackground(Color.yellow);
num3.setForeground(Color.blue);
num4.setBackground(Color.yellow);
num4.setForeground(Color.blue);
num5.setBackground(Color.yellow);
num5.setForeground(Color.blue);
num6.setBackground(Color.yellow);
num6.setForeground(Color.blue);
num7.setBackground(Color.yellow);
num7.setForeground(Color.blue);
num8.setBackground(Color.yellow);
num8.setForeground(Color.blue);
num9.setBackground(Color.yellow);
num9.setForeground(Color.blue);
num0.setBackground(Color.yellow);
num0.setForeground(Color.blue);
num_dot.setBackground(Color.yellow);
num_dot.setForeground(Color.blue);
plus.setBackground(Color.green);
minus.setBackground(Color.green);
multi.setBackground(Color.green);
divide.setBackground(Color.green);
square.setBackground(Color.green);
sign.setBackground(Color.orange);
clear.setBackground(Color.orange);
clearAll.setBackground(Color.orange);
equal.setBackground(Color.orange);
// 각 버튼의 이벤트 처리위해 아래와 같이함.
num1.addActionListener(this);
num2.addActionListener(this);
num3.addActionListener(this);
num4.addActionListener(this);
num5.addActionListener(this);
num6.addActionListener(this);
num7.addActionListener(this);
num8.addActionListener(this);
num9.addActionListener(this);
num0.addActionListener(this);
num_dot.addActionListener(this);
plus.addActionListener(this);
minus.addActionListener(this);
multi.addActionListener(this);
divide.addActionListener(this);
square.addActionListener(this);
sign.addActionListener(this);
clear.addActionListener(this);
clearAll.addActionListener(this);
equal.addActionListener(this);
// numPanel에 각 숫자 버튼 추가함.
numPanel.setLayout(new GridLayout(4,3,8,8));
numPanel.add(num1);
numPanel.add(num2);
numPanel.add(num3);
numPanel.add(num4);
numPanel.add(num5);
numPanel.add(num6);
numPanel.add(num7);
numPanel.add(num8);
numPanel.add(num9);
numPanel.add(num0);
numPanel.add(num_dot);
// arithPanel에 각 연산자 버튼 추가함
arithPanel.setLayout(new GridLayout(5,1,1,1));
arithPanel.add(plus);
arithPanel.add(minus);
arithPanel.add(multi);
arithPanel.add(divide);
arithPanel.add(square);
// otherPanel 에 나머지 버튼 추가함
otherPanel.setLayout(new GridLayout(4,1,8,8));
otherPanel.add(sign);
otherPanel.add(clear);
otherPanel.add(clearAll);
otherPanel.add(equal);
introPanel.add(name); // calculator 라는 문구가 나오는 패널 추가
// masterPanel 에 numPanel,arithPanel , otherPanel 추가함
masterPanel.add(numPanel);
masterPanel.add(arithPanel);
masterPanel.add(otherPanel);
add(introPanel);
add(mainText);
add(subText);
add(masterPanel);
}
public void actionPerformed(ActionEvent e) {
showStatus(\"\");
// 숫자버튼 누른것을 일단 문자열로 저장하고 나중에 계산할때 double로 변환.
if ( e.getSource() == num1 ) {
if ( isConti == false) { // = 누른 후 또 숫자입력하면 처음부터 다시 시작
MyAllClear();
}
else {
temp = temp + \"1\" ;
mainText.setText(temp);
}
}
if ( e.getSource() == num2 ) {
if ( isConti == false) {
MyAllClear();
}
else {
temp = temp + \"2\" ;
mainText.setText(temp);
}
}
if ( e.getSource() == num3 ) {
if ( isConti == false) {
MyAllClear();
}
else {
temp = temp + \"3\" ;
mainText.setText(temp);
}
}
if ( e.getSource() == num4 ) {
if ( isConti == false) {
MyAllClear();
}
else {
temp = temp + \"4\" ;
mainText.setText(temp);
}
}
if ( e.getSource() == num5 ) {
if ( isConti == false) {
MyAllClear();
}
else {
temp = temp + \"5\" ;
mainText.setText(temp);
}
}
if ( e.getSource() == num6 ) {
if ( isConti == false) {
MyAllClear();
}
else {
temp = temp + \"6\" ;
mainText.setText(temp);
}
}
if ( e.getSource() == num7 ) {
if ( isConti == false) {
MyAllClear();
}
else {
temp = temp + \"7\" ;
mainText.setText(temp);
}
}
if ( e.getSource() == num8 ) {
if ( isConti == false) {
MyAllClear();
}
else {
temp = temp + \"8\" ;
mainText.setText(temp);
}
}
if ( e.getSource() == num9 ) {
if ( isConti == false) {
MyAllClear();
}
else {
temp = temp + \"9\" ;
mainText.setText(temp);
}
}
if ( e.getSource() == num0 ) {
if ( isConti == false) {
MyAllClear();
}
else {
temp = temp + \"0\" ;
mainText.setText(temp);
}
}
if ( e.getSource() == num_dot ) {
if ( isConti == false) {
MyAllClear();
}
else {
temp = temp + \".\" ;
mainText.setText(temp);
}
}
if ( e.getSource() == plus ) {
MyProcess(); // 이 메쏘드 호출해서 숫자값을 저장하거나 계산등을 함.
op=\"+\";
temp=\"\";
subText.setText(\"연산자:\" + op);
}
if ( e.getSource() == minus ) {
MyProcess();
op=\"-\";
temp=\"\";
subText.setText(\"연산자:\" + op);
}
if ( e.getSource() == multi ) {
MyProcess();
op=\"*\";
temp=\"\";
subText.setText(\"연산자:\" + op);
}
if ( e.getSource() == divide ) {
MyProcess();
op=\"/\";
temp=\"\";
subText.setText(\"연산자:\" + op);
}
if ( e.getSource() == square ) {
MyProcess();
op=\"^\";
temp=\"\";
subText.setText(\"연산자:\" + op);
}
if ( e.getSource() == equal) {
MyProcess(); // 밑의 식과 위치가 바뀌면 안됨.
isConti = false; // 계산이 끝났기에..
mainText.setText( first_num + \"\");
subText.setText(\"\");
}
if ( e.getSource() == sign ) {
double t = Double.valueOf(temp.substring(0,temp.length())).doubleValue();
t = t * (-1) ; // 부호 바꿔주고
temp = t+\"\"; // 문자열에 대입
mainText.setText(temp);
}
if ( e.getSource() == clear ) {
op = \"\" ;
temp = \"\";
first_num = 0;
mainText.setText(temp);
}
if ( e.getSource() == clearAll ) {
MyAllClear(); // 모두 지우는 메쏘드 호출
mainText.setText(temp);
}
}
public void MyAllClear() {
op =\"\";
first_num = 0;
temp = \"\";
isConti=true; // 처음부터 다시 시작할것이면,계산이 끝난것이 아니므로
mainText.setText(temp);
subText.setText(op);
showStatus(\"다시 시작!\");
}
public void MyProcess() {
double buff = Double.valueOf(temp.substring(0,temp.length())).doubleValue();
if ( op ==\"\" ) { // 맨 처음 입력했을때
first_num = buff ;
}
if ( op == \"+\" ) {
first_num = first_num + buff ; // first_num+=buff; 도 가능함.
mainText.setText( first_num + \"\");
}
if ( op == \"-\" ) {
first_num = first_num - buff ;
mainText.setText( first_num + \"\");
}
if ( op == \"*\" ) {
first_num = first_num * buff ;
mainText.setText( first_num + \"\");
}
if ( op == \"/\" ) {
if ( buff == 0 ) {
MyAllClear();
showStatus(\"0 나누기는 불가능 C버튼 누르시오\");
}
else {
first_num = first_num / buff ;
mainText.setText( first_num + \"\");
}
}
if ( op == \"^\" ) {
first_num = MySquare(buff); // 거듭제곱 메쏘드 호출
mainText.setText( first_num + \"\");
}
if ( op == \"=\" ) {
subText.setText(\"\");
}
}
public double MySquare(double bu) {
// 거듭제곱은 따로 연산자가 없기에 만들어야함.
double te =1;
for ( int i=1 ; i<=bu ; i++) {
te = te * first_num;
}
return te;
}
}
import java.applet.Applet;
import java.awt.event.*;
import java.lang.String;
public class Calculator extends Applet implements ActionListener {
String op; // +,-,/ 같은 연산자 저장
String temp; // 버튼을 누를때 임시 저장
boolean isConti; // 계산중인지 계산이 끝났는지 저장
double first_num; // 입력된 숫자 저장
private TextField mainText = new TextField(\"0\",30);
private TextField subText = new TextField(\"\",7); // 연산자 표시위해..
private Button num1 = new Button(\" 1 \");
private Button num2 = new Button(\"2\");
private Button num3 = new Button(\"3\");
private Button num4 = new Button(\"4\");
private Button num5 = new Button(\"5\");
private Button num6 = new Button(\"6\");
private Button num7 = new Button(\"7\");
private Button num8 = new Button(\"8\");
private Button num9 = new Button(\"9\");
private Button num0 = new Button(\"0\");
private Button num_dot = new Button(\".\");
private Button plus = new Button(\" + \");
private Button minus = new Button(\"-\");
private Button multi = new Button(\"*\");
private Button divide = new Button(\"/\");
private Button square = new Button(\"^\");
private Button sign = new Button(\" +/- \"); // 더하기,빼기가 아니라 부호임.
private Button clear = new Button(\"CE\"); // 방금 누른것만 지움
private Button clearAll = new Button(\"C\"); // 전체 자료 지움
private Button equal = new Button(\"=\");
private Label name = new Label(\" Calculator \");
public void init() {
first_num=0;
op = \"\";
temp = \"\";
isConti = true; // 디폴트로 계산중으로 설정,
mainText.setEnabled(false); // 사용자 임의로 텍스트필드의 값 수정 불가능하게함.
subText.setEnabled(false);
Panel introPanel = new Panel(); // Calculator 라는 문구가 나오는 패널.
Panel numPanel = new Panel(); // 숫자 패널
Panel arithPanel = new Panel(); // 연산자 패널
Panel otherPanel = new Panel(); // 부호,지움,결과(즉,나머지다른것들)패널
Panel masterPanel = new Panel(); // 위의 3패널을 포함하는 패널
// 보기 좋게 하기 위해 색깔 첨가
setFont(new Font(\"SansSerif\",Font.BOLD,20));
setBackground(Color.gray);
setForeground(Color.black);
introPanel.setBackground(Color.white);
introPanel.setForeground(Color.black);
mainText.setBackground(Color.white);
mainText.setForeground(Color.blue);
subText.setBackground(Color.white);
subText.setForeground(Color.black);
num1.setBackground(Color.yellow);
num1.setForeground(Color.blue);
num2.setBackground(Color.yellow);
num2.setForeground(Color.blue);
num3.setBackground(Color.yellow);
num3.setForeground(Color.blue);
num4.setBackground(Color.yellow);
num4.setForeground(Color.blue);
num5.setBackground(Color.yellow);
num5.setForeground(Color.blue);
num6.setBackground(Color.yellow);
num6.setForeground(Color.blue);
num7.setBackground(Color.yellow);
num7.setForeground(Color.blue);
num8.setBackground(Color.yellow);
num8.setForeground(Color.blue);
num9.setBackground(Color.yellow);
num9.setForeground(Color.blue);
num0.setBackground(Color.yellow);
num0.setForeground(Color.blue);
num_dot.setBackground(Color.yellow);
num_dot.setForeground(Color.blue);
plus.setBackground(Color.green);
minus.setBackground(Color.green);
multi.setBackground(Color.green);
divide.setBackground(Color.green);
square.setBackground(Color.green);
sign.setBackground(Color.orange);
clear.setBackground(Color.orange);
clearAll.setBackground(Color.orange);
equal.setBackground(Color.orange);
// 각 버튼의 이벤트 처리위해 아래와 같이함.
num1.addActionListener(this);
num2.addActionListener(this);
num3.addActionListener(this);
num4.addActionListener(this);
num5.addActionListener(this);
num6.addActionListener(this);
num7.addActionListener(this);
num8.addActionListener(this);
num9.addActionListener(this);
num0.addActionListener(this);
num_dot.addActionListener(this);
plus.addActionListener(this);
minus.addActionListener(this);
multi.addActionListener(this);
divide.addActionListener(this);
square.addActionListener(this);
sign.addActionListener(this);
clear.addActionListener(this);
clearAll.addActionListener(this);
equal.addActionListener(this);
// numPanel에 각 숫자 버튼 추가함.
numPanel.setLayout(new GridLayout(4,3,8,8));
numPanel.add(num1);
numPanel.add(num2);
numPanel.add(num3);
numPanel.add(num4);
numPanel.add(num5);
numPanel.add(num6);
numPanel.add(num7);
numPanel.add(num8);
numPanel.add(num9);
numPanel.add(num0);
numPanel.add(num_dot);
// arithPanel에 각 연산자 버튼 추가함
arithPanel.setLayout(new GridLayout(5,1,1,1));
arithPanel.add(plus);
arithPanel.add(minus);
arithPanel.add(multi);
arithPanel.add(divide);
arithPanel.add(square);
// otherPanel 에 나머지 버튼 추가함
otherPanel.setLayout(new GridLayout(4,1,8,8));
otherPanel.add(sign);
otherPanel.add(clear);
otherPanel.add(clearAll);
otherPanel.add(equal);
introPanel.add(name); // calculator 라는 문구가 나오는 패널 추가
// masterPanel 에 numPanel,arithPanel , otherPanel 추가함
masterPanel.add(numPanel);
masterPanel.add(arithPanel);
masterPanel.add(otherPanel);
add(introPanel);
add(mainText);
add(subText);
add(masterPanel);
}
public void actionPerformed(ActionEvent e) {
showStatus(\"\");
// 숫자버튼 누른것을 일단 문자열로 저장하고 나중에 계산할때 double로 변환.
if ( e.getSource() == num1 ) {
if ( isConti == false) { // = 누른 후 또 숫자입력하면 처음부터 다시 시작
MyAllClear();
}
else {
temp = temp + \"1\" ;
mainText.setText(temp);
}
}
if ( e.getSource() == num2 ) {
if ( isConti == false) {
MyAllClear();
}
else {
temp = temp + \"2\" ;
mainText.setText(temp);
}
}
if ( e.getSource() == num3 ) {
if ( isConti == false) {
MyAllClear();
}
else {
temp = temp + \"3\" ;
mainText.setText(temp);
}
}
if ( e.getSource() == num4 ) {
if ( isConti == false) {
MyAllClear();
}
else {
temp = temp + \"4\" ;
mainText.setText(temp);
}
}
if ( e.getSource() == num5 ) {
if ( isConti == false) {
MyAllClear();
}
else {
temp = temp + \"5\" ;
mainText.setText(temp);
}
}
if ( e.getSource() == num6 ) {
if ( isConti == false) {
MyAllClear();
}
else {
temp = temp + \"6\" ;
mainText.setText(temp);
}
}
if ( e.getSource() == num7 ) {
if ( isConti == false) {
MyAllClear();
}
else {
temp = temp + \"7\" ;
mainText.setText(temp);
}
}
if ( e.getSource() == num8 ) {
if ( isConti == false) {
MyAllClear();
}
else {
temp = temp + \"8\" ;
mainText.setText(temp);
}
}
if ( e.getSource() == num9 ) {
if ( isConti == false) {
MyAllClear();
}
else {
temp = temp + \"9\" ;
mainText.setText(temp);
}
}
if ( e.getSource() == num0 ) {
if ( isConti == false) {
MyAllClear();
}
else {
temp = temp + \"0\" ;
mainText.setText(temp);
}
}
if ( e.getSource() == num_dot ) {
if ( isConti == false) {
MyAllClear();
}
else {
temp = temp + \".\" ;
mainText.setText(temp);
}
}
if ( e.getSource() == plus ) {
MyProcess(); // 이 메쏘드 호출해서 숫자값을 저장하거나 계산등을 함.
op=\"+\";
temp=\"\";
subText.setText(\"연산자:\" + op);
}
if ( e.getSource() == minus ) {
MyProcess();
op=\"-\";
temp=\"\";
subText.setText(\"연산자:\" + op);
}
if ( e.getSource() == multi ) {
MyProcess();
op=\"*\";
temp=\"\";
subText.setText(\"연산자:\" + op);
}
if ( e.getSource() == divide ) {
MyProcess();
op=\"/\";
temp=\"\";
subText.setText(\"연산자:\" + op);
}
if ( e.getSource() == square ) {
MyProcess();
op=\"^\";
temp=\"\";
subText.setText(\"연산자:\" + op);
}
if ( e.getSource() == equal) {
MyProcess(); // 밑의 식과 위치가 바뀌면 안됨.
isConti = false; // 계산이 끝났기에..
mainText.setText( first_num + \"\");
subText.setText(\"\");
}
if ( e.getSource() == sign ) {
double t = Double.valueOf(temp.substring(0,temp.length())).doubleValue();
t = t * (-1) ; // 부호 바꿔주고
temp = t+\"\"; // 문자열에 대입
mainText.setText(temp);
}
if ( e.getSource() == clear ) {
op = \"\" ;
temp = \"\";
first_num = 0;
mainText.setText(temp);
}
if ( e.getSource() == clearAll ) {
MyAllClear(); // 모두 지우는 메쏘드 호출
mainText.setText(temp);
}
}
public void MyAllClear() {
op =\"\";
first_num = 0;
temp = \"\";
isConti=true; // 처음부터 다시 시작할것이면,계산이 끝난것이 아니므로
mainText.setText(temp);
subText.setText(op);
showStatus(\"다시 시작!\");
}
public void MyProcess() {
double buff = Double.valueOf(temp.substring(0,temp.length())).doubleValue();
if ( op ==\"\" ) { // 맨 처음 입력했을때
first_num = buff ;
}
if ( op == \"+\" ) {
first_num = first_num + buff ; // first_num+=buff; 도 가능함.
mainText.setText( first_num + \"\");
}
if ( op == \"-\" ) {
first_num = first_num - buff ;
mainText.setText( first_num + \"\");
}
if ( op == \"*\" ) {
first_num = first_num * buff ;
mainText.setText( first_num + \"\");
}
if ( op == \"/\" ) {
if ( buff == 0 ) {
MyAllClear();
showStatus(\"0 나누기는 불가능 C버튼 누르시오\");
}
else {
first_num = first_num / buff ;
mainText.setText( first_num + \"\");
}
}
if ( op == \"^\" ) {
first_num = MySquare(buff); // 거듭제곱 메쏘드 호출
mainText.setText( first_num + \"\");
}
if ( op == \"=\" ) {
subText.setText(\"\");
}
}
public double MySquare(double bu) {
// 거듭제곱은 따로 연산자가 없기에 만들어야함.
double te =1;
for ( int i=1 ; i<=bu ; i++) {
te = te * first_num;
}
return te;
}
}
댓글 0