diff --git a/src/java2020spring/Calculator0106.java b/src/java2020spring/Calculator0106.java new file mode 100644 index 0000000000000000000000000000000000000000..ad315228555616ad1eb798adf6b675a431f85d77 --- /dev/null +++ b/src/java2020spring/Calculator0106.java @@ -0,0 +1,317 @@ + +package java2020spring; + +import javax.swing.*; +import javax.swing.border.Border; +import javax.swing.text.JTextComponent; + +import java.awt.*; + import java.awt.event.*; + import java.lang.*; + import java.lang.*; + + @SuppressWarnings("unused") + public class Calculator0106 extends JFrame implements ActionListener + { + + private static final StringBuffer Formula = null; + private JPanel p1 = new JPanel(); + private JPanel p2 = new JPanel(); + private JTextField t1; //文本框1用来显示输入信息 + private JTextField t2; //文本框2用来显示结果信息 + private JLabel label; + StringBuffer str; + double x,y; //x和y都是运算数 + int z; + + JButton n1,n2,n3,n4,n5,n6,n7,n8,n9,n0; + JButton b1,b2, b3, b4, b5, b6, b7, b8, b9, b10,b11,b12,b13,b14,b15;//算术功能按钮 + + @SuppressWarnings("null") + public Calculator0106 () + { + super("计算器"); //窗口名称 + Container c = getContentPane(); //创建内容面板对象 + + t1 = new JTextField(30); + t1.setEditable(false); //只能显示,不能编辑 + t2 = new JTextField(30); + t2.setEditable(false); //只能显示,不能编辑 + label = new JLabel("计算器"); + label.setForeground(Color.black); + + //创建一个空字符串缓冲区 + str=new StringBuffer(); + + + p2.add(label); + p2.add(t2); + p2.add(t1); + p2.setLayout(new GridLayout(4,1)); + + n1 =new JButton("1"); n2= new JButton ("2"); + n3= new JButton("3"); n4= new JButton("4"); + n5= new JButton("5"); n6= new JButton("6"); + n7= new JButton("7"); n8= new JButton("8") ; + n9= new JButton("9"); n0= new JButton("0"); + + b1= new JButton("/"); + b3= new JButton("*"); + b5= new JButton("+"); + b7= new JButton("-"); + b9= new JButton("-/+"); + b2= new JButton("back"); + b4= new JButton("C"); + b6= new JButton("√") ; + b8= new JButton("="); + b10=new JButton("."); + b11=new JButton("sin"); + b12=new JButton("cos"); + b13=new JButton("tan"); + b14=new JButton("x^2"); + b15=new JButton("x^3"); + + p1.add(n1); + p1.add(n2); + p1.add(n3); + p1.add(n4); + p1.add(n5); + p1.add(n6); + p1.add(n7); + p1.add(n8); + p1.add(n9); + p1.add(n0); + + b1.setForeground(Color.blue); + b3.setForeground(Color.blue); + b5.setForeground(Color.blue); + b7.setForeground(Color.blue); + b4.setForeground(Color.blue); + b6.setForeground(Color.blue); + b8.setForeground(Color.red); + b2.setForeground(Color.blue); + b9.setForeground(Color.blue); + b10.setForeground(Color.blue); + b11.setForeground(Color.blue); + b12.setForeground(Color.blue); + b13.setForeground(Color.blue); + b14.setForeground(Color.blue); + b15.setForeground(Color.blue); + + Container con=getContentPane(); + + con.setBackground(new Color(0,173,232)); + p1.add(b1); + p1.add(b3); + p1.add(b5); + p1.add(b7); + p1.add(b9); + p1.add(b10); + p1.add(b6); + p1.add(b11); + p1.add(b12); + p1.add(b13); + p1.add(b2); + p1.add(b4); + p1.add(b14); + p1.add(b15); + p1.add(b8); + p1.setLayout(new GridLayout(5,4,4,3)); + + //注册监听器 + b9.addActionListener(this); b10.addActionListener(this); + b1.addActionListener(this); b2.addActionListener(this); + b3.addActionListener(this); b4.addActionListener(this); + b5.addActionListener(this); b6.addActionListener(this); + b7.addActionListener(this); b8.addActionListener(this); + b11.addActionListener(this); b12.addActionListener(this); + b13.addActionListener(this); b14.addActionListener(this); + b15.addActionListener(this); + + n0.addActionListener(this); n1.addActionListener(this); + n2.addActionListener(this); n3.addActionListener(this); + n4.addActionListener(this); n5.addActionListener(this); + n6.addActionListener(this); n7.addActionListener(this); + n8.addActionListener(this); n9.addActionListener(this); + + c.add(p2); + c.add(p1); + c.setLayout(new FlowLayout()); + + setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //设置窗口关闭动作 + setVisible(true); //设置为可见 + setSize(350,300); //设置窗口大小 + setResizable(false); + } + +public static void main(String[] args) + + { Calculator0106 f = new Calculator0106(); } + + +//按钮的事件处理 +public void actionPerformed(ActionEvent e) +{ +try +{ + + if(e.getSource()==b9)//单击"+/-"选择输入的运算数是正数还是负数 +{ + x=Double.parseDouble(t1.getText().trim());//trim函数作用是去掉字符串中的空格 + t1.setText(""+(-x)); + t1.setHorizontalAlignment(JTextField.RIGHT); +} +else if (e.getSource()==b5)//单击加号按钮获得x的值和z的值并清空y的值 +{ + x=Double.parseDouble(t1.getText().trim()); + str.setLength(0); + y=0d; + z=0; +} +else if(e.getSource()==b7)//单击减号按钮获得x的值和z的值并清空y的值 +{ + x=Double.parseDouble(t1.getText().trim()); + str.setLength(0); + y=0d; + z=1; +} +else if(e.getSource()==b3)//单击乘号按钮获得x的值和z的值并清空y的值 +{ + x=Double.parseDouble(t1.getText().trim()); + str.setLength(0); + y=0d; + z=2; +} +else if(e.getSource()==b1)//单击除号按钮获得x的值和z的值并清空y的值 +{ + x=Double.parseDouble(t1.getText().trim()); + str.setLength(0); + y=0d; + z=3; +} +else if(e.getSource()==b8)//单击等号按钮输出计算结果 +{ + str.setLength(0); + +switch(z) +{ +case 0: t1.setText(""+(x+y)); t1.setHorizontalAlignment(JTextField.RIGHT);break; +case 1: t1.setText(""+(x-y)); t1.setHorizontalAlignment(JTextField.RIGHT);break; +case 2: t1.setText(""+(x*y)); t1.setHorizontalAlignment(JTextField.RIGHT);break; +case 3: t1.setText(""+(x/y)); t1.setHorizontalAlignment(JTextField.RIGHT);break; +} +} + +else if(e.getSource()==b11) { //添加三角函数sin事件处理 + double i=Double.parseDouble( t1.getText()); + t1.setText(String.valueOf(Math.sin(i))); + } +else if(e.getSource()==b12) { //添加三角函数cos事件处理 + double i=Double.parseDouble(t1.getText()); + t1.setText(String.valueOf(Math.cos(i))); + } +else if(e.getSource()==b13) { //添加三角函数tan事件处理 + double i=Double.parseDouble(t1.getText()); + t1.setText(String.valueOf(Math.tan(i))); + } +else if(e.getSource()==b14) { //添加平方事件处理 + double i=Double.parseDouble(t1.getText()); + t1.setText(String.valueOf(i*i)); + } +else if(e.getSource()==b15) { //添加立方事件处理 + double i=Double.parseDouble(t1.getText()); + t1.setText(String.valueOf(i*i*i)); + } + +else if(e.getSource()==b4) //选择"C"清零 + { + t1.setText("0"); //把文本框清零 + t1.setHorizontalAlignment(JTextField.RIGHT); //文本对齐右边 + str.setLength(0); //清空字符串缓冲区以准备接收新的输入运算数 + } +else if(e.getSource()==b10)//单击"."按钮输入小数 +{ +if(t1.getText().trim().indexOf('.')!=-1)//判断字符串中是否已经包含了小数点 +{ +} +else //如果没有小数点 +{ +if(t1.getText().trim().equals("0"))//如果初时显示为0 +{ + t1.setText(str.append(e.getActionCommand()).toString()); + t1.setHorizontalAlignment(JTextField.RIGHT); +} +else if(t1.getText().trim().equals(""))//如果初时显示为空则不做任何操作 +{} +else +{ + t1.setText(str.append(e.getActionCommand()).toString()); + t1.setHorizontalAlignment(JTextField.RIGHT); +} + +} +y=0d; +} + +else if(e.getSource()==b6) //求平方根 +{ + x=Double.parseDouble(t1.getText().trim()); +if(x<0) +{ + t1.setText("数字格式异常"); + t1.setHorizontalAlignment(JTextField.RIGHT); +} +else +{ + t1.setText(""+Math.sqrt(x)); + t1.setHorizontalAlignment(JTextField.RIGHT); +} + str.setLength(0); + y=0d; +} + +else +{ +if(e.getSource()==n0)//如果选择的是"0"这个数字键 +{ +if(t1.getText().trim().equals("0"))//如果显示屏显示的为零不做操作 +{} +else + t1.setText(str.append(e.getActionCommand()).toString()); + t1.setHorizontalAlignment(JTextField.RIGHT); +y=Double.parseDouble(t1.getText().trim()); +} +else if (e.getSource()==b2) //选择的是back键 +{ +if(!t1.getText().trim().equals("0"))//如果显示屏显示的不是零 +{ +if(str.length()!=1) +{ + t1.setText(str.delete(str.length()-1,str.length()).toString());//可能抛出字符串越界异常 + t1.setHorizontalAlignment(JTextField.RIGHT); +} +else +{ + t1.setText("0"); t1.setHorizontalAlignment(JTextField.RIGHT); + str.setLength(0); +} +} + y=Double.parseDouble(t1.getText().trim()); +} +else +{ + t1.setText(str.append(e.getActionCommand()).toString()); + t1.setHorizontalAlignment(JTextField.RIGHT); + y=Double.parseDouble(t1.getText().trim()); +} +} +} +catch(NumberFormatException e1){ t1.setText("数字格式异常"); + t1.setHorizontalAlignment(JTextField.RIGHT); } + +catch(StringIndexOutOfBoundsException e1){t1.setText("字符串索引越界"); + t1.setHorizontalAlignment(JTextField.RIGHT);} +} + + +}