java計(jì)算器源代碼Word文檔_第1頁
java計(jì)算器源代碼Word文檔_第2頁
已閱讀5頁,還剩6頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、package計(jì)算器; import java awt BorderLayout; import javaawt.Color; import java awt Font; import java .awt. GridLayout; import java awt event ActionEvent; import java awt event ActionListener; import java awt. event KeyAdapter; import java awt event KeyEvent: import java math BigDecimal; import javax sw

2、ingBorderFactory; import javax swing JButton; import javax swing JFramE; import javax swing JLabel; import javax swing JMenu; import javax swing JMenuBar; import javax swing JPanel; import javax swing JTextField; import javax. swing. UIManager; import javax swing. UnsupportedLookAndFeelException; im

3、port javax swing border BevelBorder; import javax swing text BadLocationException; public class Counter】 extends JFrame implements ActionListener public static final int DEF_DIV_SCALE 二 10; 人的粘度 private JMenuBar jmBar = new JMenuBar 0 ; 菜單條 private JMenu edit = new 編輯(E); private JMenu find = new JM

4、enu C查看(V); private JMenu help = new JMenu(幫助(H); /創(chuàng)建JPanel便丁管理 private private private private /創(chuàng)建文本域 private JTextField txt = new JTextFieldO ; /創(chuàng)建文本標(biāo)簽 private JLabel jlm = new JLabel0; private boolean flag = false;是否可以消除文本 private double a = 0; 數(shù)z?:a|J :YL private double b = 0; 數(shù)字b的值panTxt = new

5、JPanel0; panBack = new JPanel0; panMen = new JPanel0; panNum = new JPanel0; JPanel JPanel JPanel JPanel private double result = 0; 卜勺個(gè)數(shù) 果 private String sign = ,7 標(biāo)記運(yùn)算法則 private double m = 0; 何時(shí)儲(chǔ)心數(shù)據(jù) jbBack = new NewButton(Backspace, Color RED); jbCE = new NewButton (CE, Color. RED); jbC = new NewBut

6、ton(C, Color. RED); jbMC 二 new NewButton (MC, Color. RED); jbMR = new NewButton Color. RED); jbMS = new NewButton (/ZMS/Z, Color. RED); jbM = new NewButton(H+, Color. RED ; ( setSize (260, 260); setDefaultCloseOperation (JFrame EXIT_0_CL0SE); setLocationRelativeTo(null); setLayout(null); setJMenuBar

7、(jmBar); setTitleC 計(jì)算器); setResizable(false); initO ; / Container contentPane = getContentPane0; / Controller c 二 new Controller(this); / contentPane addKeyListener(c); private void init 0 /設(shè)置第一個(gè)JPanel里面的內(nèi)容 panTxt. setSize(230, 20); panTxt setLocation(15, 5); / panTxt setBorder(BorderFactory. create

8、LineBorder(Color RED); panTxt. setLayout (new BorderLayout 0) ; / 設(shè)置布局管理器 txt. setHorizontalAlignment (JTextField. RIGHT); 使光標(biāo)定位在右邊 txt setBorder (BorderFactory. createLoweredBevelBorder); 殳置邊RE 卩 下去 txt. setEditable(false); 設(shè)置是否可以編輯 txt setBackground(Color WHITE); / txt. setEnabled(true) ; /設(shè)置是否激活

9、/ txt. setFocusable (false); /設(shè)置是否可以獲得焦點(diǎn)(顯示光標(biāo)) txt. setTextCO); add(panTxt); panTxt add(txt); Controller c = new Controller(this); txt addKeyListener(c); /設(shè)置第二個(gè)JPanel里面的內(nèi)容 panBack setSize(190, 25); panBack setLocation(52, 35); /畫出邊框便于查看 /創(chuàng)建按鈕 private private private private private private private N

10、ewButton NewButton NewButton NewButton NewButton NewButton NewButton public Counterl0 / panBack setBorder(BorderFactory. createLineBorder(Color RED); GridLayout layPanBack = new GridLayout 0 ; 創(chuàng)建布局管理器 layPanBack. setHgap(5) ; /設(shè)置布局管理器的邊距 panBack. setLayout (layPanBack) ; / 設(shè)置布局管理器 /添加按鈕 jbBack. setF

11、ont (new Font C宋體,0, 12); panBack add(jbBack); panBack add(jbCE); panBack add(jbC); add(panBack); /設(shè)置第三個(gè)JPanel里面的內(nèi)容 panMen. setSize(40, 140); panMen setLocation(5, 65); GridLayout lay PanMen = new GridLayout (4, 1); 創(chuàng)建布局管理器 layPanMen. setVgap; 役用布討鎧;理器的邊距 panMen setLayout(layPanMen); / panMen setBor

12、der(BorderFactory createLineBorder(Color RED); panMen. add(jbMC); panMen. add(jbMR); panMen. add(jbMS); panMen. add(jbM); add(panMen); /設(shè)置第四個(gè)JPanel里面的內(nèi)容 panNum. setSize(200, 140); panNum setLocation(50, 65); / panNum setBorder(BorderFactory createLineBorder(Color RED); add(panNum); GridLayout layPan

13、Num = new GridLayout (4, 5); 創(chuàng)建布局管理器 panNum setLayout(layPanNum); newButtonNumO ; /添加菜單項(xiàng) jmBar add(edit); jmBar add(find); jmBar add(help); /設(shè)置文本標(biāo)簽的屬性和添加文本標(biāo)簽 jlm. setBorder (BorderFactory. createBevelBorder(1); jlm setSize(25, 25); jlm setLocation(15, 35); add(jlm); public void newButtonNumO NewButt

14、on jb7 = new NewButton(,z7z Color BLUB ; NewButton jb8 = new NewButton(8, Color BLUE); NewButton jb9 = new NewButton (?,9 Color. BLUE): NewButton jbDivide = new NewButColor REB ; NewButton jbSqrt = new NewButton (sqrt, Color. BLUE); NewButton jb4 = new NewButton(4, Color. BLUE): NewButton jb5 = new

15、NewButton C5Color BLUB ; NewButton jb6 = new NewButton(6, Color BLUE); NewButton jbRide = new NewButtonColor RED ; NewButton jbPer = new NewButton Color. BLUE); NewButton jbl 二 new NewButton(1, Color. BLUE): NewButton jb2 = new NewButton(2, Color. BLUE): NewButton jb3 = new NewButton(3, Color. BLUE)

16、; NewButton jbCut 二 new NewB ut Color .RED); NewButton jbReciprocal = new NewButtonCTx, Color. BLUE); NewButton jbO = new NewButton (/?0 Color. BLUE); NewButton jbMinus = new NewButtoColor BLUE); NewButton jbDot = new NewButton (Color BLUE); NewButton jbAdd = new NewButton(/z+z,, Color. RED); NewBut

17、ton jbEquals 二 new NewButtoColor. RED ; panNum add(jb7); panNum add(jb8); panNum add(jb9); panNum add(jbDivide); panNum add(jbSqrt); panNum add(jb4); panNum add(jb5); panNum add(jb6); panNum add(jbRide); panNum add(jbPer); panNum add(jbl); panNum add(jb2); panNum add(jb3); panNum add(jbCut); panNum

18、add(jbReciprocal); panNum add(jbO); panNum add(jbMinus); panNum add(jbDot); panNum add(jbAdd); panNum add(jbEquals); public static void main(StringLI args) try UIManager setLookAndFeel (UIManager getSystemLookAndFeelClassNameO); /設(shè)置和win7的布局一樣 catch (ClassNotFoundException e) e.printStackTrace 0; cat

19、ch (InstantiationException e) e.printStackTrace(); catch (IllegalAccessException e) e.printStackTrace 0; catch (UnsupportedLookAndFeelException e) e. printStackTrace 0; new Counter10 setVisible(true); private class NewButton extends JButton public NewButton(String s, Color c) super(s); / setBorder(B

20、orderFactory. createBevelBorder(BevelBorder RAISED); setBorder(BorderFactory. createEmptyBorder(); setForeground(c); setFocusable(false); /設(shè)置是否可以獲得焦點(diǎn)(顯示光標(biāo)) addActionListener(Counter1 this); /實(shí)現(xiàn)監(jiān)聽 public void actionPerformed(ActionEvent e) if (e. get Act ionCommandO = +) a = Double getText(); sign =;

21、 flag = true; else if (e. getActionCommandO = a = Double t getText(); ” H sign =-; flag = true; else if (e. get Act ionCommandO = *) a = Double getText(); sign = * ; flag = true; else if (e.getActionCommand 0 = /) a = Double get Tex t(); ” fH sign = / ; flag = true; else if (e. getActionCommand 0 =

22、=) try b = Double. pars 亡 Doubkltxt. getText (); catch (NumberFormatException nfe) b = 0; try if (sign = +) resuIt = doubleAdd(a, b); else if (sign 二二-“) resuIt = doubleSubi. b); else if (sign = *) result = doubleMulQ.y b); else if (sign = & b != 0) resuIt = doubleDiv(, b, 15); else result = b; catc

23、h (NumberFormatException nfe) result = 0; a = result; if (sign = & b = 0) txt. setTextCO); else txtsetText(result + ; flag = true; else if (e. getActionCommand 0 = C) a = 0; resuIt = 0; ” H sign = ; txt. setTextCO); else if (e. getActionCommand 0 = CE) txt. setTextCO); else if (e. getActionCommand 0

24、 = Backspace) try if (txt.getText(). length 0 1) txt. setText(txt. getText(0, txt.getText 0. length() 一 1); else txt. setTextCO); catch (BadLocationException el) txt. setTextCO); else if (e.getActionCommand0 = sqrt) a = Double getText(); resuIt = Math sqrt(a); a = resuIt; txt.setText(resuIt + ; flag

25、 = true; else if (e. getActionCommand 0 = %) b = Double pHseDoubkdxt getText(); result = doubleMui (doubleDiv(b, 100), a); txt.setText(resuIt + ); flag = true; else if (e. getActionCommand 0 = 1/x) a = Double p&rseDoubb(txtgetText (); result = 1 / a; a = result; txt.setText(resuIt + ; flag = true; e

26、lse if (e. get Act ionCommandO = +/一) txt. setText (“ + (Double p&rseDoub(txt getTextO); else if (e. get Act ionCommandO = MC) m = 0; jlm. setText(”); else if (e. getActionCommand 0 = MR) txt setText(m + “); else if (e. get Act ionCommandO = MS) m = Double p&rseDouble(tx t get Tex t(); jlm. setText(

27、“ M); flag = true; else if (e. getActionCommandO = 十) m += Double pars eDouble txt getTex t (); flag = true; :else boolean isDoc = false; for (int i = 0; i 1) txt setText (txt. getText (0, txt getText () length () 一 1); else txt. setTextCO); catch (BadLocationException el) txt. setText(zz0); else if

28、 (e. getKeyChar 0 = %) b = Double pdrseDoubleCtxtgetText(); result = doubleMui (doubleDi v(b, 100), a); txt. setText(result + ; flag = true; else if (e. getKeyChar0 = 0 x30 & e. getKeyChar0 = 0 x39 e. getKeyChar 0 =) boolean isDoc 二 false; for (int i = 0; i txt. getText 0. length0; i+) if (txt. getT

29、ext 0. charAt(i)=) isDoc 二 true; if (!isDoc e. getKeyChar0 !=) if (flag) txt setText(); flag = false; if (txt. getText (). equals (z,0,z) & e. getKeyChar () !=) txt setText(); txt setText (txt. getText () + e. getKeyChar 0); private class Controller extends KeyAdapter private Counterl frame; public Controller(Counterl frame) thisframe = frame; public void keyPressed(KeEvent e) frame keyAction(e); /提供相對(duì)精確的加法運(yùn)算 public static double doubleAdd(double dl, double d2) BigDecimal bdl = new BigDecimal(Double toStringdl); BigDecimal bd2 = new BigDecimal(Double toStrin(d2); return bdl. add(bd2

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論