?? caculator.java
字號(hào):
package caculator;/** * * @author Yuyin Li */import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.lang.*;import java.math.*;//**********************[窗體及功能類]***********************class Window extends JFrame implements ActionListener,KeyListener,ItemListener{ double pi=3.1415926535897932384626433832795; //************--------菜單條相關(guān)聲明--------************** JMenuBar menubar; JMenu menu0,menu1,menu2,menu3; JMenuItem item0,item1,item2,item3,item4,item5,item6,item7; //item0:退出;item1:復(fù)制;item2:粘貼;item3:標(biāo)準(zhǔn)型;item4:科學(xué)型;item5:幫助;item6:關(guān)于 //**************--------布局相關(guān)聲明--------************** //frame_1 =new JFrame(); // frame_1.setContentPane(new MyPane()); // 設(shè)置ContentPane // frame_1.getContentPane().setLayout(new FlowLayout()); Box basebox; JPanel panel,panel1,panel2,panel3,panel4,panel22,panel5,panel51,panel52,panel6; //************--------文本框相關(guān)聲明--------************** JTextField t_show;//主顯示框 JLabel Null,Null1,Null2,Null3,Null4,Null5;//為布局而設(shè)的空插件 JTextField t_M,t_K,t_K0,t_K1,t_K2;//t_M指示儲(chǔ)存器狀態(tài),t_K指示括號(hào)狀態(tài) JTextField b_Ave,b_Sum,b_s,b_Dat; //************----------按鈕相關(guān)聲明--------************** JButton b_Backspace,b_CE,b_C;//操作區(qū)按鈕 JButton b_MC,b_MR,b_MS,b_Madd;//存儲(chǔ)器區(qū)按鈕 JButton b_0,b_1,b_2,b_3,b_4,b_5,b_6,b_7,b_8,b_9;//數(shù)字區(qū)按鍵 JButton b_add,b_sub,b_mul,b_div,b_sqrt,b_dao,b_sign,b_dian,b_deng,b_100;//標(biāo)準(zhǔn)操作按鍵 JButton Mod,And,Or,Xor,Lsh,Not,Int,A,B,C,D,E,F; JButton b_xy,b_ln,b_log,b_pi,b_x2,b_x3,b_sin,b_cos,b_tan,b_k1,b_k2,b_n,b_FE,b_dms,b_Exp,b_Sta;//科學(xué)操作按鍵 ButtonGroup group1,group2;//單選域 JRadioButton hex,dec,oct,b,j,h,td;//單選扭,hex是十六進(jìn)制,dec是十進(jìn)制,oct是八進(jìn)制,b是二進(jìn)制,j是角度,h是弧度,td是梯度 //************----------數(shù)據(jù)相關(guān)聲明--------************** double d[]={0,0,0,0,0,0,0,0,0,0,0};//數(shù)據(jù)1~10 String t[]={"0","0","0","0","0","0","0","0","0","0","0"};//數(shù)據(jù)相應(yīng)的字符串型 //記錄運(yùn)算符 mark指示當(dāng)前是那個(gè)操作符1-mark1,2-mark2. mark[1],[2]表示 0無(wú) 1加 2減 3乘 4除 int mark=1,marks[]={0,0,0,0,0}; int state=1;//記錄狀態(tài) 說(shuō)明現(xiàn)在輸入的是第幾個(gè)數(shù) int start=0;//記錄狀態(tài) 0說(shuō)明第一次輸入,數(shù)字替換以前內(nèi)容,1說(shuō)明在尾部添加數(shù)字 int deng=0;//記錄是否進(jìn)行過(guò)'='運(yùn)算 0沒(méi)有 1有 int k1=0,k2=0;//記錄括號(hào)狀態(tài) double Mdate=0;//M儲(chǔ)存器內(nèi)數(shù)值 int radio=1;//1記錄單選鈕選中“角度”,2-弧度 int b_jz=0; Window(String s,int style)//style表征窗體類型 1代表標(biāo)準(zhǔn)型 2代表科學(xué)型 { //**************--------窗體設(shè)置---------*************** super(s); if(style==1) setBounds(100,100,380,270); else setBounds(100,100,720,330); setVisible(true); setResizable(false); //**************-------菜單條設(shè)置--------*************** menubar = new JMenuBar(); menu0 = new JMenu("文件"); menu0.setMnemonic(KeyEvent.VK_F);//設(shè)置快捷鍵ALT+E item0 = new JMenuItem("退出",new ImageIcon("exit.jpg")); item0.addActionListener(this); menu1 = new JMenu("編輯(E)"); menu1.setMnemonic(KeyEvent.VK_E);//設(shè)置快捷鍵Alt+E item1 = new JMenuItem("復(fù)制",new ImageIcon("copy.jpg")); item1.addActionListener(this); item2 = new JMenuItem("粘貼",new ImageIcon("cv.jpg")); item2.addActionListener(this); item1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK)); item2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK)); menu2 = new JMenu("查看(V)"); menu2.setMnemonic(KeyEvent.VK_V);//設(shè)置快捷鍵Alt+V item3 = new JMenuItem("標(biāo)準(zhǔn)型",new ImageIcon("a.jpg")); item3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { dispose();//釋放當(dāng)前窗口 new Window("標(biāo)準(zhǔn)型計(jì)算器",1).setVisible(true); } }); item4 = new JMenuItem("科學(xué)型",new ImageIcon("b.jpg")); item4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { dispose();//釋放當(dāng)前窗口 new Window("科學(xué)型計(jì)算器",2).setVisible(true); } }); menu3 = new JMenu("幫助(H)"); menu3.setMnemonic(KeyEvent.VK_H);//設(shè)置快捷鍵Alt+H item5 = new JMenuItem("幫助",new ImageIcon("help.jpg")); item5.addActionListener(this); item6 = new JMenuItem("關(guān)于",new ImageIcon("about.jpg")); item6.addActionListener(this); item7 = new JMenuItem("主頁(yè)",new ImageIcon("mainweb.jpg")); item7.addActionListener(this); menu0.add(item0);//將item0添加到下拉菜單里去 menu1.add(item1);//將item1添加到下拉菜單里面去 menu1.addSeparator();//設(shè)置分割線 menu1.add(item2); menu2.add(item3); menu2.addSeparator(); menu2.add(item4); menu3.add(item5); menu3.addSeparator(); menu3.add(item6); menu3.addSeparator(); menu3.add(item7); menubar.add(menu0); menubar.add(menu1); menubar.add(menu2); menubar.add(menu3); setJMenuBar(menubar); //**************---------面板設(shè)置---------************** basebox = Box.createVerticalBox();//設(shè)置了一個(gè)窗口面板,就是計(jì)算器的菜單欄下面的長(zhǎng)方形區(qū)域 FlowLayout flow = new FlowLayout(); flow.setAlignment(FlowLayout.RIGHT); panel1 = new JPanel(); if (style==1) t_show = new JTextField(32);//顯示文本框 else t_show = new JTextField(63);//顯示文本框 t_show.setText("0"); t_show.setEditable(false); t_show.setHorizontalAlignment(SwingConstants.RIGHT);//顯示右對(duì)齊 t_show.setBackground(Color.white); panel1.setLayout(flow); panel1.add(t_show); basebox.add(panel1); panel2 = new JPanel(); panel2.setLayout(flow); t_K2 = new JTextField(5); t_K2.setHorizontalAlignment(SwingConstants.CENTER);//顯示右對(duì)齊 t_K2.setText(" -Inv ");//科學(xué)型計(jì)算器空按鈕 t_K2.setEditable(false); t_K1 = new JTextField(5); t_K1.setHorizontalAlignment(SwingConstants.CENTER);//顯示右對(duì)齊 t_K1.setText(" -Hyp ");//科學(xué)型計(jì)算器空按鈕 t_K1.setEditable(false); t_K0 = new JTextField(5); t_K0.setHorizontalAlignment(SwingConstants.CENTER);//顯示右對(duì)齊 t_K0.setText(" — ");//科學(xué)型計(jì)算器空按鈕 t_K0.setEditable(false); t_K = new JTextField(5); t_K.setHorizontalAlignment(SwingConstants.CENTER);//顯示右對(duì)齊 t_K.setText(" — ");//科學(xué)型計(jì)算器空按鈕 t_K.setEditable(false); Null = new JLabel(" "); Null1 = new JLabel(" "); Null2 = new JLabel(" "); Null3 = new JLabel(" "); Null4 = new JLabel(" "); Null5 = new JLabel(" "); t_M = new JTextField(5); t_M.setHorizontalAlignment(SwingConstants.CENTER);//顯示右對(duì)齊 t_M.setText(" — ");//標(biāo)準(zhǔn)型型計(jì)算器的空按鈕 t_M.setEditable(false); b_Ave = new JTextField(5); b_Ave.setHorizontalAlignment(SwingConstants.CENTER);//顯示右對(duì)齊 b_Ave.setText(" Ave ");// b_Ave.setEditable(false); b_Sum = new JTextField(5); b_Sum.setHorizontalAlignment(SwingConstants.CENTER);//顯示右對(duì)齊 b_Sum.setText(" Sum ");// b_Sum.setEditable(false); b_s = new JTextField(5); b_s.setHorizontalAlignment(SwingConstants.CENTER);//顯示右對(duì)齊 b_s.setText(" s ");// b_s.setEditable(false); b_Dat = new JTextField(5); b_Dat.setHorizontalAlignment(SwingConstants.CENTER);//顯示右對(duì)齊 b_Dat.setText(" Dat ");// b_Dat.setEditable(false); b_Backspace = new JButton(" Backspace "); b_Backspace.setText("<html><font color=red>Backspace</font></html>"); b_CE = new JButton(" CE "); b_CE.setText("<html><font color=red>CE</font></html>"); b_C = new JButton(" C "); b_C.setText("<html><font color=red>C</font></html>"); panel22 = new JPanel(); panel22.setLayout(new GridLayout(1,5,2,1)); if(style==2) { group1 = new ButtonGroup(); hex = new JRadioButton("十六進(jìn)制",false); hex.addItemListener(this);hex.addActionListener(this);hex.addKeyListener(this); dec = new JRadioButton("十進(jìn)制",true); dec.addItemListener(this);dec.addActionListener(this);dec.addKeyListener(this); oct = new JRadioButton("八進(jìn)制",false); oct.addItemListener(this);oct.addActionListener(this);oct.addKeyListener(this); b = new JRadioButton("二進(jìn)制",false); b.addItemListener(this);b.addActionListener(this);b.addKeyListener(this); group1.add(hex); group1.add(dec); group1.add(oct); group1.add(b); group2 = new ButtonGroup(); j = new JRadioButton("角度",true); j.addItemListener(this);j.addKeyListener(this); h = new JRadioButton("弧度",false); h.addItemListener(this);h.addKeyListener(this); td = new JRadioButton("梯度",false); td.addItemListener(this);td.addKeyListener(this); group2.add(j); group2.add(h); group2.add(td); //panel22.add(t_K0); //panel22.add(Null5); panel22.add(t_K2); panel22.add(t_K1); panel22.add(t_K); } panel22.add(t_M); if(style==2) { panel5 = new JPanel(); panel5.setLayout(flow); panel5.add(hex); panel5.add(dec); panel5.add(oct); panel5.add(b); panel5.add(Null); panel5.add(j); panel5.add(h); panel5.add(td); basebox.add(panel5); panel6 = new JPanel(); panel6.setLayout(flow); panel6.add(t_K0); panel6.add(Null5); panel6.add(panel22); panel6.add(Null2); panel6.add(b_Backspace); panel6.add(b_CE); panel6.add(b_C); basebox.add(panel6); } else { panel2.add(panel22); panel2.add(Null1); panel2.add(b_Backspace); panel2.add(b_CE); panel2.add(b_C); basebox.add(panel2); } panel = new JPanel(); panel3 = new JPanel(); //聲明添加所需按鈕 b_MC = new JButton("MC"); b_MC.setText("<html><font color=red>MC</font></html>"); b_MR = new JButton("MR"); b_MR.setText("<html><font color=red>MR</font></html>"); b_MS = new JButton("MS"); b_MS.setText("<html><font color=red>MS</font></html>"); b_Madd = new JButton("M+"); b_Madd.setText("<html><font color=red>M+</font></html>"); b_0 = new JButton("0"); b_0.setText("<html><font color=blue>0</font></html>"); b_1 = new JButton("1"); b_1.setText("<html><font color=blue>1</font></html>"); b_2 = new JButton("2"); b_2.setText("<html><font color=blue>2</font></html>"); b_3 = new JButton("3"); b_3.setText("<html><font color=blue>3</font></html>"); b_4 = new JButton("4"); b_4.setText("<html><font color=blue>4</font></html>"); b_5 = new JButton("5"); b_5.setText("<html><font color=blue>5</font></html>"); b_6 = new JButton("6"); b_6.setText("<html><font color=blue>6</font></html>"); b_7 = new JButton("7"); b_7.setText("<html><font color=blue>7</font></html>"); b_8 = new JButton("8"); b_8.setText("<html><font color=blue>8</font></html>"); b_9 = new JButton("9"); b_9.setText("<html><font color=blue>9</font></html>"); b_add = new JButton("+"); b_add.setText("<html><font color=red>+</font></html>"); b_sub = new JButton("-"); b_sub.setText("<html><font color=red>-</font></html>"); b_mul = new JButton("×"); b_mul.setText("<html><font color=red>×</font></html>"); b_div = new JButton("/"); b_div.setText("<html><font color=red>/</font></html>"); b_deng = new JButton("="); b_deng.setText("<html><font color=red>=</font></html>"); b_dian = new JButton("."); b_dian.setText("<html><font color=blue>.</font></html>"); b_sign = new JButton("+/-"); b_sign.setText("<html><font color=blue>+/-</font></html>");
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -