




下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、importjavax.swing.*;把 javax包里面的 swing 包里面的所有東西 引進來importjava.awt.*;導(dǎo)入 awt 包里的所有 java 類importjava.awt.event.*;導(dǎo)入包 eventimportjava.text.DecimalFormat;處理文本、日期、數(shù)字和消息的類和接口importjava.awt.Color;importjava.awt.Dimension;importjava.awt.Font;importjava.awt.GridLayout;importjava.awt.Image;importjava.awt.Toolki
2、t;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;publicclassbiaozhunjisuanqiextendsJFrameimplementsActionListener public代表該類公有class 聲明一個類biaozhunjisuanqi為類名extends是繼承類implements窗體名稱biaozhunjisuanqi繼承 JFrame 類和ActionListener接口,并實現(xiàn)public voidactionPerformed(ActionEvent e)方法privat
3、estaticfinallongserialVersionUID= 1L;serialVersionUID相當于 java類的身份證 ,默認為 1L, serialVersionUID作用是序列化時保持版本的兼容性,即在版本升級時反序列化仍保持對象的唯一性privateJPaneljContentPane=null ; 創(chuàng)建面板對象 , null代表為這些變量賦初值為空,即不分配內(nèi)存空間,變量不指向任何地址。private 是訪問控制符,代表這些變量的訪問級別為私有,私有級別只能在當前類中進行訪問,這些( JPanel 、JTextArea、JPanel 、 JButton)是 java 中A
4、WT(包)中的類,用能進行桌面級應(yīng)用程序的開發(fā),就像平時用的word ,上面有各種文本框,菜單欄,工具欄按鈕等等,這些都是對象privateJPaneljPanel = null;JPanel 作為一個容器,可以包容一些組件,然后將這個 JPanel對象作為一個組件添加到另一個容器( 稱作父容器 ) 中privateJButtonjButton = null;9 JButton 是用戶進行計算時主要的使用工具,一般為其添加 actionPerformed事件。privateJButtonjButton1=null; ()privateJButtonjButton2=null; (sqrt)pr
5、ivateJButtonjButton3=null; (4)privateJButtonjButton4=null; (5)privateJButtonjButton5=null; (6)privateJButtonjButton6=null; (*)privateJButtonjButton7=null; (%)privateJButtonjButton8=null; (1)privateJButtonjButton9=null; (7)privateJButtonjButton10=null; (8)privateJButtonjButton11=null; (2)privateJButt
6、onjButton12=null; (3)privateJButtonjButton13=null; (-)privateJButtonjButton14=null; (1x)privateJButtonjButton15=null; (0)privateJButtonjButton16=null; (+-)privateJButtonjButton17=null; (.)privateJButtonjButton18=null; (+)privateJButtonjButton19=null; (=)privateJTextFieldjTextField=null; JTextField是一
7、個的文本框privateJTextFieldtextMemory=null; 作為輸入 M用privateJButtonjButton20=null; (MC)privateJButtonjButton21=null; (MR)privateJButtonjButton22=null; (MS)privateJButtonjButton23=null; (M+)privateJButtonjButton24=null; (Backspace)privateJButtonjButton25=null; (CE)privateJButtonjButton26=null; (C)doublememo
8、ryd ;使用內(nèi)存中存儲的數(shù)字intmemoryi ;科學(xué)計算器按鈕privateJButtonjButton31=null ; ( 十六進制 )privateJButtonjButton32=null; (sin)privateJButtonjButton33=null; (cos)privateJButtonjButton34=null; (十進制 )privateJButtonjButton35=null; (tan)privateJButtonjButton36=null ; ( 弧度 )privateJButtonjButton37=null ; ( 八進制 )privateJBut
9、tonjButton38=null; (X2)privateJButtonjButton39=null; (X3)privateJButtonjButton40=null ; ( 二進制 )privateJButtonjButton41=null; (Xy)privateJButtonjButton42=null; (n!)privateJButtonjButton43=null; (Pi)privateJButtonjButton44=null; (ln)privateJButtonjButton45=null; (log10)A-FprivateJButtonjButton46=null;
10、 (A)privateJButtonjButton47=null; (B)privateJButtonjButton48=null; (C)privateJButtonjButton49=null; (D)privateJButtonjButton50=null; (E)privateJButtonjButton51=null; (F)privateJPaneljPanel1 =null;privateJPaneljContentPane1=null ;privateStringstr ="" ; 聲明私有字符串變量 strprivatedoubledataone ; 變量
11、名用來存儲運算的數(shù)據(jù)privatechar Operator ='A' ;存放運算符privatebooleanpointIsTyped=false;判斷輸入的數(shù)據(jù)是否是小數(shù)privateJMenuBarjJMenuBar=null;privateJMenujMenu = null;JMenu 定義菜單 編輯鍵privateJMenuItemjMenuItem=null;JMenuItem 菜單中項的實現(xiàn)復(fù)制( Ctrl+C)privateJMenuItemjMenuItem1=null; 粘貼( Ctrl+V)privateJMenujMenu1 = null; 定義查看菜單
12、private JMenuItem jMenuItem3 = null ; 幫助主題 private JMenuItem jMenuItem4 = null ; 關(guān)于計算器 private JMenu jMenu2 = null ; 定義幫助菜單 private JMenuItem jMenuItem5 = null ; 標準型privateJMenuItemjMenuItem6=null; 科學(xué)型private JMenuItem jMenuItem7 = null ; 勾股定理 private JMenuItem jMenuItem8 = null ; 解一元二次方程DecimalForm
13、atdf ; 保留小數(shù)位數(shù)privateJButton getJButton() 使用 private JButton getJButton()方法添加按鈕并給按鈕命名if( jButton=null) jButton=new JButton();jButton.setText("9" );jButton.setFont(new Font( " 楷體 " , Font. BOLD, 30);使用jButton.setFont(new Font("楷體 ", Font.BOLD, 30)方法設(shè)置按鈕的字體jButton.setForeg
14、round(new Color();使用方法設(shè)置按鈕的字體顏色紅色、綠色和藍色jButton.setFocusable(false );jButton.addActionListener(new ActionListener() 相當于聲明了一個實現(xiàn)了 ActionListener接口的對象 .(因為可以看到其中的方法都被重寫實現(xiàn)了.) 更多具體細節(jié),可以參考java匿名類的使用, 就是把對象jButton注冊給事件監(jiān)聽者newActionListener() Public void actionPerformed(ActionEvent ae) ActionListener() Public
15、 void actionPerformed(ActionEvent )說明一下 new是一個匿名內(nèi)部類,用來實現(xiàn)對動作事件的處理publicvoidactionPerformed(ActionEvente) 這是ActionListener中定義的方法,事件監(jiān)聽器, 當定義的事件捕獲以后就會執(zhí)行這個方法里面的代碼str +="9" ;jTextField.setText(str ); jTextField是 api(ApplicationProgram Interface應(yīng)用程序接口) 里面得一個文本主鍵,要掉用它就要import實現(xiàn)接口,jtextfield文本框 .用來
16、建立文本框的,用戶可以在文本框輸入單行的文本。在JavaSwing 中的加 “ J ”字頭的文本輸入框包括兩種,單行文本輸入框(jtextfield)和多行文本輸入框(JTextArea)。);單擊后變色jButton.addMouseListener(new MouseListener()設(shè)置數(shù)字按鈕1 到9并添加監(jiān)控,使其在發(fā)生鼠標事件時改變按鈕顏色publicvoidactionPerformed(MouseEvent e) 這是 MouseListener中定義的方法, java 的事件監(jiān)聽器的一個,例如點擊按鈕、文本框里按下回車,就會調(diào)用這個里面的方法。jButton.setFore
17、ground(new Color();Foreground指前背景色publicvoidmouseClicked(MouseEvent e) 鼠標單擊方法(鼠標點擊后 )jButton.setForeground(new Color();publicvoidmouseEntered(MouseEvent e) 鼠標進入組件方法( 鼠標進入某個區(qū)域后)jButton.setForeground(new Color();publicvoidmouseExited(MouseEvent e) 鼠標退出組件方法( 鼠標離開某個區(qū)域后)jButton.setForeground(new Color();
18、publicvoidmousePressed(MouseEvent e) 鼠標按鍵按下后jButton.setForeground(new Color();publicvoidmouseReleased(MouseEvent e) 被按住的鼠標按鍵釋放后jButton.setForeground(new Color(););returnjButton;privateif個事件偵聽器JButton getJButton1() 注冊按鈕( jButton1=null) jButton1=new JButton();實例化一個對象, 給按鈕添中符合.jButton1.setText("&q
19、uot; );jButton1.setFont(new Font(" 楷體 " , Font.BOLD, 30);jButton1.setForeground(new Color();jButton1.addActionListener(new ActionListener() 給 jButton1new ActionListener(),當jButton被按下的時候,所需要發(fā)生的事情定義在添加一newActionListener()里publicvoidactionPerformed(ActionEvent e) str =jTextField.getText();Ope
20、rator=''dataone =Double.parseDouble( str ); 表示將轉(zhuǎn)換好的Double類型的數(shù)值賦值給dataone類str ="" ;jTextField.setText(str ););returnjButton1;privateif(JButton("sqrt");JButton getJButton2() jButton2=null) jButton2=new JButton("sqrt"對其設(shè)置監(jiān)控,調(diào)用Math.sqrt(); 開根號運算,定義jButton2 = new方法來實
21、現(xiàn)開根號運算,并在文本框中輸出結(jié)果jButton2.setFont(new Font(" 楷體 " , Font.BOLD, 30);Font 字體的意思,楷體,加粗,字體號大小30jButton2.setForeground(new Color();jButton2.addActionListener(new ActionListener() publicvoidactionPerformed(ActionEvent e) doublea=Double.parseDouble( jTextField.getText();表示將轉(zhuǎn)換好的 Double 類型的數(shù)值賦值給dou
22、ble 類型的變量。doubleb;if (a<0)jTextField.setText(" 負數(shù)不能開平方" );else b=Math. sqrt(a);jTextField.setText(Double.toString(b);Double.toString(next()將這個數(shù)double數(shù)轉(zhuǎn)化成 String類型(字符串));returnjButton2;privateJButton getJButton3() if( jButton3=null) jButton3=new JButton();jButton3.setText("4");
23、jButton3.setFont(new Font(" 楷體 " , Font. BOLD, 30);jButton3.setForeground(new Color();jButton3.setFocusable(false);jButton3.addActionListener(new ActionListener() publicvoidactionPerformed(ActionEvent e) str+="4"jTextField.setText(str););jButton3.addMouseListener(new MouseListene
24、r()publicvoidactionPerformed(MouseEvent e) jButton3.setForeground(new Color();publicvoidmouseClicked(MouseEvent e) jButton3.setForeground(new Color();publicvoidmouseEntered(MouseEvent e) jButton3.setForeground(new Color();publicvoidmouseExited(MouseEvent e) jButton3.setForeground(new Color();publicv
25、oidmousePressed(MouseEvent e) jButton3.setForeground(new Color();publicvoidmouseReleased(MouseEvent e) jButton3.setForeground(new Color(););returnjButton3;privateJButton getJButton4() if( jButton4=null) jButton4=new JButton();jButton4.setText("5");jButton4.setFont(new Font(" 楷體 "
26、 , Font. BOLD, 30);jButton4.setForeground(new Color();jButton4.setFocusable(false);jButton4.addActionListener(new ActionListener() publicvoidactionPerformed(ActionEvent e) str+="5"jTextField.setText(str););jButton4.addMouseListener(new MouseListener()publicvoidactionPerformed(MouseEvent e)
27、 jButton4.setForeground(new Color();publicvoidmouseClicked(MouseEvent e) jButton4.setForeground(new Color();publicvoidmouseEntered(MouseEvent e) jButton4.setForeground(new Color();publicvoidmouseExited(MouseEvent e) jButton4.setForeground(new Color();publicvoidmousePressed(MouseEvent e) jButton4.set
28、Foreground(new Color();publicvoidmouseReleased(MouseEvent e) jButton4.setForeground(new Color(););returnjButton4;privateJButton getJButton5() if( jButton5=null) jButton5=new JButton();jButton5.setText("6");jButton5.setFont(new Font(" 楷體 " , Font. BOLD, 30);jButton5.setForeground(
29、new Color();jButton5.setFocusable(false);jButton5.addActionListener(new ActionListener() publicvoidactionPerformed(ActionEvent e) str+="6"jTextField.setText(str););jButton5.addMouseListener(new MouseListener()publicvoidactionPerformed(MouseEvent e) jButton5.setForeground(new Color();public
30、voidmouseClicked(MouseEvent e) jButton5.setForeground(new Color();publicvoidmouseEntered(MouseEvent e) jButton5.setForeground(new Color();publicvoidmouseExited(MouseEvent e) jButton5.setForeground(new Color();publicvoidmousePressed(MouseEvent e) jButton5.setForeground(new Color();publicvoidmouseRele
31、ased(MouseEvent e) jButton5.setForeground(new Color(););returnjButton5;privateJButton getJButton6() if( jButton6=null) jButton6=new JButton();jButton6.setText("*" );jButton6.setFont(new Font(" 楷體 " , Font.BOLD, 30);jButton6.setForeground(new Color();jButton6.addActionListener(new
32、 ActionListener() publicvoidactionPerformed(ActionEvent e) str =jTextField.getText();Operator='*'dataone =Double.parseDouble( str );str ="" ;jTextField.setText(str ););returnjButton6;privateJButton getJButton7() if( jButton7=null) jButton7=new JButton("%" ); 設(shè)置百分比按鈕,jButt
33、on7 = newJButton("%");使用變量 i1 來存放從文本框中讀出的數(shù)據(jù)并轉(zhuǎn)換成double 類型,定義 double 類型變量 z1,z1=i1100實現(xiàn)求百分比運算,在使用jTextField.setText(Double.toString(z1)使其在文本框中輸出結(jié)果jButton7.setFont(new Font(" 楷體 " , Font.BOLD, 30);jButton7.setForeground(new Color();jButton7.addActionListener(new ActionListener() pub
34、licvoidactionPerformed(ActionEvent e) doublei1=Double.parseDouble( jTextField.getText();doublez1=i1100;jTextField.setText(Double.toString(z1););returnjButton7;privateJButton getJButton8() if( jButton8=null) jButton8=new JButton();jButton8.setText("1");jButton8.setFont(new Font(" 楷體 &q
35、uot; , Font. BOLD, 30);jButton8.setForeground(new Color();jButton8.setFocusable(false);jButton8.addActionListener(new ActionListener() publicvoidactionPerformed(ActionEvent e) str+="1"jTextField.setText(str ););jButton8 .addMouseListener(new MouseListener()publicvoidactionPerformed(MouseEv
36、ent e) jButton8.setForeground(new Color();publicvoidmouseClicked(MouseEvent e) jButton8.setForeground(new Color();publicvoidmouseEntered(MouseEvent e) jButton8.setForeground(new Color();publicvoidmouseExited(MouseEvent e) jButton8.setForeground(new Color();publicvoidmousePressed(MouseEvent e) jButto
37、n8.setForeground(new Color();publicvoidmouseReleased(MouseEvent e) jButton8.setForeground(new Color(););returnjButton8;privateJButton getJButton9() if( jButton9=null) jButton9=new JButton();jButton9.setText("7");jButton9.setFont(new Font(" 楷體 " , Font. BOLD, 30);jButton9.setForeg
38、round(new Color();jButton9.setFocusable(false);jButton9.addActionListener(new ActionListener() publicvoidactionPerformed(ActionEvent e) str+="7"jTextField.setText(str););jButton9.addMouseListener(new MouseListener()publicvoidactionPerformed(MouseEvent e) jButton9.setForeground(new Color();
39、publicvoidmouseClicked(MouseEvent e) jButton9.setForeground(new Color();publicvoidmouseEntered(MouseEvent e) jButton9.setForeground(new Color();publicvoidmouseExited(MouseEvent e) jButton9.setForeground(new Color();publicvoidmousePressed(MouseEvent e) jButton9.setForeground(new Color();publicvoidmou
40、seReleased(MouseEvent e) jButton9.setForeground(new Color(););returnjButton9;privateif(JButton getJButton10() jButton10=null) jButton10=new JButton();jButton10.setText("8" );jButton10.setFocusable(false);jButton10.setFont(new Font(" 楷體 " , Font.BOLD, 30);jButton10.setForeground(n
41、ew Color();jButton10.addActionListener(new ActionListener() publicvoidactionPerformed(ActionEvent e) str +="8" ;jTextField.setText(str ););jButton10public.addMouseListener(new MouseListener()voidactionPerformed(MouseEvent e) jButton10.setForeground(new Color();publicvoidjButton10mouseClick
42、ed(MouseEvent e) .setForeground(new Color();publicvoidjButton10mouseEntered(MouseEvent e) .setForeground(new Color();publicvoidjButton10mouseExited(MouseEvent e) .setForeground(new Color();publicvoidjButton10mousePressed(MouseEvent e) .setForeground(new Color();publicvoidjButton10mouseReleased(Mouse
43、Event e) .setForeground(new Color(););returnjButton10;privateif(JButton getJButton11() jButton11=null) jButton11=new JButton();jButton11.setText("2" );jButton11.setFont(new Font(" 楷體 " , Font.BOLD, 30);jButton11.setForeground(new Color();jButton11.setFocusable(false);jButton11.addActionListener(new ActionListener() publicvoidactionPerformed(ActionE
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 第八章+認識國家(美國、巴西)(串講課件)-2024-2025學(xué)年七年級地理下學(xué)期期末考點大串講(中圖版北京2024)
- GCP質(zhì)量管理精要
- Brand KPIs for online betting:Betfair in Brazil-英文培訓(xùn)課件2025.5
- 2025年(完整版)小升初數(shù)學(xué)公式
- AI大模型賦能區(qū)域醫(yī)療數(shù)字化醫(yī)聯(lián)體建設(shè)方案
- 華為公司干部管理與培養(yǎng)(一)7P
- 山東省德州市武城縣五校聯(lián)考2024-2025學(xué)年八年級下學(xué)期第二次月考數(shù)學(xué)試卷(答案不完整)
- 先進先出試題及答案
- 武漢理化試題及答案詳解
- 廣東省東莞市光正實驗學(xué)校2024-2025學(xué)年高一下學(xué)期期中考試英語試卷(解析版)
- 中小學(xué)學(xué)校德育工作管理制度匯編
- 路由與交換基礎(chǔ)知識試題及答案
- 混凝土澆筑清包合同范本
- 部編版2024-2025學(xué)年四年級下冊語文期末測試卷(含答案)
- 2025-2030中國軍用無人機行業(yè)市場現(xiàn)狀供需分析及投資評估規(guī)劃分析研究報告
- 生子前簽協(xié)議書
- 2025年中國工裝治具市場調(diào)查研究報告
- 收購爛尾樓可行性研究報告
- 加裝電梯補償協(xié)議書
- 人教版小學(xué)語文五年級下冊復(fù)習(xí)材料
- 2025年國際貿(mào)易實務(wù)課程考試試題及答案
評論
0/150
提交評論