1、java 简单计算器import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.util.regex.*;import javax.script.*;class CalculatorBoundary extends JFrame public static final int FRAME_WIDTH = 449;/定义窗口的宽度 public static final int FRAME_HEIGHT = 288;/定义窗口的高度 public static int FLAG = 0;/用于控制文本框的显
2、示 public static int ZERO = 1;/用于控制用户的输入,如:0009这种形式 public static final Font FNT = new Font (宋体, 0, 20);/定义字体类型 public static final Pattern P = Ppile (0-9);/定义一个正则表达式 private Matcher m; private static int COUNT1 = 0;/统计左括号的个数 private static int COUNT2 = 0;/统计右括号的个数 private String str; private StringB
3、uffer sb; private JButton jbu = new JButton20;/定义按钮数组 /定义面板 private JPanel jpan1 = new JPanel (); private JPanel jpan2 = new JPanel (); private JPanel jpan3 = new JPanel (); private JTextField jte = new JTextField ();/用于存放输入的每一个数 private JTextField jtf = new JTextField (0, 35);/定义文本框,用于显示用户输入的表达式和计算
4、结果 private JTextArea jta = new JTextArea (错误信息描述:);/定义文本域,用于显示表达的错误描述信息 private JLabel jlab1 = new JLabel (计算框:);/定义标签 private String strJbu = 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, +, -, *, /, (, ), =, ., BACK, CLEAR; /定义有参构造函数,用于初使化组件 public CalculatorBoundary (String s) super (s);/调用父函数设置窗口的标题 /初使化按钮组件 fo
5、r (int i = 0; i strJbu.length; i+) jbui = new JButton (strJbui); /取消按钮边框 for (int i = 0; i jbu.length; i+) jbui.setFocusPainted(false); public void launchFrame () this.setSize (FRAME_WIDTH, FRAME_HEIGHT);/设置窗口的宽度和高度 this.setLocation (400, 260);/设置窗口的位置 this.setResizable (false);/设置窗口不允许改变大小 this.set
6、Layout (null);/取消窗口的内在布局 jtf.setEditable(false);/设置文本框jtf为只读 jta.setEditable(false);/设置文本域jta为只读 jpan1.setLayout (new FlowLayout (FlowLayout.LEFT);/设置面板jpan1的内在布局 jpan1.setLocation (0, 0);/设置面板jpan1的位置 jpan1.setSize (444, 40);/设置面板japn1的大小 Font fon = new Font (黑体, 0, 20); jlab1.setFont (fon);/设置标签jl
7、ab1的字体 jpan1.add (jlab1); jtf.setFont (CalculatorBoundary.FNT);/设置文本框jtf的字体 jtf.setCaretPosition (1);/设置文本框jtf的光标位置 jpan1.add (jtf); /对面板jpan2的设置 jpan2.setLocation (0, 40); jpan2.setSize (300, 222); jpan2.setLayout (new GridLayout (5, 4, 5, 5); /添加按钮到面板jpan2上 for (int i = 0; i jbu.length; i+) jpan2.
8、add (jbui); /对面板jpan3的设置 jpan3.setLayout (new GridLayout (1, 1); jpan3.setLocation (300,40); jpan3.setSize (145, 223); /jpan3.setBackground (Color.RED); jta.setLineWrap (true); jta.setEditable (false); jpan3.add (jta); /把面板添加到窗口里 this.add (jpan1); this.add (jpan2); this.add (jpan3); this.setVisible
9、(true);/显示窗口 /添加窗口关闭事件 this.addWindowListener (new WindowAdapter () public void windowClosing (WindowEvent e) System.exit (0); ); /添加按钮事件 for (int i = 0; i jbu.length; i+) jbui.addActionListener (new Monitor (); /定义事件类用于监听事件源发出的事件 private class Monitor implements ActionListener public void actionPer
10、formed (ActionEvent e) if (CalculatorBoundary.FLAG = 1) jtf.setText (); CalculatorBoundary.FLAG = 0; /对按钮1的事件进行处理 if (e.getSource () = jbu0) if (jtf.getText ().equals (0) jtf.setText (); str = jtf.getText (); if (str.length () != 0) sb = new StringBuffer (str); str = sb.substring (sb.length () - 1,
11、sb.length (); if (!str.equals () & CalculatorBoundary.ZERO = 1) jtf.setText (jtf.getText () + 1); jte.setText (jte.getText () + 1); /对按钮2的事件进行处理 else if (e.getSource () = jbu1) if (jtf.getText ().equals (0) jtf.setText (); str = jtf.getText (); if (str.length () != 0) sb = new StringBuffer (str); st
12、r = sb.substring (sb.length () - 1, sb.length (); if (!str.equals () & CalculatorBoundary.ZERO = 1) jtf.setText (jtf.getText () + 2); jte.setText (jte.getText () + 2); /对按钮3的事件进行处理 else if (e.getSource () = jbu2) if (jtf.getText ().equals (0) jtf.setText (); str = jtf.getText (); if (str.length () !
13、= 0) sb = new StringBuffer (str); str = sb.substring (sb.length () - 1, sb.length (); if (!str.equals () & CalculatorBoundary.ZERO = 1) jtf.setText (jtf.getText () + 3); jte.setText (jte.getText () + 3); /对按钮4的事件进行处理 else if (e.getSource () = jbu3) if (jtf.getText ().equals (0) jtf.setText (); str =
14、 jtf.getText (); if (str.length () != 0) sb = new StringBuffer (str); str = sb.substring (sb.length () - 1, sb.length (); if (!str.equals () & CalculatorBoundary.ZERO = 1) jtf.setText (jtf.getText () + 4); jte.setText (jte.getText () + 4); /对按钮5的事件进行处理 else if (e.getSource () = jbu4) if (jtf.getText
15、 ().equals (0) jtf.setText (); str = jtf.getText (); if (str.length () != 0) sb = new StringBuffer (str); str = sb.substring (sb.length () - 1, sb.length (); if (!str.equals () & CalculatorBoundary.ZERO = 1) jtf.setText (jtf.getText () + 5); jte.setText (jte.getText () + 5); /对按钮6的事件进行处理 else if (e.
16、getSource () = jbu5) if (jtf.getText ().equals (0) jtf.setText (); str = jtf.getText (); if (str.length () != 0) sb = new StringBuffer (str); str = sb.substring (sb.length () - 1, sb.length (); if (!str.equals () & CalculatorBoundary.ZERO = 1) jtf.setText (jtf.getText () + 6); jte.setText (jte.getTe
17、xt () + 6); /对按钮7的事件进行处理 else if (e.getSource () = jbu6) if (jtf.getText ().equals (0) jtf.setText (); str = jtf.getText (); if (str.length () != 0) sb = new StringBuffer (str); str = sb.substring (sb.length () - 1, sb.length (); if (!str.equals () & CalculatorBoundary.ZERO = 1) jtf.setText (jtf.get
18、Text () + 7); jte.setText (jte.getText () + 7); /对按钮8的事件进行处理 else if (e.getSource () = jbu7) if (jtf.getText ().equals (0) jtf.setText (); str = jtf.getText (); if (str.length () != 0) sb = new StringBuffer (str); str = sb.substring (sb.length () - 1, sb.length (); if (!str.equals () & CalculatorBou
19、ndary.ZERO = 1) jtf.setText (jtf.getText () + 8); jte.setText (jte.getText () + 8); /对按钮9的事件进行处理 else if (e.getSource () = jbu8) if (jtf.getText ().equals (0) jtf.setText (); str = jtf.getText (); if (str.length () != 0) sb = new StringBuffer (str); str = sb.substring (sb.length () - 1, sb.length ()
20、; if (!str.equals () & CalculatorBoundary.ZERO = 1) jtf.setText (jtf.getText () + 9); jte.setText (jte.getText () + 9); /对按钮0的事件进行处理 else if (e.getSource () = jbu9) if (jtf.getText ().equals (0) jtf.setText (); str = jtf.getText (); if (str.length () != 0) sb = new StringBuffer (str); str = sb.subst
21、ring (sb.length () - 1, sb.length (); if (!str.equals () & CalculatorBoundary.ZERO = 1) jtf.setText (jtf.getText () + 0); jte.setText (jte.getText () + 0); if (jte.getText ().equals (0) CalculatorBoundary.ZERO = 0; System.out.println (jte.getText (): + jte.getText () + + ZERO: + CalculatorBoundary.Z
22、ERO); /对按钮+的事件进行处理 else if (e.getSource () = jbu10) str = jtf.getText (); if (str.length () != 0) sb = new StringBuffer (str); str = sb.substring (sb.length () - 1, sb.length (); m = CalculatorBoundary.P.matcher (str); if (m.matches () | str.equals () jtf.setText (jtf.getText () + +); jte.setText ()
23、; CalculatorBoundary.ZERO = 1; else if (str.equals (+) | str.equals (-) | str.equals (*) | str.equals (/) & jtf.getText ().length () != 1) jtf.setText (sb.substring (0, sb.length () - 1) + +); jte.setText (); CalculatorBoundary.ZERO = 1; /对按钮-的事件进行处理 else if (e.getSource () = jbu11) str = jtf.getTex
24、t (); if (str.equals (0) jtf.setText (); if (str.length () != 0) sb = new StringBuffer (str); str = sb.substring (sb.length () - 1, sb.length (); m = CalculatorBoundary.P.matcher (str); if (m.matches () | str.equals () | str.length () = 0 | str.equals () jtf.setText (jtf.getText () + -); jte.setText
25、 (); CalculatorBoundary.ZERO = 1; else if (str.equals (+) | str.equals (-) | str.equals (*) | str.equals (/) jtf.setText (sb.substring (0, sb.length () - 1) + -); jte.setText (); CalculatorBoundary.ZERO = 1; /对按钮*的事件进行处理 else if (e.getSource () = jbu12) str = jtf.getText (); if (str.length () != 0) sb = new StringBuffer (str); str = sb.substring (sb.length () - 1, sb.length (); m = CalculatorBoundary.P.matcher (str); if (m.matches () | str.equals () jtf.setText (j
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1