分别用Java和html+js写了两个计算器,虽然都有bug,先记录一下,怕丢了
JAVA
单行文本框(JTextField)+数字键输入(1、2、3、4、5、6、7、8、9、0、PI)+功能键运算(+、-、*、/、1/X、sin、cos……)
导入包
import java.awt.*; import java.awt.event.*; import java.math.*; import javax.swing.*; import java.awt.Color; import bsh.Interpreter;
整体布局
JFrame jf = new JFrame("徐天宇的哲学计算器");//对话框标题 Container pane=jf.getContentPane();//容器 pane.setLayout(new GridLayout(2,1));//网格总体布局二行一列 JPanel j1=new JPanel();//容器j1 j1.setLayout(new FlowLayout()); JPanel j2=new JPanel(); j2.setLayout(new GridLayout(4,8));//界面4*8按钮 final JTextField text = new JTextField(50);//文本域 text.setFont(new Font("楷体", Font.PLAIN, 30) );//文本域字体大小
将容器j1、j2加到容器pane
pane.add(j1); pane.add(j2);
将文本框加到容器j1中
j1.add(text);//将文本域加到j1容器中
容器j2的按键
JButton button1 = new JButton("1");//添加按钮 JButton button2 = new JButton("2"); JButton button3 = new JButton("3"); JButton button4 = new JButton("4"); JButton button5 = new JButton("5"); JButton button6 = new JButton("6"); JButton button7 = new JButton("7"); JButton button8 = new JButton("8"); JButton button9 = new JButton("9"); JButton button0 = new JButton("0"); JButton buttonadd = new JButton("+"); JButton buttonc = new JButton("C"); JButton buttonsubtract = new JButton("-"); JButton buttonmul = new JButton("×"); JButton buttondelete = new JButton("delete"); JButton buttondivide = new JButton("÷"); JButton buttonequal = new JButton("="); JButton buttonnegation = new JButton("+/-"); JButton buttonbackwards = new JButton("1/x"); JButton buttondecimal = new JButton("."); JButton buttonsin = new JButton("sin(X)"); JButton buttoncos = new JButton("cos(X)"); JButton buttontan = new JButton("tan(X)"); JButton buttoncsc = new JButton("csc(X)"); JButton buttonsec = new JButton("sec(X)"); JButton buttoncot = new JButton("cot(X)"); JButton buttonpi = new JButton("π"); JButton buttonsqrt = new JButton("√X"); JButton buttonpingfang = new JButton("X²"); JButton buttonlifang = new JButton("X³"); JButton buttonlifanggen = new JButton("³√X"); JButton buttonnuaa = new JButton("nuaa.work");
将各个按键加入到容器j2中
j2.add(button1); j2.add(button2); j2.add(button3); j2.add(buttonadd); j2.add(buttonc); j2.add(buttonsqrt); j2.add(buttonsin); j2.add(buttoncsc); j2.add(button4); j2.add(button5); j2.add(button6); j2.add(buttonsubtract); j2.add(buttondelete); j2.add(buttonpingfang); j2.add(buttoncos); j2.add(buttonsec); j2.add(button7); j2.add(button8); j2.add(button9); j2.add(buttonmul); j2.add(buttonbackwards); j2.add(buttonlifang); j2.add(buttontan); j2.add(buttoncot); j2.add(buttonnegation); j2.add(button0); j2.add(buttondecimal); j2.add(buttondivide); j2.add(buttonpi); j2.add(buttonlifanggen); j2.add(buttonequal); j2.add(buttonnuaa);
设置文本框和按键的前、后景色
text.setBackground(Color.yellow);//设置按钮前景和背景色 button1.setForeground(Color.orange); button1.setBackground(Color.gray); button2.setForeground(Color.orange); button2.setBackground(Color.gray); button3.setForeground(Color.orange); button3.setBackground(Color.gray); button4.setForeground(Color.orange); button4.setBackground(Color.gray); button5.setForeground(Color.orange); button5.setBackground(Color.gray); button6.setForeground(Color.orange); button6.setBackground(Color.gray); button7.setForeground(Color.orange); button7.setBackground(Color.gray); button8.setForeground(Color.orange); button8.setBackground(Color.gray); button9.setForeground(Color.orange); button9.setBackground(Color.gray); button0.setForeground(Color.orange); button0.setBackground(Color.gray); buttonnegation.setForeground(Color.CYAN); buttonnegation.setBackground(Color.DARK_GRAY); buttondecimal.setForeground(Color.CYAN); buttondecimal.setBackground(Color.DARK_GRAY); buttonpi.setForeground(Color.black); buttonpi.setBackground(Color.gray); buttonadd.setForeground(Color.red); buttonadd.setBackground(Color.gray); buttonsubtract.setForeground(Color.red); buttonsubtract.setBackground(Color.gray); buttonmul.setForeground(Color.red); buttonmul.setBackground(Color.gray); buttondivide.setForeground(Color.red); buttondivide.setBackground(Color.gray); buttonc.setForeground(Color.blue); buttonc.setBackground(Color.gray); buttondelete.setForeground(Color.blue); buttondelete.setBackground(Color.gray); buttonbackwards.setForeground(Color.blue); buttonbackwards.setBackground(Color.gray); buttonpi.setForeground(Color.orange); buttonpi.setBackground(Color.gray); buttonsin.setForeground(Color.green); buttonsin.setBackground(Color.gray); buttoncos.setForeground(Color.green); buttoncos.setBackground(Color.gray); buttontan.setForeground(Color.green); buttontan.setBackground(Color.gray); buttoncsc.setForeground(Color.magenta); buttoncsc.setBackground(Color.gray); buttonsec.setForeground(Color.magenta); buttonsec.setBackground(Color.gray); buttoncot.setForeground(Color.magenta); buttoncot.setBackground(Color.gray); buttonnuaa.setForeground(Color.white); buttonnuaa.setBackground(Color.gray); buttonsqrt.setForeground(Color.pink); buttonsqrt.setBackground(Color.gray); buttonpingfang.setForeground(Color.pink); buttonpingfang.setBackground(Color.gray); buttonlifang.setForeground(Color.pink); buttonlifang.setBackground(Color.gray); buttonlifanggen.setForeground(Color.pink); buttonlifanggen.setBackground(Color.gray); buttonequal.setForeground(Color.red); buttonequal.setBackground(Color.black);
按下数字按钮输入
button1.addActionListener(new ActionListener()//数字键输入 { public void actionPerformed(ActionEvent e) { text.setText(text.getText()+"1"); } }); button2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.setText(text.getText()+"2"); } }); button3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.setText(text.getText()+"3"); } }); button4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.setText(text.getText()+"4"); } }); button5.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.setText(text.getText()+"5"); } }); button6.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.setText(text.getText()+"6"); } }); button7.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.setText(text.getText()+"7"); } }); button8.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.setText(text.getText()+"8"); } }); button9.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.setText(text.getText()+"9"); } }); button0.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.setText(text.getText()+"0"); } }); buttonpi.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.setText(text.getText()+"3.141592653589793238462643383279"); } }); buttondecimal.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.setText(text.getText()+"."); } });
退位和清零
buttondelete.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.setText(text.getText().substring(0,text.getText().length()-1)); } }); /**/ buttonc.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.setText("0"); } });
功能键进行运算
buttonadd.addActionListener(new ActionListener()//功能键输入 { public void actionPerformed(ActionEvent e) { text.setText(text.getText()+"+"); } }); buttonequal.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { Interpreter a = new Interpreter(); Object obj = a.eval(text.getText()); text.setText(obj+""); }catch(Exception e2) { e2.printStackTrace(); } } }); buttonsubtract.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.setText(text.getText()+"-"); } }); buttonmul.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.setText(text.getText()+"*"); } }); buttondivide.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.setText(text.getText()+"/"); } }); buttonbackwards.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { double s; s = Double.parseDouble(text.getText()); text.setText(1/s+""); } }); buttonnegation.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { double s; s = Double.parseDouble(text.getText()); text.setText(-s+""); } }); buttonsqrt.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { double s; s = Double.parseDouble(text.getText()); text.setText(Math.sqrt(s)+""); } }); buttonpingfang.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { double s; s = Double.parseDouble(text.getText()); text.setText(s*s+""); } }); buttonlifang.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { double s; s = Double.parseDouble(text.getText()); text.setText(s*s*s+""); } }); buttonlifanggen.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { double s; s = Double.parseDouble(text.getText()); text.setText(Math.pow(s, 1.0/3.0)+""); } }); buttonsin.addActionListener(new ActionListener()//求正弦值 { public void actionPerformed(ActionEvent e) { double s; s = Double.parseDouble(text.getText()); text.setText(Math.sin(Math.PI*s/180)+""); } }); buttoncos.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { double s; s = Double.parseDouble(text.getText()); text.setText(Math.cos(Math.PI*s/180)+""); } }); buttontan.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { double s; s = Double.parseDouble(text.getText()); text.setText(Math.tan(Math.PI*s/180)+""); } }); buttoncsc.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { double s; s = Double.parseDouble(text.getText()); text.setText(1/(Math.sin(Math.PI*s/180))+""); } }); buttoncot.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { double s; s = Double.parseDouble(text.getText()); text.setText(1/(Math.tan(Math.PI*s/180))+""); } }); buttonsec.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { double s; s = Double.parseDouble(text.getText()); text.setText(1/(Math.cos(Math.PI*s/180))+""); } }); buttonnuaa.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent arg0) { try { Runtime.getRuntime().exec("cmd /c start http://nuaa.work"); } catch (Exception ex) { System.out.println("error"); ex.printStackTrace(); } } });
设置窗口位置
jf.setSize(800,300); jf.setVisible(true); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JS计算器
<!doctype html> <html> <head> <meta charset="utf-8"> <title>JS计算器</title> <style> .position-child{ position:absolute; top:50%; left:50%; transform:translate(-50%,-50%); } .footer{ clear: both; display: block; text-align: center; margin: 0px auto; position: absolute; bottom: 100px; width: 100%; } </style> <meta name="viewport"content="width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=2.0, user-scalable=yes"/> <link rel="stylesheet" type="text/css" href="untitled.css"> </head> <body> <div class="position-child"> <center> <div class="box"> <form action="" class="form1" name="form1"> <div class="header"> <center><input style=" width:220px;" type="text" id="text" name="text" class="text"/></center> </div> <table class="table"> <tr> <td> <input id="CE" style="height:55px; width:120px;" type="button" value="CE" onClick="jisuan(this.id)"/></td> <td> <input id="Backspace" style="height:55px; width:120px;" type="button" value="Backspace" onClick="jisuan(this.id)"/></td> </tr> </table> <table class="table"> <tr> <td> <input id="1" style="height:55px; width:55px;" type="button" value="1" onClick="jisuan(this.id)"/> </td> <td> <input id="2" style="height:55px; width:55px;" type="button" value="2" onClick="jisuan(this.id)"/> </td> <td> <input id="3" style="height:55px; width:55px;" type="button" value="3" onClick="jisuan(this.id)"/> </td> <td> <input id="+" style="height:55px; width:55px;" type="button" value="+" onClick="jisuan(this.id)"/> </td> </tr> <tr> <td> <input id="4" style="height:55px; width:55px;" type="button" value="4" onClick="jisuan(this.id)"/> </td> <td> <input id="5" style="height:55px; width:55px;" type="button" value="5" onClick="jisuan(this.id)"/> </td> <td> <input id="6" style="height:55px; width:55px;" type="button" value="6" onClick="jisuan(this.id)"/> </td> <td> <input id="-" style="height:55px; width:55px;" type="button" value="-" onClick="jisuan(this.id)"/> </td> </tr> <tr> <td> <input id="7" style="height:55px; width:55px;" type="button" value="7" onClick="jisuan(this.id)"/> </td> <td> <input id="8" style="height:55px; width:55px;" type="button" value="8" onClick="jisuan(this.id)"/> </td> <td> <input id="9" style="height:55px; width:55px;" type="button" value="9" onClick="jisuan(this.id)"/> </td> <td> <input id="*" style="height:55px; width:55px;" type="button" value="*" onClick="jisuan(this.id)"/> </td> </tr> <tr> <td> <input id="/" style="height:55px; width:55px;" type="button" value="/" onClick="jisuan(this.id)"/> </td> <td> <input id="0" style="height:55px; width:55px;" type="button" value="0" onClick="jisuan(this.id)"/> </td> <td> <input id="." style="height:55px; width:55px;" type="button" value="." onClick="jisuan(this.id)"/></td> <td> <input id="=" style="height:55px; width:55px;" type="button" value="=" onClick="jisuan(this.id)"/> </td> </tr> </table> </form> </div> </center> <script type="text/javascript" src="https://cdn.bootcss.com/canvas-nest.js/1.0.1/canvas-nest.min.js"></script> </div> </body> <script type="text/javascript"> var result=""; function jisuan(num) { if(num=="=") { document.form1.text.value=eval(result); } else if (num=="CE") // 用户按了"CE"键 { result = "0"; document.form1.text.value=eval(result); }else if (num=="Backspace") { var o = document.getElementById('text'); var v = o.value; v.length > 0 && (o.value = v.substr(0, v.length - 1)); document.form1.text.value=eval(v.length - 1); } else{ result=result+num; document.form1.text.value=result; } } </script> </html>
附上整体源代码
JAVA计算器
import java.awt.*; import java.awt.event.*; import java.math.*; import javax.swing.*; import java.awt.Color; import bsh.Interpreter; public class caculator2 extends JFrame { public caculator2() { JFrame jf = new JFrame("徐天宇的哲学计算器");//对话框标题 Container pane=jf.getContentPane();//容器 pane.setLayout(new GridLayout(2,1));//网格总体布局二行一列 JPanel j1=new JPanel();//容器 j1.setLayout(new FlowLayout()); JPanel j2=new JPanel(); j2.setLayout(new GridLayout(4,8));//界面4*8按钮 final JTextField text = new JTextField(50);//文本域 text.setFont(new Font("楷体", Font.PLAIN, 30) );//文本域字体大小 JButton button1 = new JButton("1");//添加按钮 JButton button2 = new JButton("2"); JButton button3 = new JButton("3"); JButton button4 = new JButton("4"); JButton button5 = new JButton("5"); JButton button6 = new JButton("6"); JButton button7 = new JButton("7"); JButton button8 = new JButton("8"); JButton button9 = new JButton("9"); JButton button0 = new JButton("0"); JButton buttonadd = new JButton("+"); JButton buttonc = new JButton("C"); JButton buttonsubtract = new JButton("-"); JButton buttonmul = new JButton("×"); JButton buttondelete = new JButton("delete"); JButton buttondivide = new JButton("÷"); JButton buttonequal = new JButton("="); JButton buttonnegation = new JButton("+/-"); JButton buttonbackwards = new JButton("1/x"); JButton buttondecimal = new JButton("."); JButton buttonsin = new JButton("sin(X)"); JButton buttoncos = new JButton("cos(X)"); JButton buttontan = new JButton("tan(X)"); JButton buttoncsc = new JButton("csc(X)"); JButton buttonsec = new JButton("sec(X)"); JButton buttoncot = new JButton("cot(X)"); JButton buttonpi = new JButton("π"); JButton buttonsqrt = new JButton("√X"); JButton buttonpingfang = new JButton("X²"); JButton buttonlifang = new JButton("X³"); JButton buttonlifanggen = new JButton("³√X"); JButton buttonnuaa = new JButton("nuaa.work"); j1.add(text);//将文本域加到j1容器中 j2.add(button1); j2.add(button2); j2.add(button3); j2.add(buttonadd); j2.add(buttonc); j2.add(buttonsqrt); j2.add(buttonsin); j2.add(buttoncsc); j2.add(button4); j2.add(button5); j2.add(button6); j2.add(buttonsubtract); j2.add(buttondelete); j2.add(buttonpingfang); j2.add(buttoncos); j2.add(buttonsec); j2.add(button7); j2.add(button8); j2.add(button9); j2.add(buttonmul); j2.add(buttonbackwards); j2.add(buttonlifang); j2.add(buttontan); j2.add(buttoncot); j2.add(buttonnegation); j2.add(button0); j2.add(buttondecimal); j2.add(buttondivide); j2.add(buttonpi); j2.add(buttonlifanggen); j2.add(buttonequal); j2.add(buttonnuaa); pane.add(j1); pane.add(j2); text.setBackground(Color.yellow);//设置按钮前景和背景色 button1.setForeground(Color.orange); button1.setBackground(Color.gray); button2.setForeground(Color.orange); button2.setBackground(Color.gray); button3.setForeground(Color.orange); button3.setBackground(Color.gray); button4.setForeground(Color.orange); button4.setBackground(Color.gray); button5.setForeground(Color.orange); button5.setBackground(Color.gray); button6.setForeground(Color.orange); button6.setBackground(Color.gray); button7.setForeground(Color.orange); button7.setBackground(Color.gray); button8.setForeground(Color.orange); button8.setBackground(Color.gray); button9.setForeground(Color.orange); button9.setBackground(Color.gray); button0.setForeground(Color.orange); button0.setBackground(Color.gray); buttonnegation.setForeground(Color.CYAN); buttonnegation.setBackground(Color.DARK_GRAY); buttondecimal.setForeground(Color.CYAN); buttondecimal.setBackground(Color.DARK_GRAY); buttonpi.setForeground(Color.black); buttonpi.setBackground(Color.gray); buttonadd.setForeground(Color.red); buttonadd.setBackground(Color.gray); buttonsubtract.setForeground(Color.red); buttonsubtract.setBackground(Color.gray); buttonmul.setForeground(Color.red); buttonmul.setBackground(Color.gray); buttondivide.setForeground(Color.red); buttondivide.setBackground(Color.gray); buttonc.setForeground(Color.blue); buttonc.setBackground(Color.gray); buttondelete.setForeground(Color.blue); buttondelete.setBackground(Color.gray); buttonbackwards.setForeground(Color.blue); buttonbackwards.setBackground(Color.gray); buttonpi.setForeground(Color.orange); buttonpi.setBackground(Color.gray); buttonsin.setForeground(Color.green); buttonsin.setBackground(Color.gray); buttoncos.setForeground(Color.green); buttoncos.setBackground(Color.gray); buttontan.setForeground(Color.green); buttontan.setBackground(Color.gray); buttoncsc.setForeground(Color.magenta); buttoncsc.setBackground(Color.gray); buttonsec.setForeground(Color.magenta); buttonsec.setBackground(Color.gray); buttoncot.setForeground(Color.magenta); buttoncot.setBackground(Color.gray); buttonnuaa.setForeground(Color.white); buttonnuaa.setBackground(Color.gray); buttonsqrt.setForeground(Color.pink); buttonsqrt.setBackground(Color.gray); buttonpingfang.setForeground(Color.pink); buttonpingfang.setBackground(Color.gray); buttonlifang.setForeground(Color.pink); buttonlifang.setBackground(Color.gray); buttonlifanggen.setForeground(Color.pink); buttonlifanggen.setBackground(Color.gray); buttonequal.setForeground(Color.red); buttonequal.setBackground(Color.black); button1.addActionListener(new ActionListener()//数字键输入 { public void actionPerformed(ActionEvent e) { text.setText(text.getText()+"1"); } }); button2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.setText(text.getText()+"2"); } }); button3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.setText(text.getText()+"3"); } }); button4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.setText(text.getText()+"4"); } }); button5.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.setText(text.getText()+"5"); } }); button6.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.setText(text.getText()+"6"); } }); button7.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.setText(text.getText()+"7"); } }); button8.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.setText(text.getText()+"8"); } }); button9.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.setText(text.getText()+"9"); } }); button0.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.setText(text.getText()+"0"); } }); buttonpi.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.setText(text.getText()+"3.141592653589793238462643383279"); } }); buttondecimal.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.setText(text.getText()+"."); } }); buttonadd.addActionListener(new ActionListener()//功能键输入 { public void actionPerformed(ActionEvent e) { text.setText(text.getText()+"+"); } }); buttonequal.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { Interpreter a = new Interpreter(); Object obj = a.eval(text.getText()); text.setText(obj+""); }catch(Exception e2) { e2.printStackTrace(); } } }); buttonsubtract.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.setText(text.getText()+"-"); } }); buttonmul.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.setText(text.getText()+"*"); } }); buttondivide.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.setText(text.getText()+"/"); } }); buttonbackwards.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { double s; s = Double.parseDouble(text.getText()); text.setText(1/s+""); } }); buttonnegation.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { double s; s = Double.parseDouble(text.getText()); text.setText(-s+""); } }); buttondelete.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.setText(text.getText().substring(0,text.getText().length()-1)); } }); /**/ buttonc.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.setText("0"); } }); buttonsqrt.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { double s; s = Double.parseDouble(text.getText()); text.setText(Math.sqrt(s)+""); } }); buttonpingfang.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { double s; s = Double.parseDouble(text.getText()); text.setText(s*s+""); } }); buttonlifang.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { double s; s = Double.parseDouble(text.getText()); text.setText(s*s*s+""); } }); buttonlifanggen.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { double s; s = Double.parseDouble(text.getText()); text.setText(Math.pow(s, 1.0/3.0)+""); } }); buttonsin.addActionListener(new ActionListener()//求正弦值 { public void actionPerformed(ActionEvent e) { double s; s = Double.parseDouble(text.getText()); text.setText(Math.sin(Math.PI*s/180)+""); } }); buttoncos.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { double s; s = Double.parseDouble(text.getText()); text.setText(Math.cos(Math.PI*s/180)+""); } }); buttontan.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { double s; s = Double.parseDouble(text.getText()); text.setText(Math.tan(Math.PI*s/180)+""); } }); buttoncsc.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { double s; s = Double.parseDouble(text.getText()); text.setText(1/(Math.sin(Math.PI*s/180))+""); } }); buttoncot.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { double s; s = Double.parseDouble(text.getText()); text.setText(1/(Math.tan(Math.PI*s/180))+""); } }); buttonsec.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { double s; s = Double.parseDouble(text.getText()); text.setText(1/(Math.cos(Math.PI*s/180))+""); } }); buttonnuaa.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent arg0) { try { Runtime.getRuntime().exec("cmd /c start http://nuaa.work"); } catch (Exception ex) { System.out.println("error"); ex.printStackTrace(); } } }); jf.setSize(800,300); jf.setVisible(true); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { caculator2 a = new caculator2(); a.setLocationRelativeTo(null); } }
关于作者