科学计算器java源代码 计算器程序代码java

求一个 JAVA 的 科学计算器 的 源代码科学计算器java源代码我给科学计算器java源代码你一个功能强大科学计算器java源代码的计算器!已运行通过!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator3 extends JFrame implements ActionListener {
private boolean dotExist, operated, equaled; // 帮助运算科学计算器java源代码的布尔变量
private double storedNumber; // 目前的结果
private char lastOperator; // 表示上一运算符
private JTextField operation; // 结果栏
private JButton dot, plus, minus, multi, div, sqrt, equal, changePN, clear; // 运算符
private JButton[] numbers; // 数字
// 构造者
public Calculator3() {
setTitle("Calculator");
// 初始化变量
dotExist = false; // 表示当前的数是否有小数点
operated = false; // 表示任意运算符是否被按下
equaled = false; // 表示等号是否被按下
storedNumber = 0;
lastOperator = '?';
// 初始化窗口变量
operation = new JTextField("0");
operation.setEditable(false);
numbers = new JButton[10];
for (int i = 0; i10; i)
numbers[i] = new JButton(""i);
dot = new JButton(".");
plus = new JButton(" ");
minus = new JButton("-");
multi = new JButton("*");
div = new JButton("/");
sqrt = new JButton("√");
equal = new JButton("=");
changePN = new JButton("±");
clear = new JButton("AC");
// 将窗口物体放入窗口
GridBagLayout layout = new GridBagLayout();
getContentPane().setLayout(layout);
addComponent(layout, operation, 0, 0, 4, 1);
addComponent(layout, numbers[1], 1, 0, 1, 1);
addComponent(layout, numbers[2], 1, 1, 1, 1);
addComponent(layout, numbers[3], 1, 2, 1, 1);
addComponent(layout, numbers[4], 2, 0, 1, 1);
addComponent(layout, numbers[5], 2, 1, 1, 1);
addComponent(layout, numbers[6], 2, 2, 1, 1);
addComponent(layout, numbers[7], 3, 0, 1, 1);
addComponent(layout, numbers[8], 3, 1, 1, 1);
addComponent(layout, numbers[9], 3, 2, 1, 1);
addComponent(layout, dot, 4, 0, 1, 1);
addComponent(layout, numbers[0], 4, 1, 1, 1);
addComponent(layout, sqrt, 4, 2, 1, 1);
addComponent(layout, plus, 1, 3, 1, 1);
addComponent(layout, minus, 2, 3, 1, 1);
addComponent(layout, multi, 3, 3, 1, 1);
addComponent(layout, div, 4, 3, 1, 1);
addComponent(layout, equal, 5, 0, 2, 1);
addComponent(layout, changePN, 5, 2, 1, 1);
addComponent(layout, clear, 5, 3, 1, 1);
}
// 对按钮进行反应的方法
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton) e.getSource();
if (btn == clear) {
operation.setText("0");
dotExist = false;
storedNumber = 0;
lastOperator = '?';
} else if (btn == equal) {
operate('=');
equaled = true;
} else if (btn == plus) {
operate(' ');
equaled = false;
} else if (btn == minus) {
operate('-');
equaled = false;
} else if (btn == multi) {
operate('*');
equaled = false;
} else if (btn == div) {
operate('/');
equaled = false;
} else if (btn == changePN) {
operate('p');
operate('=');
equaled = true;
} else if (btn == sqrt) {
operate('s');
operate('=');
equaled = true;
} else {
if (equaled)
storedNumber = 0;
for (int i = 0; i10; i)
if (btn == numbers[i]) {
if (operation.getText().equals("0"))
operation.setText(""i);
else if (!operated)
operation.setText(operation.getText()i);
else {
operation.setText(""i);
operated = false;
}
}
if (btn == dot!dotExist) {
operation.setText(operation.getText()".");
dotExist = true;
}
}
}
// 进行运算的方法
private void operate(char operator) {
double currentNumber = Double.valueOf(operation.getText())
.doubleValue();
if (lastOperator == '?')
storedNumber = currentNumber;
else if (lastOperator == ' ')
storedNumber= currentNumber;
else if (lastOperator == '-')
storedNumber -= currentNumber;
else if (lastOperator == '*')
storedNumber *= currentNumber;
else if (lastOperator == '/')
storedNumber /= currentNumber;
else if (lastOperator == 'p')
storedNumber *= -1;
else if (lastOperator == 's')
storedNumber = Math.sqrt(currentNumber);
else if (lastOperator == '='equaled)
storedNumber = currentNumber;
operation.setText(""storedNumber);
operated = true;
lastOperator = operator;
}
// 快捷使用GridBagLayout的方法
private void addComponent(GridBagLayout layout, Component component,
int row, int col, int width, int height) {
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(10, 2, 10, 2);
constraints.weightx = 100;
constraints.weighty = 100;
constraints.gridx = col;
constraints.gridy = row;
constraints.gridwidth = width;
constraints.gridheight = height;
layout.setConstraints(component, constraints);
if (component instanceof JButton)
((JButton) component).addActionListener(this);
getContentPane().add(component);
}
// 主方法初始化并显示窗口
public static void main(String[] args) {
Calculator3 calc = new Calculator3();
【科学计算器java源代码 计算器程序代码java】calc.setSize(290, 400);
calc.setVisible(true);
}
}
用JAVA编写的科学计算器源代码这个你参考一下 。
import javax.swing.*;
//import javax.swing.event.*;
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);//文本框不能编辑
setLayout(new BorderLayout());//设定布局管理器边框布局
add(text, BorderLayout.CENTER);//text放置在窗体的中间
}
}
//计算器数字按钮定义面板
class Number_Key extends JPanel
{
JButton zero = new JButton("0");//数字键0
JButton one = new JButton("1");//数字键1
JButton two = new JButton("2");//数字键2
JButton three = new JButton("3");//数字键3
JButton four = new JButton("4");//数字键4
JButton five = new JButton("5");//数字键5
JButton six = new JButton("6");//数字键6
JButton seven = new JButton("7");//数字键7
JButton eight = new JButton("8");//数字键8
JButton nine = new JButton("9");//数字键9
JButton plus = new JButton(" ");
JButton sub = new JButton("-");
JButton mul = new JButton("*");
JButton div = new JButton("/");
JButton equal = new JButton("=");
JButton ce = new JButton("ce");//置零键
JButton point = new JButton(".");
JButton tzero = new JButton("00");
Number_Key()
{
setLayout(new GridLayout(6, 3, 10, 10));//定义布局管理器为网格布局
//添加各个按钮键
add(seven);
add(eight);
add(nine);
add(four);
add(five);
add(six);
add(one);
add(two);
add(three);
add(zero);
add(tzero);
add(plus);
add(sub);
add(mul);
add(div);
add(point);
add(equal);
add(ce);
}
}
//计算器主类
class sakura extends JFrame implements ActionListener
{
Result result = new Result();//定义text的面板
Number_Key number_key = new Number_Key();//定义按钮面板
//当点击按钮 、-、*、/时科学计算器java源代码,com = true
boolean com = false;
//当i=0时说明是我们第一次输入科学计算器java源代码 , 字符串sum不会累加
int i = 0;
//存放text的内容
String sum = "";
//存放点击按钮 、-、*、/之前的数值
double total = 0;
// 、-、*、/的代号分别为1科学计算器java源代码 , 2,3,4
int symbol = 0;
sakura()
{
super("Calculator");//设定标题
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设定关闭窗体时退出程序
JPanel pane = new JPanel();//定义主面板
pane.setLayout(new BorderLayout());
pane.add(result, BorderLayout.NORTH);
pane.add(number_key, BorderLayout.SOUTH);
number_key.one.addActionListener(this);//对1按钮添加监听事件
number_key.two.addActionListener(this);//对2按钮添加监听事件
number_key.three.addActionListener(this);//对3按钮添加监听事件
number_key.four.addActionListener(this);//对4按钮添加监听事件
number_key.five.addActionListener(this);//对5按钮添加监听事件
number_key.six.addActionListener(this);//对6按钮添加监听事件
number_key.seven.addActionListener(this);//对7按钮添加监听事件
number_key.eight.addActionListener(this);//对8按钮添加监听事件
number_key.nine.addActionListener(this);//对9按钮添加监听事件
number_key.zero.addActionListener(this);//对0按钮添加监听事件
number_key.ce.addActionListener(this);//对置零按钮添加监听事件
number_key.plus.addActionListener(this);//对 按钮添加监听事件
number_key.equal.addActionListener(this);//对=按钮添加监听事件
number_key.sub.addActionListener(this);//对-按钮添加监听事件
number_key.mul.addActionListener(this);//对*按钮添加监听事件
number_key.div.addActionListener(this);//对/按钮添加监听事件
number_key.tzero.addActionListener(this);//对00按钮添加监听事件
number_key.point.addActionListener(this);//对.按钮添加监听事件
setContentPane(pane);
pack();//初始化窗体大小为正好盛放所有按钮
}
//各个按钮触发的事件
public void actionPerformed(ActionEvent e) {
/*如果是点击数字按钮那么先要判断是否在此之前点击了 、-、*、/、=,如果是那么com=true
* 如果没有com= false;或者是否点击数字键,如果是i = 1,如果没有 i = 0;
**/
if (e.getSource() == number_key.one)
{
if (com || i == 0)
{
result.text.setText("1");
com = false;
i = 1;
}
else
{
sum = result.text.getText();
result.text.setText(sum"1");
}
}
else if (e.getSource() == number_key.two)
{
if (com || i == 0)
{
result.text.setText("2");
com = false;
i = 1;
}
else
{
sum = result.text.getText();
result.text.setText(sum"2");
}
}
else if (e.getSource() == number_key.three)
{
if (com || i == 0)
{
result.text.setText("3");
com = false;
i = 1;
}
else
{
sum = result.text.getText();
result.text.setText(sum"3");
}
}
else if (e.getSource() == number_key.four)
{
if (com || i == 0)
{
result.text.setText("4");
com = false;
i = 1;
}
else
{
sum = result.text.getText();
result.text.setText(sum"4");
}
}
else if (e.getSource() == number_key.five)
{
if (com || i == 0)
{
result.text.setText("5");
com = false;
i = 1;
}
else
{
sum = result.text.getText();
result.text.setText(sum"5");
}
}
else if (e.getSource() == number_key.six)
{
if (com || i == 0)
{
result.text.setText("6");
com = false;
i = 1;
}
else
{
sum = result.text.getText();
result.text.setText(sum"6");
}
}
else if (e.getSource() == number_key.seven)
{
if (com || i == 0)
{
result.text.setText("7");
com = false;
i = 1;
}
else
{
sum = result.text.getText();
result.text.setText(sum"7");
}
}
else if (e.getSource() == number_key.eight)
{
if (com || i == 0)
{
result.text.setText("8");
com = false;
i = 1;
}
else
{
sum = result.text.getText();
result.text.setText(sum"8");
}
}
else if (e.getSource() == number_key.nine)
{
if (com || i == 0)
{
result.text.setText("9");
com = false;
i = 1;
}
else
{
sum = result.text.getText();
result.text.setText(sum"9");
}
}
/*对于0这个按钮有一定的说法,在我的程序里不会出现如00000这样的情况,我加了判断条件就是
* 如果text中的数值=0就要判断在这个数值中是否有.存在?如果有那么就在原来数值基础之上添
* 加0;否则保持原来的数值不变
*/
else if (e.getSource() == number_key.zero)
{
if (com || i == 0)
{
result.text.setText("0");
com = false;
i = 1;
}
else
{
sum = result.text.getText();
if (Float.parseFloat(sum)0 || Float.parseFloat(sum)0)
{
result.text.setText(sum"0");
}
else
{
if (sum.trim().indexOf(".") == -1)
{
result.text.setText(sum);
}
else
{
result.text.setText(sum"0");
}
}
}
}
else if (e.getSource() == number_key.ce)
{
result.text.setText("0");
i = 0;
com = true;
}
else if (e.getSource() == number_key.tzero)
{
if (com || i == 0)
{
result.text.setText("0");
com = false;
i = 1;
}
else
{
sum = result.text.getText();
if (Float.parseFloat(sum)0 || Float.parseFloat(sum)0)
{
result.text.setText(sum"00");
}
else
{
if (sum.trim().indexOf(".") == -1)
{
result.text.setText(sum);
}
else
{
result.text.setText(sum"00");
}
}
}
}
/*本程序不会让一个数值中出现2个以上的小数点.具体做法是:判断是否已经存在.存在就不添加,
* 不存在就添加.
*/
else if (e.getSource() == number_key.point)
{
if (com || i == 0)
{
result.text.setText("0.");
com = false;
i = 1;
}
else
{
sum = result.text.getText();
if (sum.trim().indexOf(".") == -1)
{
result.text.setText(sum".");
}
else
{
result.text.setText(sum);
}
}
}
//获得点击 之前的数值
else if (e.getSource() == number_key.plus)
{
com = true;
i = 0;
total = Double.parseDouble(result.text.getText());
symbol = 1;
}//获得点击-之前的数值
else if (e.getSource() == number_key.sub)
{
com = true;
i = 0;
total = Double.parseDouble(result.text.getText());
symbol = 2;
}//获得点击*之前的数值
else if (e.getSource() == number_key.mul)
{
com = true;
i = 0;
total = Double.parseDouble(result.text.getText());
System.out.println(total);
symbol = 3;
}//获得点击/之前的数值
else if (e.getSource() == number_key.div)
{
com = true;
i = 0;
total = Double.parseDouble(result.text.getText());
symbol = 4;
}
else if (e.getSource() == number_key.equal)
{
switch (symbol)
{
case 1 ://计算加法
{
double ad =
totalDouble.parseDouble(result.text.getText());
result.text.setText(ad"");
i = 0;
sum = "";
break;
}
case 2 ://计算减法
{
double ad =
total - Double.parseDouble(result.text.getText());
result.text.setText(String.valueOf(ad));
i = 0;
sum = "";
break;
}
case 3 ://计算乘法
{
double ad =
total * Double.parseDouble(result.text.getText());
result.text.setText(ad"");
i = 0;
sum = "";
break;
}
case 4 ://计算除法
{
double ad =
total / Double.parseDouble(result.text.getText());
result.text.setText(ad"");
i = 0;
sum = "";
break;
}
}
System.out.println(com);
}
}
public static void main(String[] args)
{
sakura ww = new sakura();
ww.setVisible(true) ;
}
}
求一个 JAVA 的 科学计算器 的 源代码最好里面有 sin cos 倒数、平方根、平方、立方高级一点的!import javax.swing.*;
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();
tf.setHorizontalAlignment(JTextField.RIGHT);//文本框
c.add(tf,"North");
JPanel pn3 = new JPanel(new BorderLayout());
c.add(pn3,"Center");
JPanel pn2 = new JPanel();//功能键界面(清除键和关闭键)
pn2.setLayout(new BorderLayout());
JPanel pn1 = new JPanel();//运算界面
pn1.setLayout(new GridLayout(4,4));
pn3.add(pn2,"North");
pn3.add(pn1);
//设置按钮
JButton b = new JButton("CLEAR");
b.setToolTipText("请按清除键!");//设置清零键
b.setForeground(Color.RED);//设置字体颜色
b.setBackground(Color.YELLOW);//设置背景色
b.addActionListener(this);
pn2.add(b,"Center");
b = new JButton("OFF");
b.setToolTipText("请按退出键!");//设置off键,点击退出应用程序b.addActionListener(this);
b.setForeground(Color.RED);//字体颜色
b.setBackground(Color.ORANGE);//背景色
pn2.add(b,"East");
b = new JButton("1");//add butten 1
b.addActionListener(this);
pn1.add(b);
b = new JButton("2");//add butten 2
b.addActionListener(this);
pn1.add(b);
b = new JButton("3");//add butten 3
b.addActionListener(this);
pn1.add(b);
b = new JButton(" ");//add butten
b.setForeground(Color.BLUE);//设置字体颜色
b.addActionListener(this);
pn1.add(b);
b = new JButton("4");//add butten 4
b.addActionListener(this);
pn1.add(b);
b = new JButton("5");//add butten 5
b.addActionListener(this);
pn1.add(b);
b = new JButton("6");//add button 6
b.addActionListener(this);
pn1.add(b);
b = new JButton("-");//add button -
b.setForeground(Color.BLUE);//设置字体颜色
b.addActionListener(this);
pn1.add(b);
b = new JButton("7");//add button 7
b.addActionListener(this);
pn1.add(b);
b = new JButton("8");//add button 8
b.addActionListener(this);
pn1.add(b);
b = new JButton("9");//add button 9
b.addActionListener(this);
pn1.add(b);
b = new JButton("*");//add button *
b.setForeground(Color.BLUE);//设置字体颜色
b.addActionListener(this);
pn1.add(b);
b = new JButton("0");//add button 0
b.addActionListener(this);
pn1.add(b);
b = new JButton(".");//add button .
b.addActionListener(this);
pn1.add(b);
b = new JButton("=");//add button =
b.setForeground(Color.RED);//设置字体颜色
b.addActionListener(this);
pn1.add(b);
b = new JButton("\\");//add button \
b.setForeground(Color.BLUE);//设置字体颜色
b.addActionListener(this);
pn1.add(b);
jf.setSize(300,300);//设置大小
jf.setVisible(true);//设置为可视
}
//处理按钮按下时的动作,进行相应的处理
public void actionPerformed(ActionEvent e)
{
String command = e.getActionCommand();
tf.setText(tf.getText() command);
if(command.equals("CLEAR")) //清零键 按下时返回初始状态
{
s1=null;
s="";
tf.setText("");//记录输入值的变量清空
}
else if(command.equals("OFF")) System.exit(0);//off键 关闭应用程序
else if(!command.equals("*")!command.equals("\\")
!command.equals(" ")!command.equals("-")
!command.equals("="))//判断输入是否为数字
{
if(s1==null)//判断输入是否为第一个
s1 = command;
else s1 =command;
d1 = new Double(s1).doubleValue();//字符串型转换为双精度型,还原输入数字
try
{
if(s.equals(" ")) d1 = d1 d2;//加法运算
else if(s.equals("-")) d1 = d2-d1;//减法运算
else if(s.equals("*")) d1 = d1*d2;//乘法运算
else if(s.equals("\\"))d1 = d2/d1;//除法运算
}
catch(Exception ex)
{
tf.setText("Error");//错误显示"Error"
System.out.println(ex.getMessage());
}
}
else if(!command.equals("=")) //判断输入是否为- * \
{
s = command;
s1 = null;
d2 = d1;
}
else//输入=时,显示运算结果
{
tf.setText(tf.getText() d1);
}
}
public static void main(String [] args)
{
new Calculator().init();
}
}
关于科学计算器java源代码和计算器程序代码java的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读