1、用java编写一个简易的计算器代码精品资料import java.awt.BorderLayout;import java.awt.Color;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import javax.swing.JButton;import javax.swing.JFrame;import javax
2、.swing.JPanel;import javax.swing.JTextField;/*使用java 语言开发一个简易计算器 * * */public class TestJsq extends JFrame implements ActionListener, KeyListener private JTextField jtf;private JButton jb_bk, jb_ce, jb_c;private String xs = ;private double sum = 0;private int fh;public static void main(String args)
3、new TestJsq().creatCUI();public void creatCUI() JFrame jf = new JFrame();jf.setTitle(计算器);jtf = new JTextField(0.);jtf.setHorizontalAlignment(JTextField.RIGHT);jf.add(jtf, BorderLayout.NORTH);JPanel jp_main = new JPanel(new BorderLayout();jf.add(jp_main);JPanel jp1 = new JPanel(new GridLayout(1, 3,
4、1, 1);jb_bk = new JButton(Backspace);jb_bk.setForeground(Color.RED);jb_bk.addActionListener(this);jb_ce = new JButton(CE);jb_ce.setForeground(Color.RED);jb_ce.addActionListener(this);jb_c = new JButton(C);jb_c.setForeground(Color.RED);jb_c.addActionListener(this);jp1.add(jb_bk);jp1.add(jb_ce);jp1.ad
5、d(jb_c);jp_main.add(jp1, BorderLayout.NORTH);JPanel jp2 = new JPanel(new GridLayout(4, 5, 1, 1);String str = 7, 8, 9, /, sqrt, 4, 5, 6, *, %,1, 2, 3, -, 1/x, 0, +/-, ., +, = ;for (int i = 0; i 20; i+) JButton jb = new JButton(stri);jb.addActionListener(this);jb.setForeground(Color.blue);if (i = 3 |
6、i = 8 | i = 13 | i = 18 | i = 19) jb.setForeground(Color.RED);jp2.add(jb);jp_main.add(jp2, BorderLayout.CENTER);jf.setResizable(false);jf.setBounds(50, 50, 350, 350);jf.setVisible(true);Overridepublic void actionPerformed(ActionEvent e) String s = e.getActionCommand();if (s.equals(CE) | s.equals(C)
7、xs = ;jtf.setText(0.); else if (s.equals(Backspace) String str = xs.substring(0, xs.length() - 1);xs = str;jtf.setText(xs); else if (s.equals(7) | s.equals(8) | s.equals(9)| s.equals(4) | s.equals(5) | s.equals(6)| s.equals(1) | s.equals(2) | s.equals(3)| s.equals(0) | s.equals(.) xs = xs + s;jtf.se
8、tText(xs); else if (s.equals(+/-) double x = Double.parseDouble(xs);xs= + (-x);jtf.setText(xs); else if (s.equals(/) fh = 4;int point = xs.indexOf(.);if (point != -1) String strt = xs.substring(0, point);String z = xs.substring(point + 1, xs.length();int z_num = z.length();sum = Integer.decode(strt)
9、 + (double) (Integer.decode(z)/ (10 * z_num); else sum = Integer.decode(xs);xs = ;jtf.setText(null); else if (s.equals(*) fh = 3;int point = xs.indexOf(.);if (point != -1) String strt = xs.substring(0, point);String z = xs.substring(point + 1, xs.length();int z_num = z.length();sum = Integer.decode(
10、strt) + (double) (Integer.decode(z)/ (10 * z_num); else sum = Integer.decode(xs);xs = ;jtf.setText(null); else if (s.equals(-) fh = 2;int point = xs.indexOf(.);if (point != -1) String strt = xs.substring(0, point);String z = xs.substring(point + 1, xs.length();int z_num = z.length();sum = Integer.de
11、code(strt) + (double) (Integer.decode(z)/ (10 * z_num); else sum = Integer.decode(xs);xs = ;jtf.setText(null); else if (s.equals(+) fh = 1;int point = xs.indexOf(.);if (point != -1) String strt = xs.substring(0, point);String z = xs.substring(point + 1, xs.length();int z_num = z.length();sum = Integ
12、er.decode(strt) + (double) (Integer.decode(z)/ (10 * z_num); else sum = Integer.decode(xs);xs = ;jtf.setText(null); else if (s.equals(sqrt) double x = Double.parseDouble(xs);double x1 = Math.sqrt(x);xs = x1 + ;jtf.setText(xs); else if (s.equals(%) double x = Double.parseDouble(xs);x=0.01 * x*x;xs=x+
13、;jtf.setText(xs); else if (s.equals(1/x) if (xs = 0) jtf.setText(除数不能为0); else double x = Double.parseDouble(xs);double x1 = 1 / x;xs = x1 + ;jtf.setText(String.valueOf(xs); else if (s.equals(=) if (fh != 0) switch (fh) case 1:int point1 = xs.indexOf(.);if (point1 != -1) String s1 = xs.substring(0,
14、point1);String z1 = xs.substring(point1 + 1, xs.length();int z1_num = z1.length();xs = ;double ss1 = Integer.decode(s1)+ (double) Integer.decode(z1) / (10 * z1_num);sum = sum + ss1; else sum = sum + Integer.decode(xs);xs = ;fh = 0;break;case 2:int point2 = xs.indexOf(.);if (point2 != -1) String s2 =
15、 xs.substring(0, point2);String z2 = xs.substring(point2 + 1, xs.length();int z2_num = z2.length();xs = ;double ss2 = Integer.decode(s2)+ (double) Integer.decode(z2) / (10 * z2_num);sum = sum - ss2; else sum = sum - Integer.decode(xs);xs = ;fh = 0;break;case 3:int point3 = xs.indexOf(.);if (point3 !
16、= -1) String s3 = xs.substring(0, point3);String z3 = xs.substring(point3 + 1, xs.length();int z1_num = z3.length();xs = ;double s4 = Integer.decode(s3)+ (double) Integer.decode(z3) / (10 * z1_num);sum = sum * s4; else sum = sum * Integer.decode(xs);xs = ;fh = 0;break;case 4:int point4 = xs.indexOf(
17、.);if (point4 != -1) String s4 = xs.substring(0, point4);String z4 = xs.substring(point4 + 1, xs.length();int z4_num = z4.length();xs = ;double s5 = Integer.decode(s4)+ (double) Integer.decode(z4) / (10 * z4_num);sum = sum / s5; else sum = sum / Integer.decode(xs);xs = ;fh = 0;break;default:break;xs
18、 = String.valueOf(sum);jtf.setText(xs);fh = 0; else jtf.setText(null);Overridepublic void keyPressed(KeyEvent e) / TODO Auto-generated method stubOverridepublic void keyReleased(KeyEvent e) / TODO Auto-generated method stubOverridepublic void keyTyped(KeyEvent e) / TODO Auto-generated method stub仅供学习与交流,如有侵权请联系网站删除 谢谢8