當前位置:編程學習大全網 - 源碼下載 - 源代碼加減法交替除法的第壹步

源代碼加減法交替除法的第壹步

用swing做,找本書自己學著做。很簡單。可以學習寫壹個計算器給初學者。

下面是我大學時寫的壹個計算器。可以稍微修改壹下。

導入Java . awt . *;

導入Java . awt . data transfer . clipboard;

導入Java . awt . data transfer . *;

導入javax . swing . *;

導入Java . awt . event . *;

導入Java . text . *;

公共類M06001526 {

公共靜態void main(String[] args) {

新計算器();

}

}

類計算器擴展JFrame實現ActionListener

{

/**

*

*/

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;

double prenum = 0;

double nownum = 0;

number format nf = number format . getinstance();

布爾clear = false

布爾標記=假;

int dianornot = 0;

char操作;

clipboard clip = toolkit . getdefaulttoolkit()。getsystem clipboard();

計算器()

{

set layout(new flow layout());

setResizable(false);

setLocation(450,300);

setSize(250,250);

add(新JLabel(" ");

show . set editable(false);

添加(顯示);

JMenuBar main menu = new JMenuBar();//制作菜單

setJMenuBar(主菜單);

JMenu edit menu = new JMenu(" edit ");

JMenu view menu = new JMenu(" view ");

JMenu help menu = new JMenu(" help ");

main menu . add(edit menu);

mainMenu.add(視圖菜單);

main menu . add(help menu);

copy menu = new JMenuItem(" copy ");

paste menu = new JMenuItem(" paste ");

exit file = new JMenuItem(" exit ");

copy menu . addactionlistener(this);

paste menu . addactionlistener(this);

exit file . addactionlistener(this);

edit menu . add(copy menu);

edit menu . add(paste menu);

edit menu . add(exit file);

t = new JMenuItem("●standard ");

S = new JMenuItem("科學類型");

view menu . add(t);

viewMenu.add

t . addactionlistener(this);

s . addactionlistener(this);

about = new JMenuItem(" about ");

me = new JMenuItem(" author ");

helpMenu.add(關於);

helpMenu.add(我);

about . addactionlistener(this);

me . addactionlistener(this);

//Rectangle 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 div = new JButton("/");

Bt clear = new JButton(" reset ");

Back = new JButton(" Back ");

btop = new JButton("+/-");

btequ = new JButton(" = ");

添加(Bt 1);

添加(bt2);

添加(bt3);

添加(bt4);

添加(bt5);

添加(bt6);

添加(bt7);

添加(bt8);

添加(bt9);

添加(Bt 10);

添加(Bt 11);

add(btAdd);

add(Bt sub);

add(Bt mult);

add(Bt div);

添加(btop);

加(回);

添加(Bt clear);

添加(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);

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((operation = = ' = ' | | operation = = '+' | | operation = = '-' | | operation = = ' * ' | | operation = = '/')& amp;& ampmark==true)

show . settext(null);

show . append(" 1 ");

mark = false

}

else if (e.getSource()==bt2)

{ if((operation = = ' = ' | | operation = = '+' | | operation = = '-' | | operation = = ' * ' | | operation = = '/')& amp;& ampmark==true)

show . settext(null);

show . append(" 2 ");

mark = false

}

else if (e.getSource()==bt3)

{ if((operation = = ' = ' | | operation = = '+' | | operation = = '-' | | operation = = ' * ' | | operation = = '/')& amp;& ampmark==true)

show . settext(null);

show . append(" 3 ");

mark = false}

else if (e.getSource()==bt4)

{ if((operation = = ' = ' | | operation = = '+' | | operation = = '-' | | operation = = ' * ' | | operation = = '/')& amp;& ampmark==true)

show . settext(null);

show . append(" 4 ");

mark = false}

else if (e.getSource()==bt5)

{ if((operation = = ' = ' | | operation = = '+' | | operation = = '-' | | operation = = ' * ' | | operation = = '/')& amp;& ampmark==true)

show . settext(null);

show . append(" 5 ");

mark = false}

else if (e.getSource()==bt6)

{ if((operation = = ' = ' | | operation = = '+' | | operation = = '-' | | operation = = ' * ' | | operation = = '/')& amp;& ampmark==true)

show . settext(null);

show . append(" 6 ");

mark = false}

else if (e.getSource()==bt7)

{ if((operation = = ' = ' | | operation = = '+' | | operation = = '-' | | operation = = ' * ' | | operation = = '/')& amp;& ampmark==true)

show . settext(null);

show . append(" 7 ");

mark = false}

else if (e.getSource()==bt8)

{ if((operation = = ' = ' | | operation = = '+' | | operation = = '-' | | operation = = ' * ' | | operation = = '/')& amp;& ampmark==true)

show . settext(null);

show . append(" 8 ");

mark = false}

else if (e.getSource()==bt9)

{ if((operation = = ' = ' | | operation = = '+' | | operation = = '-' | | operation = = ' * ' | | operation = = '/')& amp;& ampmark==true)

show . settext(null);

show . append(" 9 ");

mark = false}

else if(e . getsource()= = Bt 10)

{

if((operation = = ' = ' | | operation = = '+' | | operation = = '-' | | operation = = ' * ' | | operation = = '/')& amp;& ampmark==true)

show . settext(null);

show . append(" 0 ");

mark = false}

else if(e . getsource()= = Bt 11)

{

string s 1 = show . gettext();

for(int I = 0;我& 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(操作!='='||mark==false))

{//show . append(" 2 ");

string s = show . gettext();

//show.insert(",s . length());

show . settext(null);

for(int I = 0;我& 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;我& ltS2 . length();i++)

{

char a2 = S2 . charat(I);

show . settext(show . gettext()+a2);

}

//show . settext(S2);

}

else {show.insert("-",0);

//show . setcaretposition(2);

//show . settext(S2);

}

}

else if(e.getSource()==btequ)

{字符串結果;

nownum = double . parse double(show . gettext());

開關(操作){

大小寫“+”:

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(結果);

//show . settext(null);

operation = ' = ';

mark = true

打破;

大小寫'-':

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(結果);

//show . settext(null);

operation = ' = ';

mark = true

打破;

大小寫' * ':

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(結果);

//show . settext(null);

operation = ' = ';

mark = true

打破;

案例“/”:

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(結果);

//show . settext(null);

operation = ' = ';

mark = true

打破;

默認:break

}

}

else if(e.getSource()==about)

{

Joptionpane。顯示消息對話框(顯示,“簡單計算器”,“關於計算器”,

JOptionPane。信息_消息);

}

else if(e.getSource()==me)

{

Joption窗格。顯示消息對話框(顯示,“版權歸* * *”,“關於作者”,

JOptionPane。信息_消息);

}

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()= = paste menu)

{嘗試

{

show . settext(getClipboardText(clip));

}

catch(異常ee){}

}

}

public int getFloatNum(雙a)

{

int num = 0;

string s = double . tostring(a);

for(int I = 0;我& 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;

}

受保護的靜態void setClipboardText(剪貼板剪輯,字符串寫入)

{

transferable tText = new string selection(write me);

clip.setContents(tText,null);

}

受保護的靜態字符串getClipboardText(剪貼板剪輯)引發

例外

{

//獲取剪貼板的內容

transferable clipT = clip . get contents(null);

如果(剪輯!= null) {

//檢查內容是否為文本類型。

if(clipt . is dataflavorsupported(data flavor . string flavor))

return(String)clipt . gettransferdata(data flavor . String flavor);

}

返回null

}

}

  • 上一篇:如何DIY出自己的機械鍵盤?
  • 下一篇:如果妳中了五百萬彩票之後,妳會怎麽做
  • copyright 2024編程學習大全網