여기 제가 대학에서 쓴 계산기가 있습니다. 조금 수정할 수 있습니다.
자바 . awt . *;
Import Java . awt . 데이터 전송 . clipboard;
자바 . awt . 데이터 전송 . *;
Import javax . swing . *;
자바 . awt . event . *;
Import Java . text . *;
공개 클래스 M06001526 {
공개 static void main(String[] args) {
New Calculator();
}
}
Class Calculator는 액션 리스너를 구현하기 위해 JFrame을 확장
{<
/**
*
*/
private static final long serialVersionUID = 1L;
JTextArea show = new JTextArea(1, 20);
JButton bt1, bt2, bt3, bt4, bt5, bt6, bt7, bt8, bt9, bt10, bt11, btAdd, btSub, btMult, btDiv, btop, btClear, back
JButton btequ
JMenuItem copyMenu, pasteMenu, exitFile, s, t, about, me;
더블 프레넘 = 0;
더블 노넘 = 0;
숫자 형식 nf = 숫자 형식 . getinstance();
boolean clear = false
boolean mark = false;
int dianornot = 0;
char operation;
clipboard clip = toolkit . getsystem clipboard();
계산기()
{
set layout(new flow layout());
setResizable(false);
{
설정 레이아웃(새로운 흐름 레이아웃());
setLocation(450, 300);
setSize(250, 250);
add(new JLabel(" "));
show . set editable(false);
add(show);
JMenuBar main menu = new JMenuBar();//make menu
setJMenuBar(main menu);
JMenu 편집 메뉴 = new JMenu . (" edit ");
JMenu 보기 메뉴 = new JMenu(" view ");
JMenu 도움말 메뉴 = new JMenu(" help ");
메인 메뉴 . 추가(편집 메뉴);
mainMenu.add(보기 메뉴);
메인 메뉴 . add(help menu);
copy menu = new JMenuItem(" copy");
paste menu = new JMenuItem(" paste");
exit file = new JMenuItem(" exit");
메뉴 복사 . addactionlistener(this);
paste menu . addactionlistener(this);
파일 종료 . addactionlistener(this);
편집 메뉴 . add(복사 메뉴);
편집 메뉴 . add(붙여넣기 메뉴);
편집 메뉴 . add(exit file);
t = new JMenuItem("●standard");
S = new JMenuItem("scientific type");
보기 메뉴 . add(t);
viewMenu.add
t . addactionlistener(this);
s . addactionlistener(this);
about = new JMenuItem(" about ");
me = new JMenuItem(" author ");
helpMenu.add(about);
helpMenu.add(me);
about . addactionlistener(this);
me . addactionlistener(this);
/직사각형 rect=new Rectangle().
Bt 1 = new JButton(" 1 ");
bt2 = new JButton(" 2 ");
bt3 = new JButton(" 3 ");
bt4 = new JButton(" 4 ");
bt5 = new JButton(. " 5 ");
bt6 = new JButton(" 6 ");
bt7 = new JButton(" 7 ");
bt8 = new JButton(" 8 ");
bt9 = new JButton(" 9 ");
Bt 10 = new JButton(" 0 ");
bt11 = new JButton(" . ");
Bt 11 . setsize(18, 6);
bt11.setBounds(0, 0, 8, 6);
btAdd = new JButton("+");
Bt sub = new JButton("-");
Bt mult = new JButton("×");
Bt mult. ");
Bt div = new JButton("/");
Bt clear = new JButton("reset");
Back = new JButton(" Back ");
btop = new JButton("+/-"); p>
btequ = new JButton(" = ");
Add (Bt 1);
Add (bt2);
Add (bt3);
Add (bt4);
Add (bt5);
Add (bt6);
Add (bt7 );
add(bt8);
add(bt9);
add(Bt 10);
add(Bt 11);
add(btAdd);
add(Bt sub);
add(Bt mult);
add( Bt div);
add(btop);
add(back);
add(Bt clear);
add(btequ);
Bt 1 . addactionlistener(this);
bt2 . addactionlistener(this);
bt3 . addactionlistener(this);
bt4 . addactionlistener(this);
bt5 . addactionlistener(this);
bt6 . addactionlistener(this);
bt7 . addactionlistener(this);
bt8 . addactionlistener(this);
bt9 . addactionlistener(this);
Bt 10 . addactionlistener(this);
Bt 11 . addactionlistener(this);
Btadd . addactionlistener(this);
btsub . addactionlistener(this);
Bt mult . addactionlistener(this);
Bt div . addactionlistener(this);
Bt clear . addactionlistener(this);
Bt back . addactionlistener(this);
btop . addactionlistener(this);
btequ . addactionlistener(this);
set visible(true);
setTitle("계산기");
this . setdefaultcloseoperation(JFrame. DISPOSE _ ON _ CLOSE);
}
공개 유효하지 않은 연산이 실행됨(연산 이벤트 e)
{
if(clear==true)
{
show . SETTEXT(NULL);
}
else if(e . getsource()== Bt 1)
{
if((연산 = = = ' = ' | 연산 = = '+' | 연산 = = '-' | 연산 = = ' * ' | 연산 = = '/') & amp; & ampmark==. true)
show . SETEXT(NULL);
show . append(" 1 ");
p>
mark = false
}
else if (e.getSource()==bt2)
{ if ((operation = = ' = ' | | operation = = '+' | | operation = = '-' | |. operation = = ' * ' | | operation = = '/')& amp; & ampmark==true)
show . SETEXT(NULL);
show . append(" 2 ");
p>
mark = false
}
else if (e.getSource()==bt3)
{ if ((operation = = ' = ' | | operation = = '+' | | operation = = '-' | |. operation = = ' * ' | | operation = = '/')& amp; & ampmark==true)
show . SETEXT(NULL);
show . append(" 3 ");
p>
mark = false}
else if (e.getSource() == bt4)
{ if ((operation = = ' = ' | | operation = = '+' | | operation = = '-' | |. operation = = ' * ' | | operation = = '/')& amp; & ampmark==true)
show . SETEXT(NULL);
show . append(" 4 ");
p>
mark = false}
else if (e.getSource() == bt5)
{ if ((operation = = ' = ' | | operation = = '+' | | operation = = '-' | | . operation = = ' * ' | | operation = = '/')& amp; & ampmark==true)
show . SETEXT(NULL);
show . append(" 5 ");
p>
mark = false}
else if (e.getSource() == bt6)
{ if ((operation = = ' = ' | | operation = = '+' | | operation = = '-' | |. operation = = ' * ' | | operation = = '/')& amp; & ampmark==true)
show . SETEXT(NULL);
show . append(" 6 ");
mark = false}
else if (e.getSource() == bt7)
{ if ((operation = = = ' | | operation = = '+' | | operation = = '-' | |. operation = = ' * ' | | operation = = '/')& amp; & ampmark==true)
show . SETEXT(NULL);
show . append(" 7 ");
p>
mark = false}
else if (e.getSource() == bt8)
{ if ((operation = = ' = ' | | operation = = '+' | | operation = = '-' | |. operation = = ' * ' | | operation = = '/')& amp; & ampmark==true)
show . SETEXT(NULL);
show . append(" 8 ");
p>
mark = false}
else if (e.getSource() == bt9)
{ if ((operation = = ' = ' | | operation = = '+' | | operation = = '-' | | . operation = = ' * ' | | operation = = '/')& amp; & ampmark==true)
show . SETEXT(NULL);
show . append(" 9 ");
mark = false}
else if(e . getsource() == Bt 10)
{
if((operation = = = ' | | operation = = '+' | | operation = = '-' | | operation = = ' * ' | | operation = = '/') & amp; & ampmark=. = true)
show . SETEXT(NULL);
show . append(" 0 ");
mark = false}
else if(e . getsource() == Bt 11)
{
string s 1 = show . gettext();
for(int I = 0; I& lts 1 . LENGTH(); I++)
{
char a 1 = s 1 . charat(I);
if (a1== ' . ')
dianornot = 1;
}
if (show.getCaretPosition()! = 0 & amp& ampdianornot==0)
show.append(" . ");
dianornot = 0;
}
else if (e.getSource()==btAdd)
{
prenum = double . parse double(show . gettext());
mark = true
operation = '+';
}
else if (e.getSource()==btSub)
{
prenum = double . parse double(show . gettext());
mark = true
operation = '-';
}
else if (e.getSource()==btMult)
{ prenum = double . parse double(show . gettext());
mark = true
operation = ' * ';}
else if (e.getSource()==btDiv)
{
prenum = double . parse double(show . gettext());
mark = true
operation = '/';}
else if (e.getSource()==btClear)
{ show . SETTEXT(NULL); }
else if (e . getsource() == back & amp; & amp(operation! ='='||mark==false))
{//show . append(" 2 ");
string s = show . gettext();
//show.insert(", s . LENGTH());
//show . SETTEXT(NULL);
for(int I = 0; I& lts . LENGTH()-1; I++)
{
char a = s . charat(I);
show . SETTEXT(SHOW . GETTEXT()+A);
}
//show . setcaretposition(0);
}
//else if (e.getSource()==)
else if (e.getSource()==btop)
{
string S2 = show . gettext();
if (s2.charAt(0)=='-')
{
show . SETTEXT(NULL);
for(int I = 1; I& ltS2 . LENGTH(); i++)
{
char a2 = S2 . charat(I);
show . SETTEXT(SHOW . GETTEXT()+A2);
}
//show . SETTEXT(S2);
}
else {show.insert("-", 0);
///show . setcaretposition(2);
///show . setcontent(S2);
}
}
else if(e.getSource()==btequ)
{string result;
nownum = double . parse double(show . gettext());
Switch (operation){
Case "+":
int num = max(getFloatNum(nownum), getFloatNum(prenum));
/. /String temp 2 = double . tostring(nownum);
nf . setminimumcfractiondigits(num);
double re = prenum+nownum;
nf . format(re);
result = double . tostring(re);
show . setText(null);
show.setText(result);
//show . setext(null);
operation = ' = ';
mark = true
break;
case '-':
num=max(getFloatNum(nownum), getFloatNum(prenum))
///String temp 2 = double . tostring(nownum);
nf . setminimumcfractiondigits(num);
re = prenum-nownum;
nf . format(re);
result = double . tostring(re);
show . setText(null);
show.setText(result);
//show . setext(null);
operation = ' = ';
mark = true
breaking;
case ' * ':
num=max(getFloatNum(nownum), getFloatNum(prenum))
///String temp 2 = double . tostring(nownum);
nf . setminimumcfractiondigits(num);
p>
re = prenum * nownum
nf . format(re);
result = double . tostring(re);
show . setText(null);
show.setText(result);
//show . setext(null);
operation = ' = ';
mark = true
break;
case "/":
num=max(getFloatNum( nownum), getFloatNum(prenum));
///String temp 2 = double . tostring(downum);
p>nf . setminimumcfractiondigits(num);
p>
re = prenum/nownum;
nf . format(re);
result = double . tostring(re);
show . setText(null);
show.setText(result);
//show . setext(null);
operation = ' = ';
mark = true
break;
default:break
}
}
else if(e.getSource()==about)
{
J옵션창. 메시지 대화 상자 표시(쇼, "간단한 계산기", "계산기 정보",
JOptionPane. 정보 메시지);
< p>}else if(e.getSource()==me)
{
JoptionPane. 메시지 대화상자 표시 (표시, "저작권 * * *", "작성자 정보",
JOptionPane. info_message);
}
else if(e. getSource()==exitFile)
{
system . exit(0);
}
else if(e.getSource()==copyMenu)
{
string copy text = show . gettext();
setClipboardText(clip, copy text);
}
else if(e . getsource()= = 붙여넣기 메뉴)
{try
{
show . setext(getClipboardText(clip));
}
catch(exception ee){}
}
}
public int getFloatNum(double a)
{
int num = 0
string s = double . tostring(a);
for(int I = 0; I& lts.length(). i++)
{
num = num+1;
char b = s . charat(I);
if(b== ' . ')
num = 0;
}
리턴 개수;
}
공개 int max(int a, int b)
{ if(a & gt; b)
리턴 a;
기타 b;
}
Protected static void setClipboardText(클립보드 클립, 문자열 쓰기)
{
전송가능 tText = new string selection(write me);
clip.setContents(tText, null );
}
보호된 정적 문자열 getClipboardText(클립보드 클립) 트리거
예외
{
/// 클립보드의 내용 가져오기
전송 가능 clipT = clip . get contents(null);
if (clip! = null) {
//콘텐츠가 텍스트 타입인지 확인합니다.
if(clipt . is dataflavorsupported(데이터 맛 . 문자열 flavour))
return(String)clipt . gettransferdata(data flavour . String flavour);
}
return null
}
}