글쓴이 :이승배 1998년 2월 02일 18:49:59
간단한 계산기를 만드는데 너무 힘이 드네요.
일단 맨 아래 질문부터 읽어주시고 답변을 주시면 감사하겠습니다.
//계산기 프로그램.
import java.awt.*;
public class Calgui extends Frame
{
Label digitLabel;
String save_value = "";
int second;
public Calgui()
{
resize(200, 250);
//메뉴바 만들고 아이템 만들기.
MenuBar FMenuBar = new MenuBar();
setMenuBar(FMenuBar);
Menu FileMenu = new Menu("File");
FMenuBar.add(FileMenu);
MenuItem ExitItem = new MenuItem("Exit");
FileMenu.add(ExitItem);
//숫자가 나올 레이블 만들기.
BorderLayout digitLayout = new BorderLayout();
setLayout(digitLayout);
digitLabel = new Label("0.", Label.RIGHT);
add("North", digitLabel);
//판넬을 만들고 버튼 구성.
Panel digitPanel = new Panel();
digitPanel.setLayout(new GridLayout(0, 4, 5, 5));
digitPanel.add(new Button("7"));
digitPanel.add(new Button("8"));
digitPanel.add(new Button("9"));
digitPanel.add(new Button("/"));
digitPanel.add(new Button("4"));
digitPanel.add(new Button("5"));
digitPanel.add(new Button("6"));
digitPanel.add(new Button("*"));
digitPanel.add(new Button("1"));
digitPanel.add(new Button("2"));
digitPanel.add(new Button("3"));
digitPanel.add(new Button("-"));
digitPanel.add(new Button("0"));
digitPanel.add(new Button("+/-"));
digitPanel.add(new Button("."));
digitPanel.add(new Button("+"));
digitPanel.add(new Button("="));
digitPanel.add(new Button("C"));
digitPanel.add(new Button("CE"));
add(digitPanel);
show();
}
public boolean handleEvent (Event e)
{
//버튼이 눌렸을때.
if(e.id == Event.ACTION_EVENT && e.target instanceof Button)
{
String Buttonlabel = ((Button)(e.target)).getLabel();
digit_to_label(Buttonlabel);
}
//메뉴아이템이 눌렸을때.
if (e.id == Event.ACTION_EVENT && e.target instanceof MenuItem)
if (((MenuItem)e.target).getLabel().equals("Exit"))
System.exit(0);
if (e.id == Event.WINDOW_DESTROY)
System.exit(0);
return false;
}
//버튼이 눌렸을때 처리.
void digit_to_label(String Buttonlabel)
{
int first_value = 0;
int int_value = 0;
String symbol_value = "";
try{ int_value = Integer.parseInt(Buttonlabel); }
//숫자로 변환을 시키지 못할 경우 예외상황 발생.
catch ( NumberFormatException e )
{
if (Buttonlabel == "+")
symbol_value = "+";
if (Buttonlabel == "=")
if (symbol_value == "+")
digitLabel.setText(String.valueOf(first_value + second).toString());
}
//들어온 값이 숫자일때.
if (int_value >= 0 && int_value <= 9) //이게 왜 실행되는지..
{
save_value = save_value + Buttonlabel;
digitLabel.setText(save_value);
first_value = int_value;
}
}
public static void main(String args[])
{
new Calgui();
}
}
이렇게 프로그램을 짰는데요.