java計算器實現(xiàn)源代碼上傳.doc_第1頁
java計算器實現(xiàn)源代碼上傳.doc_第2頁
java計算器實現(xiàn)源代碼上傳.doc_第3頁
java計算器實現(xiàn)源代碼上傳.doc_第4頁
java計算器實現(xiàn)源代碼上傳.doc_第5頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

java計算器實現(xiàn)源代碼上傳.txt性格本身沒有好壞,樂觀和悲觀對這個世界都有貢獻,前者發(fā)明了飛機,后者發(fā)明了降落傘。import java.awt.*;import java.awt.event.*;import javax.swing.*;public class testpublic static void main(String args )jisuan frame = new jisuan();/新建一個jisuan類對象frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/為該對象設(shè)置默認關(guān)閉動作frame.setResizable(false);/設(shè)置不可調(diào)整大小frame.show();/將該框架對象顯示出來class jisuan extends JFrame implements ActionListener/jisuan類繼承自JFrame類private JMenuBar menubar = new JMenuBar();private JMenu m1 = new JMenu(文件(field);private JMenu m2 = new JMenu(編輯(education);private JMenu m3 = new JMenu(幫助(help);/新建菜單欄private JMenuItem t2,t3,t4,t7,t8;private JPanel j1,j2;private JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;private JButton kaigen,qiuyu,pingfang,zengxuan,yuxuan ,jiecheng,jia,jian,cheng,chu,dengyu;private Container contenPane = this.getContentPane();/獲取當前的內(nèi)容窗格private JTextField p1,p2;private JLabel j0;private double tem1 = 0;private double tem2 = 0;private double tem3 = 0;private int biaozhi=0;public jisuan() /構(gòu)造函數(shù)super.setTitle(簡單計算器);/設(shè)置標題super.setSize(640,200);/設(shè)置尺寸super.setJMenuBar(menubar);menubar.add(m1);menubar.add(m2);menubar.add(m3);t2 = new JMenuItem(全部清零);t3 = new JMenuItem(打開圓周率);t4 = new JMenuItem(退出);m1.add(t2);m1.add(t3);m1.add(t4);t7 = new JMenuItem(寫字本);m2.add(t7);t8 = new JMenuItem(關(guān)于.);m3.add(t8);j1 = new JPanel();p1 = new JTextField(40);p1.setEditable(false);j0 = new JLabel(請輸入要計算的數(shù)字:);/ p2 = new JTextField(請輸入計算的數(shù)字,10);/ p3 = new JTextField(結(jié)果是,10);/ p3.setEditable(false);j1.add(j0);j1.add(p1);/ j1.add(p2);/ j1.add(p3);j2 = new JPanel();GridLayout f = new GridLayout(3,7);/新建一個布局管理器對象j2.setLayout(f);/將布局管理器對象加入到面板中b0 = new JButton(0);b1 = new JButton(1);b2 = new JButton(2);b3 = new JButton(3);b4 = new JButton(4);b5 = new JButton(5);b6 = new JButton(6);b7 = new JButton(7);b8 = new JButton(8);b9 = new JButton(9);/ j3 = new JPanel();j2.add(b0);j2.add(b1);j2.add(b2);j2.add(b3);j2.add(b4);j2.add(b5);j2.add(b6);j2.add(b7);j2.add(b8);j2.add(b9);jia = new JButton(+);jian = new JButton(-);cheng = new JButton(*);chu = new JButton(/);kaigen = new JButton(立方根);qiuyu = new JButton(平方根);pingfang = new JButton(平方);zengxuan = new JButton(sin);yuxuan = new JButton(cos);jiecheng = new JButton(階乘);dengyu = new JButton(=);j2.add(jia);j2.add(jian);j2.add(cheng);j2.add(chu);j2.add(kaigen);j2.add(qiuyu);j2.add(pingfang);j2.add(zengxuan);j2.add(yuxuan);j2.add(jiecheng);j2.add(dengyu);contenPane.add(j2,BorderLayout.CENTER);/將j2添加到內(nèi)容窗格中同時為其指定位置contenPane.add(j1,BorderLayout.NORTH);b0.addActionListener(this);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);jia.addActionListener(this);jian.addActionListener(this);cheng.addActionListener(this);chu.addActionListener(this);kaigen.addActionListener(this);qiuyu.addActionListener(this);pingfang.addActionListener(this);zengxuan.addActionListener(this);yuxuan.addActionListener(this);jiecheng.addActionListener(this);dengyu.addActionListener(this);/為按鈕添加事件監(jiān)聽器t2.addActionListener(this);t3.addActionListener(this);t4.addActionListener(this);t7.addActionListener(this);t8.addActionListener(this);/JMenuItem類對象添加事件監(jiān)聽器public void actionPerformed(ActionEvent e)if(e.getSource()=b0)p1.setText(0);j1.add(p1);tem1 = 0;else if(e.getSource() = b1)p1.setText(1);j1.add(p1);tem1 = 1;else if(e.getSource() = b2)p1.setText(2);j1.add(p1);tem1 = 2;else if(e.getSource() = b3)p1.setText(3);j1.add(p1);tem1 = 3;else if(e.getSource() = b4)p1.setText(4);j1.add(p1);tem1 = 4;else if(e.getSource() = b5)p1.setText(5);j1.add(p1);tem1 = 5;else if(e.getSource() = b6)p1.setText(6);j1.add(p1);tem1 = 6;else if(e.getSource() = b7)p1.setText(7);j1.add(p1);tem1 = 7;else if(e.getSource() = b8)p1.setText(8);j1.add(p1);tem1 = 8;else if(e.getSource() = b9)p1.setText(9);j1.add(p1);tem1 = 9;if(e.getSource()=jia)p1.setText(tem1+);j1.add(p1);tem2 = tem1;biaozhi = 0; if(e.getSource()=jian)p1.setText(tem1+-);j1.add(p1);tem2 = tem1;biaozhi = 1;if(e.getSource()=cheng)p1.setText(tem1+*);j1.add(p1);tem2 = tem1;biaozhi = 2;if(e.getSource()=chu)p1.setText(tem1+/);j1.add(p1);tem2 = tem1;biaozhi = 3; if(e.getSource()=kaigen) p1.setText(tem1+立方根); j1.add(p1); tem2 = tem1; biaozhi = 4; if(e.getSource()=qiuyu) p1.setText(tem1+平方根); j1.add(p1); tem2 = tem1; biaozhi = 5; if(e.getSource()=pingfang) p1.setText(tem1+平方); j1.add(p1); tem2 = tem1; biaozhi = 6; if(e.getSource()=zengxuan) p1.setText(tem1+sina); j1.add(p1); tem2 = tem1; biaozhi = 7; if(e.getSource()=yuxuan) p1.setText(tem1+cosa); j1.add(p1); tem2 = tem1; biaozhi = 8; if(e.getSource()=jiecheng) p1.setText(tem1+!); j1.add(p1); tem2 = tem1; biaozhi = 9; if(e.getSource()=dengyu) if(biaozhi =0) tem3 = tem2+tem1; if(biaozhi =1) tem3 = tem2-tem1; if(biaozhi =2) tem3 = tem2*tem1; if(biaozhi =3) tem3 = tem2/tem1; if(biaozhi =4) tem3 =Math.cbrt(tem1); if(biaozhi =5) tem3 =Math.sqrt(tem1); if(biaozhi =6) tem3 = tem1*tem1; if(biaozhi =7) tem3 = Math.sin(tem1); if(biaozhi =8) tem3 = Math.cos(tem1); if(biaozhi =9) tem3 = jisuanjiecheng(tem1); p1.setText(=+tem3); j1.add(p1); else if(e.getSource()=t2) p1.setText(0.0); j1.add(p1); else if(e.getSource()=t3) p1.setText(Math.PI+); j1.add(p1); else if(e.getSource()=t4) System.exit(0); else if(e.getSource()=t8) helpFile myhelpfile=new helpFile(); myhelpfile.show(); else if(e.getSource()=t7) repaint(); jishiben mynewjisuan=new jishiben(); mynewjisuan.show(); /實現(xiàn)監(jiān)聽器 private double jisuanjiecheng(double a) if(a=1) return 1;/遞歸出口 else return (jisuanjiecheng(a-1)*a);/實現(xiàn)遞歸計算階乘 class jishiben extends JFrame/實現(xiàn)記事本類 private Container contenPane1 = getContentPane(); private JTextArea text = new JTextArea(); private JButton tuibutton; private JPanel ji1,ji2; public jishiben() super.setTitle(文本編輯器);/設(shè)置標題 super.setSize(300,200);/設(shè)置尺寸 ji1 = new JPanel(); ji2 = new JPanel(); text = new JTextArea(請輸入你要記錄的文字,7,20);/行數(shù)為5列數(shù)為20 text.setLineWrap(true);/為文本框設(shè)置可回繞,即滿行之后自動實現(xiàn)換行 tuibutton = new JButton(退出); tuibutton.addActionListener(new tuiActionListener(); ji1.add(text); ji2.add(tuibutton); contenPane1.add(ji2,BorderLayout.SOUTH); contenPane1.add(ji1,BorderLayout.NORTH); private class tuiActionListener implements ActionListener public void actionPerformed(ActionEvent e) setVisible(false); class helpFile extends JFrame/實現(xiàn)幫助文檔類 private Container contenPane1 = getContentPane(); private JTextArea text = new

溫馨提示

  • 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)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論