如何利用Java?AWT?創(chuàng)建一個簡易計算器
摘要:手把手教你使用 Java AWT 創(chuàng)建一個簡易計算器。
一、關(guān)于AWT
AWT (抽象窗口工具包)是一個有助于構(gòu)建 GUI 的 API (圖形用戶界面)基于 java 應(yīng)用程序。GUI使用一些圖形幫助用戶交互。它主要由一組的類和方法所必需的,如在一個簡化的方式創(chuàng)建和管理的GUI按鈕,窗口,框架,文本框,單選按鈕 等等
我所提供的Java代碼對于動作監(jiān)聽器接口用于事件處理的計算器。
二、邏輯部分
1.對于數(shù)字按鈕
if(e.getSource()==b1){ //b1 代表數(shù)字 1 zt=l1.getText(); z=zt+"1";// 1 將合并在前一個值的末尾 l1.setText(z); }
當(dāng)按下任何數(shù)字按鈕時,標(biāo)簽 l1 中的任何值都將存儲在變量 zt 中,然后與相應(yīng)的數(shù)字連接,然后顯示在標(biāo)簽 l1 中,對于 NEGATIVE 和 DECIMAL PTS 按鈕,我們也做了類似的處理
2.對于算術(shù)按鈕
if(e.getSource()==badd){ //對應(yīng)加法 num1=Double.parseDouble(l1.getText()); z=""; l1.setText(z); check=1; }
現(xiàn)在,我們將標(biāo)簽 l1 的值轉(zhuǎn)換為 double
類型后,將其存儲到變量 num1
中,這在技術(shù)上將是第一個數(shù)字,然后將標(biāo)簽 l1 設(shè)置為 null
我們將只使用一個檢查變量來獲取這個特定的氣動按鈕(這里是 +)被點擊,這樣我們就可以在我們的 = 按鈕中執(zhí)行這個操作
3.對于等號按鈕
if(e.getSource()==bcalc){ num2=Double.parseDouble(l1.getText()); if(check==1) xd =num1+num2; if(check==2) xd =num1-num2; if(check==3) xd =num1*num2; if(check==4) xd =num1/num2; if(check==5) xd =num1%num2; l1.setText(String.valueOf(xd)); }
現(xiàn)在再次將值存儲 l1到 num2變量中,這將是算術(shù)上的第二個數(shù)字,然后檢查變量的值,check然后進(jìn)行相應(yīng)的操作,然后在標(biāo)簽中顯示結(jié)果 l1
4.對于清除按鈕
if(e.getSource()==bclr){ num1=0; num2=0; check=0; xd=0; z=""; l1.setText(z); }
此處將我們使用的所有變量更新為其默認(rèn)值 0
并將標(biāo)簽 l1 設(shè)置為 null,以便我們之后可以開始新的計算
5.對于退格按鈕
if(e.getSource()==bback){ zt=l1.getText(); try{ z=zt.substring(0, zt.length()-1); }catch(StringIndexOutOfBoundsException f){return;} l1.setText(z); }
這里只是l1通過使用substring函數(shù)刪除最后一位數(shù)字來更新值
并處理了一個 StringIndexOutOfBoundsException
當(dāng)我們在標(biāo)簽中的值為 null 并且仍然按下返回按鈕時發(fā)生的異常
6.特殊插件功能
我所做的只是處理了 EQUAL 和所有 ARITHMETIC Buttons
中的一個異常,并根據(jù)情況打印了所需的消息
算術(shù)按鈕:
try{ num1=Double.parseDouble(l1.getText()); }catch(NumberFormatException f){ l1.setText("Invalid Format"); return; }
等于按鈕:
try{ num2=Double.parseDouble(l1.getText()); }catch(Exception f){ l1.setText("ENTER NUMBER FIRST "); return; }
當(dāng)我們將值轉(zhuǎn)換為雙精度值時,但可以說,標(biāo)簽 l1 具有空值(即標(biāo)簽為空)并且我們?nèi)匀话聪逻@些按鈕,然后它將生成 NumberFormatException execption
,所以處理并打印所需的消息。
7.==例如==:
如果我點擊1然后+然后我點擊-而不是其他一些數(shù)字按鈕,因此這是一個無效的格式,并且當(dāng)-當(dāng)時被點擊時標(biāo)簽為空因此生成了execption所以只是處理它并在標(biāo)簽中打印無效格式
類似地,當(dāng)標(biāo)簽為空時,并且在這種情況下單擊= ENTER NUMBER FIRST
將顯示在標(biāo)簽內(nèi)
至此,我們結(jié)束了本次 Java AWT 教程。
三、GIF演示
四、附完整代碼
import java.awt.*; import java.awt.event.*; class MyCalc extends WindowAdapter implements ActionListener{ Frame f; Label l1; Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0; Button badd,bsub,bmult,bdiv,bmod,bcalc,bclr,bpts,bneg,bback; double xd; double num1,num2,check; MyCalc(){ f= new Frame("MY CALCULATOR"); // 實例化組件 l1=new Label(); l1.setBackground(Color.LIGHT_GRAY); l1.setBounds(50,50,260,60); b1=new Button("1"); b1.setBounds(50,340,50,50); b2=new Button("2"); b2.setBounds(120,340,50,50); b3=new Button("3"); b3.setBounds(190,340,50,50); b4=new Button("4"); b4.setBounds(50,270,50,50); b5=new Button("5"); b5.setBounds(120,270,50,50); b6=new Button("6"); b6.setBounds(190,270,50,50); b7=new Button("7"); b7.setBounds(50,200,50,50); b8=new Button("8"); b8.setBounds(120,200,50,50); b9=new Button("9"); b9.setBounds(190,200,50,50); b0=new Button("0"); b0.setBounds(120,410,50,50); bneg=new Button("+/-"); bneg.setBounds(50,410,50,50); bpts=new Button("."); bpts.setBounds(190,410,50,50); bback=new Button("back"); bback.setBounds(120,130,50,50); badd=new Button("+"); badd.setBounds(260,340,50,50); bsub=new Button("-"); bsub.setBounds(260,270,50,50); bmult=new Button("*"); bmult.setBounds(260,200,50,50); bdiv=new Button("/"); bdiv.setBounds(260,130,50,50); bmod=new Button("%"); bmod.setBounds(190,130,50,50); bcalc=new Button("="); bcalc.setBounds(245,410,65,50); bclr=new Button("CE"); bclr.setBounds(50,130,65,50); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); b5.addActionListener(this); b6.addActionListener(this); b7.addActionListener(this); b8.addActionListener(this); b9.addActionListener(this); b0.addActionListener(this); bpts.addActionListener(this); bneg.addActionListener(this); bback.addActionListener(this); badd.addActionListener(this); bsub.addActionListener(this); bmult.addActionListener(this); bdiv.addActionListener(this); bmod.addActionListener(this); bcalc.addActionListener(this); bclr.addActionListener(this); f.addWindowListener(this); //添加到框架 f.add(l1); f.add(b1); f.add(b2); f.add(b3); f.add(b4); f.add(b5);f.add(b6); f.add(b7); f.add(b8);f.add(b9);f.add(b0); f.add(badd); f.add(bsub); f.add(bmod); f.add(bmult); f.add(bdiv); f.add(bmod);f.add(bcalc); f.add(bclr); f.add(bpts);f.add(bneg); f.add(bback); f.setSize(360,500); f.setLayout(null); f.setVisible(true); } //關(guān)閉窗口 public void windowClosing(WindowEvent e) { f.dispose(); } public void actionPerformed(ActionEvent e){ String z,zt; //數(shù)字按鈕 if(e.getSource()==b1){ zt=l1.getText(); z=zt+"1"; l1.setText(z); } if(e.getSource()==b2){ zt=l1.getText(); z=zt+"2"; l1.setText(z); } if(e.getSource()==b3){ zt=l1.getText(); z=zt+"3"; l1.setText(z); } if(e.getSource()==b4){ zt=l1.getText(); z=zt+"4"; l1.setText(z); } if(e.getSource()==b5){ zt=l1.getText(); z=zt+"5"; l1.setText(z); } if(e.getSource()==b6){ zt=l1.getText(); z=zt+"6"; l1.setText(z); } if(e.getSource()==b7){ zt=l1.getText(); z=zt+"7"; l1.setText(z); } if(e.getSource()==b8){ zt=l1.getText(); z=zt+"8"; l1.setText(z); } if(e.getSource()==b9){ zt=l1.getText(); z=zt+"9"; l1.setText(z); } if(e.getSource()==b0){ zt=l1.getText(); z=zt+"0"; l1.setText(z); } if(e.getSource()==bpts){ //添加小數(shù)點 zt=l1.getText(); z=zt+"."; l1.setText(z); } if(e.getSource()==bneg){ //對于減 zt=l1.getText(); z="-"+zt; l1.setText(z); } if(e.getSource()==bback){ // 退格用 zt=l1.getText(); try{ z=zt.substring(0, zt.length()-1); }catch(StringIndexOutOfBoundsException f){return;} l1.setText(z); } //算術(shù)按鈕 if(e.getSource()==badd){ //對應(yīng)加法 try{ num1=Double.parseDouble(l1.getText()); }catch(NumberFormatException f){ l1.setText("Invalid Format"); return; } z=""; l1.setText(z); check=1; } if(e.getSource()==bsub){ //對應(yīng)減法 try{ num1=Double.parseDouble(l1.getText()); }catch(NumberFormatException f){ l1.setText("Invalid Format"); return; } z=""; l1.setText(z); check=2; } if(e.getSource()==bmult){ //對應(yīng)乘法 try{ num1=Double.parseDouble(l1.getText()); }catch(NumberFormatException f){ l1.setText("Invalid Format"); return; } z=""; l1.setText(z); check=3; } if(e.getSource()==bdiv){ //對應(yīng)除法 try{ num1=Double.parseDouble(l1.getText()); }catch(NumberFormatException f){ l1.setText("Invalid Format"); return; } z=""; l1.setText(z); check=4; } if(e.getSource()==bmod){ //對應(yīng)MOD/剩余 try{ num1=Double.parseDouble(l1.getText()); }catch(NumberFormatException f){ l1.setText("Invalid Format"); return; } z=""; l1.setText(z); check=5; } //結(jié)果按鈕 if(e.getSource()==bcalc){ try{ num2=Double.parseDouble(l1.getText()); }catch(Exception f){ l1.setText("ENTER NUMBER FIRST "); return; } if(check==1) xd =num1+num2; if(check==2) xd =num1-num2; if(check==3) xd =num1*num2; if(check==4) xd =num1/num2; if(check==5) xd =num1%num2; l1.setText(String.valueOf(xd)); } //清除標(biāo)簽和內(nèi)存 if(e.getSource()==bclr){ num1=0; num2=0; check=0; xd=0; z=""; l1.setText(z); } } //實例化 MyCalc 對象的 main 方法 public static void main(String args[]){ new MyCalc(); } }
到此這篇關(guān)于如何利用Java AWT 創(chuàng)建一個簡易計算器的文章就介紹到這了,更多相關(guān)Java AWT 創(chuàng)建計算器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring?aop?Pointcut?execution規(guī)則介紹
這篇文章主要介紹了spring?aop?Pointcut?execution規(guī)則,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11Jenkins 關(guān)閉和重啟詳細(xì)介紹及實現(xiàn)
這篇文章主要介紹了Jenkins的關(guān)閉、重啟的相關(guān)資料,用jar -jar jenkins.war來啟動jenkins服務(wù)器,那么我們?nèi)绾侮P(guān)閉或者重啟jenkins服務(wù)器呢,這里就給出實現(xiàn)的方法,需要的朋友可以參考下2016-11-11詳解使用spring validation完成數(shù)據(jù)后端校驗
這篇文章主要介紹了詳解使用spring validation完成數(shù)據(jù)后端校驗,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03