用java编写科学计算器的代码!一定要有求立方

如题所述

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){}
}
温馨提示:内容为网友见解,仅供参考
无其他回答

用java编写科学计算器的代码!一定要有求立方
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=fal...

用JAVA编写的科学计算器源代码
以下是一个简单的用Java编写的科学计算器的源代码示例:java import java.util.Scanner;public class ScientificCalculator { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);System.out.println("Welcome to the Scientific Calculator!");System.out.println("Ente...

用JAVA编写的科学计算器源代码
import java.awt.*; import java.awt.event.*; \/\/计算器显示结果的窗体 class Result extends JPanel { JTextField text = new JTextField("0"); \/\/text先是输入和结果 Result() { text.setHorizontalAlignment(SwingConstants.RIGHT); \/\/讲输入的数字或得到的结果在text的右边显示 text.setEnabled(false...

JAVA科学计算器代码如何编
import java.awt.*;import java.awt.event.*;import java.lang.*;import javax.swing.*;public class Counter extends Frame { \/\/声明三个面板的布局 GridLayout gl1,gl2,gl3;Panel p0,p1,p2,p3;JTextField tf1;TextField tf2;Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b...

求JAVA编写的 简易计算器程序,附上注释
import java.awt.*;import java.awt.event.*;public class Calculator implements ActionListener { String s="",s1;double d1,d2;JFrame jf = new JFrame("小计算器by Graduate") ;JTextField tf = new JTextField();public void init()\/\/实现计算器界面 { Container c=jf.getContentPane();...

java如何编写计算器代码
import java.awt.event.*;public class Calculator extends JFrame implements ActionListener { private boolean dotExist, operated, equaled; \/\/ 帮助运算的布尔变量 private double storedNumber; \/\/ 目前的结果 private char lastOperator; \/\/ 表示上一运算符 private JTextField operation; \/\/ 结果栏...

用Java编写计算器
import java.util.regex.*;public class Calculation { public static void main(String[] args) throws Exception { \/\/ TODO Auto-generated method stub if (args.length != 3) { System.out.println("MyCal need 3 parameters the formate is:");System.out.println("MyCal num1 operate num2...

java编一个计算器的代码
代码如下:import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.util.*;public class Counter { public static void main(String[] args) { CounterFrame frame = new CounterFrame();frame.show();} } class CounterFrame extends JFrame { public CounterFrame() { ...

求一个java计算器原代码,最好有文档说明。
import java.awt.*;import java.awt.event.*;public class Calculator extends WindowAdapter implements ActionListener { private double result=0,data1=0,radixPointDepth=1;private boolean radixPointIndicate=false,resultIndicate=false;private char prec='+';private Frame f;private TextField tf;...

求JAVA计算器源代码!!!
f=new JFrame("计算器");p=new Panel();\/\/运算界面p p.setLayout(new GridLayout(4,4)); \/\/ 设置p的布局为GridLayout,四行四列 tf=new TextField(30);\/\/实例化按钮 b1=new Button("7");b2=new Button("8");b3=new Button("9");b4=new Button("+");b5=new Button("4");b6...

相似回答