《Java计算器源码.doc》由会员分享,可在线阅读,更多相关《Java计算器源码.doc(25页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、计算器本计算器有显示过程和不显示过程两种功能,其中不显示过程为普通计算器,类似于windows自带的计算器;显示过程的,可以显示预算过程,其中包含所需应用的部分函数,以及格式化功能(最左上角,第一个键格式化,然后输入需要格式化的数位,然后点第二个数,显示格式化结果),当一次运算结束后,会在右边自动显示运算过程,并且运算过程处有保存,复制粘贴清除功能。代码如下:主类:import ZF.*;public class JiSuanQi public static void main(String args) Calculator c = new Calculator(计算器);包ZF中的类:Cal
2、culator类package ZF;/* * 计算器布局界面 */import java.io.*;import java.awt.*;import javax.swing.*;import java.awt.event.*;public class Calculator extends JFrame implements ActionListener static int xg = 1;/ 用于判断是否显示过程,初始状态为显示JSplitPane jsp1, jsp2, jsp3;static JTextField jtf;/ 文本框static JTextArea jta;/ 保存等的历
3、史文本区public static String zc;/ 用来暂时存储数字,实现第一个菜单选项JScrollPane JS;/ 专门用于给文本区提供滚动条public static FileDialog load, save;/ 声明引入对话框BufferedReader in;/ FileReader reader;/JP1 jp1;JP2 jp2;JP3 jp3;JP4 jp4;JP5 jp5;/ 菜单条JMenuBar mb;JMenu m0, m1, m2;JMenuItem m01, m02, m03, m04, m11, m12, m21, m22;public Calculat
4、or(String s) super(s);Container con = getContentPane();/ 桌面jp1 = new JP1();jp2 = new JP2();jp3 = new JP3();jp4 = new JP4();jp5 = new JP5();jta = new JTextArea();jta.setEditable(false);JS = new JScrollPane(jta);jsp1 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, false, jp1, jp2);jsp2 = new JSplitPane(JS
5、plitPane.VERTICAL_SPLIT, false, jp3, JS);jsp3 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, false, jp4, jp5);jtf = new JTextField();jtf.setText(String.valueOf(0);jtf.setEditable(false);jtf.setBackground(Color.white);load = new FileDialog(this, 读取历史运算过程, FileDialog.LOAD);load.setVisible(false);load.add
6、WindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) load.setVisible(false););save = new FileDialog(this, 保存历史运算过程, FileDialog.SAVE);save.setVisible(false);save.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) save.setVisible(false););/ 菜单条mb = ne
7、w JMenuBar();m0 = new JMenu(编辑);m1 = new JMenu(查看);m2 = new JMenu(帮助);m01 = new JMenuItem(复制);m02 = new JMenuItem(粘贴);m03 = new JMenuItem(读取);m04 = new JMenuItem(关闭);m01.addActionListener(this);m02.addActionListener(this);m03.addActionListener(this);m04.addActionListener(this);m11 = new JMenuItem(不显
8、过程);m12 = new JMenuItem(显示过程);m11.addActionListener(this);m12.addActionListener(this);m21 = new JMenuItem(帮助主题);m22 = new JMenuItem(关于计算器);m21.addActionListener(this);m22.addActionListener(this);m0.add(m01);m0.add(m02);m0.add(m03);m0.add(m04);m1.add(m11);m1.add(m12);m2.add(m21);m2.addSeparator();m2.
9、add(m22);mb.add(m0);mb.add(m1);mb.add(m2);setJMenuBar(mb);con.add(jtf, BorderLayout.NORTH);con.add(jsp1, BorderLayout.CENTER);con.add(jsp2, BorderLayout.EAST);con.add(jsp3, BorderLayout.WEST);setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);setBounds(100, 100, 800, 260);setResizable(false);setVisib
10、le(true);validate();public void actionPerformed(ActionEvent e) if (!jtf.getText().equals(error) if (e.getSource() = m01) zc = jtf.getText(); else if (e.getSource() = m02) if (zc != null) if (xg = 1) if (jtf.getText().equals(0)jtf.setText(zc);elsejtf.setText(jtf.getText() + zc); elsejtf.setText(zc);i
11、f (e.getSource() = m03) /读取文件Calculator.load.setVisible(true);Calculator.jta.setText(null);String s;if(Calculator.load.getFile()!=null)tryFile file=new File(Calculator.load.getDirectory(),Calculator.load.getFile();reader=new FileReader(file);in=new BufferedReader(reader);while(s=in.readLine()!=null)
12、Calculator.jta.append(s+n);in.close();reader.close();catch(IOException e2)Calculator.jta.append(读取错误); else if (e.getSource() = m04) System.exit(0); else if (e.getSource() = m11) / 换窗体大小,同时换规则setSize(320, 260);remove(jsp2);remove(jsp3);JP2.sf = 0;jtf.setText(String.valueOf(0);xg = 0;/ 改变菜单选项的可选性m03.
13、setEnabled(false);m04.setEnabled(false);/ 换对齐方式JP2.sf = 0;jtf.setEditable(false);jtf.setBackground(Color.white);jtf.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);validate(); else if (e.getSource() = m12) setSize(800, 260);add(jsp2, BorderLayout.EAST);add(jsp3, BorderLayout.WEST);JP2.sf
14、 = 0;jtf.setText(String.valueOf(0);xg = 1;m03.setEnabled(true);m04.setEnabled(true);JP2.eg = 0;jtf.setEditable(false);jtf.setBackground(Color.white);jtf.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);validate(); else if (e.getSource() = m21) JOptionPane.showMessageDialog(this,本计算器以模仿Win
15、dows系统自带计算器为主,详情请参照系统计算器“帮助主题”。, 帮助主题,JOptionPane.INFORMATION_MESSAGE); else if (e.getSource() = m22) new JA(关于计算器);JA类package ZF;import java.awt.*;import javax.swing.*;public class JA extends JFrame JLabel jl = new JLabel6;JTextField jtf1 = new JTextField6;JTextField jtf2 = new JTextField6;public J
16、A(String s) super(s);setLayout(new GridLayout(6, 3);jl0 = new JLabel(制作组:);jl1 = new JLabel(组成员:);for (int i = 2; i 6; i+) jli = new JLabel();jtf10 = new JTextField(小叮当);jtf11 = new JTextField(曹剑);jtf12 = new JTextField(李剑);jtf13 = new JTextField(王斌);jtf14 = new JTextField(翟鹏翔);jtf15 = new JTextFiel
17、d(奥然);jtf20 = new JTextField(第一组);jtf21 = new JTextField(项目经理);for (int i = 2; i 6; i+) jtf2i = new JTextField(程序员);for (int i = 0; i 6; i+) jtf1i.setEditable(false);jtf2i.setEditable(false);for (int i = 0; i 6; i+) add(jli);add(jtf1i);add(jtf2i);setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);set
18、Bounds(100, 100, 250, 250);setVisible(true);validate();JP1类package ZF;import java.awt.*;import javax.swing.*;import java.awt.event.*;public class JP1 extends JPanel implements ActionListener JButton b1, b2, b3;public JP1() setLayout(new GridLayout(1, 3, 5, 5);b1 = new JButton(Backspace);b2 = new JBu
19、tton(CE);b3 = new JButton(C);b1.addActionListener(this);b2.addActionListener(this);b3.addActionListener(this);add(b1);add(b2);add(b3);public void actionPerformed(ActionEvent e) String s = Calculator.jtf.getText();if (!Calculator.jtf.getText().equals(error) if (e.getSource() = b1) if (s.length() != 1
20、)Calculator.jtf.setText(s.substring(0, s.length() - 1);elseCalculator.jtf.setText(String.valueOf(0); else if (e.getSource() = b2) Calculator.jtf.setText(String.valueOf(0);if (Calculator.xg = 1)JP2.eg = 0;if (e.getSource() = b3) Calculator.jtf.setText(String.valueOf(0);if (Calculator.xg = 0)JP2.sf =
21、0;elseJP2.eg = 0;JP2类package ZF;/* * 计算器核心部分,基本算法按钮 */import java.awt.*;import javax.swing.*;import java.awt.event.*;public class JP2 extends JPanel implements ActionListener JButton b = new JButton24;static int eg, sf, gs;/ eg判断是否摁过按钮,sf判断算法, gs用于用于存放给格式化的位数static double sfg;/ sfg用于存放各种算法的结果float z
22、sg;/ zsg用于存放最终算术结果static String zgc, sgc, fh;/* * zgc用于存放zsg所转换而成的字符串, sgc用于存放sfg所转化而成的字符串, * fh用于存放显示的符号 */public JP2() setLayout(new GridLayout(4, 5, 5, 5);for (int i = 0; i 10; i+) bi = new JButton( + i);b10 = new JButton(+/-);b11 = new JButton(.);b12 = new JButton(+);b13 = new JButton(-);b14 = n
23、ew JButton();b15 = new JButton();b16 = new JButton(=);b17 = new JButton(sqrt);b18 = new JButton(%);b19 = new JButton(1/x);for (int i = 0; i 20; i+) bi.addActionListener(this);add(b7);add(b8);add(b9);add(b15);add(b17);add(b4);add(b5);add(b6);add(b14);add(b18);add(b1);add(b2);add(b3);add(b13);add(b19)
24、;add(b0);add(b10);add(b11);add(b12);add(b16);public static void NumberFa(int i) / 数字法则if (Calculator.xg = 0) / 如果不显示过程double zhs = Double.parseDouble(Calculator.jtf.getText();/ zhs用于存放暂时获取的数字,以下同理if (eg = 0) / 判断是否按过加法法则等按钮,如果没按过,将再次按得数字与原数字组成一个新的字符串if (zhs != 0 | zhs = 0& Calculator.jtf.getText().i
25、ndexOf(.) != -1) / 判断是原来的数字是否为零,为零或者不为零但有小数点Calculator.jtf.setText(Calculator.jtf.getText()+ String.valueOf(i); else/ 因为0不能做第一个数字(小数点除外),所以如果数字为零,则直接设置为按得数字Calculator.jtf.setText(String.valueOf(i); else / 如果按过法则按钮,则直接设置文本框Calculator.jtf.setText(String.valueOf(i);eg = 0; else if (eg = 0 | Calculator.
26、jtf.getText().equals(0)Calculator.jtf.setText(String.valueOf(i);elseCalculator.jtf.setText(Calculator.jtf.getText()+ String.valueOf(i);eg = 1;public static void SuanFa() / 算法法则if (Calculator.xg = 0) / 如果不显示过程double zhs = Double.parseDouble(Calculator.jtf.getText();String s = 零;/ 判断除数是否为0switch (sf)
27、case 0:/ 表示没有按加减乘除等sfg = zhs;break;case 1:sfg = sfg + zhs;break;case 2:sfg = sfg - zhs;break;case 3:sfg = sfg * zhs;break;case 4:if (zhs != 0)sfg = sfg / zhs;elses = null;break;if (s != null) sgc = String.valueOf(sfg);if (Double.parseDouble(sgc.substring(sgc.indexOf(.) = 0) / 判断小数点后的数字是否为零,如果为零,取整sg
28、c = String.valueOf(int) sfg);Calculator.jtf.setText(sgc); elseCalculator.jtf.setText(String.valueOf(sfg); elseCalculator.jtf.setText(error);eg = 1; else switch (sf) case 1:fh = +;break;case 2:fh = -;break;case 3:fh = ;break;case 4:fh = ;break;Calculator.jtf.setText(Calculator.jtf.getText() + fh);eg
29、= 1;public void actionPerformed(ActionEvent e) try if (!Calculator.jtf.getText().equals(error) for (int i = 0; i 10; i+) / 按钮事件if (e.getSource() = bi) if (Calculator.jtf.getText().length() 44& Calculator.xg = 0| Calculator.jtf.getText().length() 112& Calculator.xg = 1) / 输入的位数不超过当前文本框NumberFa(i);for
30、 (int i = 12; i = 0) double zhs = Math.sqrt(Double.parseDouble(Calculator.jtf.getText();String s = String.valueOf(zhs);if (Double.parseDouble(s.substring(s.indexOf(.) = 0) s = String.valueOf(int) zhs);Calculator.jtf.setText(s); elseCalculator.jtf.setText(s); elseCalculator.jtf.setText(error); else if (Cal