package jisuanqi;
import java.awt.*;
import java.awt.MenuBar;
import javax.swing.*;
import java.awt.event.*;
//主类
class jsq
extends Frame {
//声明
double op, pa1, pa2, pa3;
String op1, op2, ll, s;
boolean isResult = false;
//设置标题
Frame p=new Frame("计算器");
//创建按钮
Panel p0 = new Panel();
Panel p1 = new Panel();
Panel p2 = new Panel();
Panel p3 = new Panel();
Panel p4 = new Panel();
Panel p5 = new Panel();
Panel p6 = new Panel();
Panel p7 = new Panel();
// 创建文本框
JTextField t = new JTextField("0");
TextArea te = new TextArea();
//初始化按钮
Button b1 = new Button("Backspace");
Button b2 = new Button("CE");
Button b3 = new Button("sin");
Button b4 = new Button("MC");
Button b5 = new Button("7");
Button b6 = new Button("8");
Button b7 = new Button("9");
Button b8 = new Button("/");
Button b9 = new Button("sqrt");
Button b10 = new Button("MR");
Button b11 = new Button("4");
Button b12 = new Button("5");
Button b13 = new Button("6");
Button b14 = new Button("*");
Button b15 = new Button("%");
Button b16 = new Button("MS");
Button b17 = new Button("1");
Button b18 = new Button("2");
Button b19 = new Button("3");
Button b20 = new Button("-");
Button b21 = new Button("1/x");
Button b22 = new Button("M+");
Button b23 = new Button("0");
Button b24 = new Button("+/-");
Button b25 = new Button(".");
Button b26 = new Button("+");
Button b27 = new Button("=");
Button ba = new Button("exit");
Button a1 = new Button("n!");
Button a2 = new Button("cos");
Button a3 = new Button("tan");
Button a4 = new Button("cot");
//创建JMenuBar
MenuBar mbar = new MenuBar();
//创建Menu
Menu menu1 = new Menu("File");
Menu menu2 = new Menu("help");
Menu menu3 = new Menu("Edit");
//创建MenuItem
MenuItem me1 = new MenuItem("copy");
MenuItem me2 = new MenuItem("paste");
MenuItem me3 = new MenuItem("about");
MenuItem me4 = new MenuItem("open");
MenuItem me5 = new MenuItem("source");
public void jbInit() {
t.setHorizontalAlignment(JTextField.RIGHT);
//添加菜单项
this.setMenuBar(mbar);
mbar.add(menu1);
mbar.add(menu2);
mbar.add(menu3);
menu1.add(me1);
menu1.addSeparator();
menu1.add(me2);
menu1.addSeparator();
menu1.add(me4);
menu2.add(me3);
menu3.add(me5);
//为me1创建快捷键
me1.setShortcut(new MenuShortcut(67));
me1.setActionCommand("copy");
me2.setShortcut(new MenuShortcut(86));
me2.setActionCommand("paste");
//添加画板"
this.add(p0);
//为画板p0设置 布局
p0.setLayout(new GridLayout(9, 6, 10, 5));
p0.setBackground(Color.lightGray);
p0.add(p1);
//为画板p1设置 布局
p1.setLayout(new BorderLayout());
p1.setBackground(Color.lightGray);
p1.add(t);
//为画板p7设置 布局
p7.setLayout(new GridLayout(1, 4, 5, 5));
p7.setBackground(Color.lightGray);
//向画板P7中添加按钮
p0.add(p7);
p7.add(a1);
p7.add(a2);
p7.add(a3);
p7.add(a4);
p0.add(p2);
//为画板p0设置 布局
p2.setLayout(new GridLayout(1, 4, 5, 5));
p2.setBackground(Color.lightGray);
//向画板P2中添加按钮
p2.add(ba);
p2.add(b1);
p2.add(b2);
p2.add(b3);
p0.add(p3);
//为画板p0设置 布局
p3.setLayout(new GridLayout(1, 6, 5, 5));
p3.setBackground(Color.lightGray);
//向画板P3中添加按钮
p3.add(b4);
p3.add(b5);
p3.add(b6);
p3.add(b7);
p3.add(b8);
p3.add(b9);
p0.add(p4);
//为画板p0设置 布局
p4.setLayout(new GridLayout(1, 6, 5, 5));
p4.setBackground(Color.lightGray);
//向画板P4中添加按钮
p4.add(b10);
p4.add(b11);
p4.add(b12);
p4.add(b13);
p4.add(b14);
p4.add(b15);
p0.add(p5);
//为画板p0设置 布局
p5.setLayout(new GridLayout(1, 6, 5, 5));
p5.setBackground(Color.lightGray);
//向画板P5中添加按钮
p5.add(b16);
p5.add(b17);
p5.add(b18);
p5.add(b19);
p5.add(b20);
p5.add(b21);
p0.add(p6);
//为画板p0设置 布局
p6.setLayout(new GridLayout(1, 6, 5, 5));
p6.setBackground(Color.lightGray);
//向画板P6中添加按钮
p6.add(b22);
p6.add(b23);
p6.add(b24);
p6.add(b25);
p6.add(b26);
p6.add(b27);
//f.add(pp);
// f.add(te);
//调整大小并显示
this.pack();
this.setVisible(true);
t.setEditable(false);
t.setBackground(Color.white);
// 向按钮中增加监听器
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String m = t.getText();
t.setText("0");
}
});
b23.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ll = t.getText();
if (ll.equals("0")) {
}
else {
t.setText(t.getText() + 0);
}
}
});
b17.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ll = t.getText();
if (ll.equals("0")) {
t.setText("1");
}
else {
t.setText(t.getText() + 1);
}
}
});
b18.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ll = t.getText();
if (ll.equals("0")) {
t.setText("2");
}
else {
t.setText(t.getText() + 2);
}
}
});
b19.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ll = t.getText();
if (ll.equals("0")) {
t.setText("3");
}
else {
t.setText(t.getText() + 3);
}
}
});
b11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ll = t.getText();
if (ll.equals("0")) {
t.setText("4");
}
else {
t.setText(t.getText() + 4);
}
}
});
b12.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ll = t.getText();
if (ll.equals("0")) {
t.setText("5");
}
else {
t.setText(t.getText() + 5);
}
}
});
b13.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ll = t.getText();
if (ll.equals("0")) {
t.setText("6");
}
else {
t.setText(t.getText() + 6);
}
}
});
b5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ll = t.getText();
if (ll.equals("0")) {
t.setText("7");
}
else {
t.setText(t.getText() + 7);
}
}
});
b6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ll = t.getText();
if (ll.equals("0")) {
t.setText("8");
}
else {
t.setText(t.getText() + 8);
}
}
});
b7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ll = t.getText();
if (ll.equals("0")) {
t.setText("9");
}
else {
t.setText(t.getText() + 9);
}
}
});
ba.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0); //设置退出按钮
}
});
b25.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s11 = t.getText();
if (s11.indexOf(".") < 0) {
s11 = s11 + ".";
t.setText(s11);
}
}
});
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String string=e.getActionCommand() ;
String mm = t.getText();
if(e.equals("弧度") ){
double nn = Double.parseDouble(mm);
nn *= Math.PI / 180;
nn = Math.sin(nn);
nn = nn * 10000;
nn = Math.rint(nn);
nn = nn / 10000;
t.setText(" " + nn);
}
else{
double num=Double.parseDouble(mm) ;
t.setText(String.valueOf(Math.sin(num) ) ) ;
}
}
});
//为sin添加监听器
a2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String mm = t.getText();
double nn = Double.parseDouble(mm); //小数化整
nn *= Math.PI / 180; //角度变为弧度
try {
nn = Math.cos(nn);
}
catch ( NumberFormatException ex ) {
}
nn = nn * 10000;
nn = Math.rint(nn);
nn = nn / 10000;
t.setText(" " + nn);
}
});
a3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String mm = t.getText();
double nn = Double.parseDouble(mm); //小数化整
nn *= Math.PI / 180; //角度变为弧度
nn = Math.tan(nn);
try {
nn = nn * 10000;
}
catch ( NumberFormatException ex) {
}
nn = Math.rint(nn);
nn = nn / 10000;
t.setText(" " + nn);
}
});
b26.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
op1 = t.getText();
t.setText(" ");
op = '+'; //设置加号
}
});
b20.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
op1 = t.getText();
t.setText(" ");
op = '-'; //设置减号
}
});
b14.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
op1 = t.getText();
t.setText(" ");
op = '*'; //设置乘号
}
});
b8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
op1 = t.getText();
t.setText(" ");
op = '/'; //设置除号
}
});
//设置清除按钮
b8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
op1 = t.getText();
t.setText(" ");
// op='sin';
}
});
//设置等号按钮
b27.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
op2 = t.getText();
pa1 = Double.parseDouble(op1); //字弧串变为双精度数
pa2 = Double.parseDouble(op2); //字弧串变为双精度数
//double pa3 =0;
if (op == '+')
pa3 = pa1 + pa2;
if (op == '-')
pa3 = pa1 - pa2;
if (op == '*')
pa3 = pa1 * pa2;
if (op == '/')
pa3 = pa1 / pa2;
t.setText(" " + pa3);
}
});
//增加菜单监听器
me1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str = t.getText();
}
});
me2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
t.setText(str);
}
});
me3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialog u=new dialog();
u.setSize(300,200) ;
u.show() ;
//t.setText(" ");
//t.setText(t.getText() + "这是我的计算器,我爱JAVA .");
}
});
me4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
/*www u=new www();
u.pack() ;
u.show() ;*/
}
}) ;
b21.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
double len =Double.parseDouble(t.getText());
len = (1 / len);
if (t.getText() != null) {
t.setText("" + len);
}
}
catch (NumberFormatException ex) {
}
}
}) ;
b24.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
int len = Integer.parseInt(t.getText());
len = ~len + 1;
if (t.getText() != null) {
t.setText("" + len);
}
}
catch (NumberFormatException ex) {
System.out .print("error") ;
}
}
}) ;
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String ss=t.getText() ;
int len=ss.length();
if(ss!=null){
t.setText(ss.substring(0,len-1)) ;
if(len==1){
t.setText("0");
}
}
b1_actionPerformed(e);
}
}) ;
me5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialog1 m=new dialog1();
m.setSize(300,300) ;
m.show() ;
}
}) ;
b9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
double oo=Double.parseDouble(t.getText() ) ;
t.setText(" ") ;
t.setText(String.valueOf(-Math.sqrt(oo) ) ) ;
t.setText(String.valueOf( Math.sqrt(oo) ) ) ;
}
});
t.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if(e.getClickCount() ==2){
System.exit(0) ;
}
}
});
//增加窗口监听器
this.addWindowListener(new WindowAdapter( ) {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//添加阶乘监听器
a1.addActionListener(new ActionListener() {
// try {
public void actionPerformed(ActionEvent e) {
int mull = 1;
int x = Integer.parseInt(t.getText()); //小数化整
t.setText(" ");
for (int i = 1; i <= x; i++)
mull = mull * i;
t.setText(String.valueOf(mull));
}
});
}
String str;
public jsq() {
jbInit();
}
public static void main(String[] args) {
jsq u = new jsq(); //实例化a
}
void b1_actionPerformed(ActionEvent e) {
}
}
class dialog extends JFrame{
JLabel jlabel=new JLabel();
JPanel jpanel=new JPanel();
dialog(){
this.getContentPane().add(jpanel) ;
jpanel.setLayout(new BorderLayout( )) ;
jpanel.add(jlabel) ;
jlabel.setText("本计算机的版权归个人所有!!" +
" 任何公司和个人不得侵权!!!!!") ;
}
}
class dialog1 extends Frame{
ScrollPane scrollpane=new ScrollPane();
TextArea textarea=new TextArea ();
dialog1(){
this.add(scrollpane) ;
scrollpane.add(textarea) ;
textarea.setText(" ") ;
}
}
这个绝对能用的 是我以前写的作业
这些自己都可以搜索到的,下面是我找到的3个
http://zhidao.baidu.com/question/2471672.html?si=8
http://zhidao.baidu.com/question/2076628.html?si=7
http://zhidao.baidu.com/question/8556886.html?si=5
急求java科学计算器源代码
悬赏分:20 - 解决时间:2006-3-18 10:11
编写一个功能较全面的科学计算器,该计算器不仅能进行加、减、乘、除等混合运算,而且能计算sin、cos、tan、log等函数的值,还要具有清零、退格、求倒数、求相反数等功能。(绻�茉诵谐晒Φ幕白芳?0分啊)
提问者: ssitfkcn - 试用期 一级
最佳答案
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Counter extends WindowAdapter
{
static JFrame f=new JFrame("计算器");
static JTextField text1=new JTextField("0.");
static String source="";
static String cal="";
static String object="";
static boolean flag=false;
static boolean flag1=true;
static boolean flag2=false;
public void init()
{
try
{
Container c=f.getContentPane();
JPanel pan1=new JPanel();
JButton b1=new JButton("1");
JButton b2=new JButton("2");
JButton b3=new JButton("3");
JButton b4=new JButton("4");
JButton b5=new JButton("5");
JButton b6=new JButton("6");
JButton b7=new JButton("7");
JButton b8=new JButton("8");
JButton b9=new JButton("9");
JButton b0=new JButton("0");
JButton b11=new JButton("+");
JButton b12=new JButton("-");
JButton b13=new JButton("*");
JButton b14=new JButton("/");
JButton b15=new JButton(".");
JButton b16=new JButton("=");
JButton bclar=new JButton("清零");
text1.setHorizontalAlignment(JTextField.RIGHT);
c.add(text1,"North");
c.add(pan1);
A aa=new A();
Result re=new Result();
Opertion op=new Opertion();
Clar cl=new Clar();
b1.addActionListener(aa);
b2.addActionListener(aa);
b3.addActionListener(aa);
b4.addActionListener(aa);
b5.addActionListener(aa);
b6.addActionListener(aa);
b7.addActionListener(aa);
b8.addActionListener(aa);
b9.addActionListener(aa);
b0.addActionListener(aa);
b11.addActionListener(op);
b12.addActionListener(op);
b13.addActionListener(op);
b14.addActionListener(op);
b16.addActionListener(re);
b15.addActionListener(aa);
bclar.addActionListener(cl);
pan1.add(b1);
pan1.add(b2);
pan1.add(b3);
pan1.add(b11);
pan1.add(b4);
pan1.add(b5);
pan1.add(b6);
pan1.add(b12);
pan1.add(b7);
pan1.add(b8);
pan1.add(b9);
pan1.add(b13);
pan1.add(b0);
pan1.add(b15);
pan1.add(b16);
pan1.add(b14);
pan1.add(bclar);
f.setSize(200,220);
f.setVisible(true);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
class A implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String a=text1.getText();
String s=e.getActionCommand();
if(a.equals("0.")||a.equals("+")||a.equals("-")||a.equals("*")||a.equals("/"))
text1.setText(s);
else {
if(flag2)
{
text1.setText(s);
flag2=false;
}
else
text1.setText(a+s);
}
}
}
class Opertion implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
cal=e.getActionCommand();
if(flag1==true)
source=text1.getText();
text1.setText(cal);
flag1=false;
flag=true;
}
}
class Result implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double num1;
num1=Double.parseDouble(source);
object=text1.getText();
double num2;
num2=Double.parseDouble(object);
double result=0;
if(cal.equals("+"))
result=num1+num2;
if(cal.equals("-"))
result=num1-num2;
if(cal.equals("*"))
result=num1*num2;
if(cal.equals("/"))
if(num2==0)
text1.setText("除数不能为0");
else
result=num1/num2;
String s1=Double.toString(result);
text1.setText(s1);
flag1=true;
flag2=true;
}
}
class Clar implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
text1.setText("0.");
}
}
public static void main(String[] args)
{
Counter count=new Counter();
count.init();
}
public void windowClosing(WindowEvent e){
System.exit(1);
}
public void windowOpened(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
}
给的分太少