java计算器课设

头像
艰难之旅   2021-09-18   5578浏览
已有145条回答
头像
bluebirdtang
2022-12-12

我有,给你一个
import java.awt.*;
import java.awt.event.*;
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;
private Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17;
private Panel p;
static public void main(String args[])
{

Calculator de=new Calculator();
de.go();
}
public void go()
{
f=new Frame("计算器");
p=new Panel();
p.setLayout(new GridLayout(4,4));
tf=new TextField(30);

b1=new Button("7");
b2=new Button("8");
b3=new Button("9");
b4=new Button("+");
b5=new Button("4");
b6=new Button("5");
b7=new Button("6");
b8=new Button("-");
b9=new Button("1");
b10=new Button("2");
b11=new Button("3");
b12=new Button("*");
b13=new Button("0");
b14=new Button(".");
b15=new Button("=");
b16=new Button("/");
b17=new Button("清零");
f.add(tf,"North");
f.add(p,"Center");
f.add(b17,"South");

p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
p.add(b5);
p.add(b6);
p.add(b7);
p.add(b8);
p.add(b9);
p.add(b10);
p.add(b11);
p.add(b12);
p.add(b13);
p.add(b14);
p.add(b15);
p.add(b16);

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
b17.addActionListener(this);
f.addWindowListener(this);
f.setSize(250,190);
f.setVisible(true);
}

public void actionPerformed(ActionEvent e)
{
String s;
s=e.getActionCommand();

switch(s.charAt(0))
{
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': //按了“0-9”,就执行下面
if(resultIndicate)
{
result=0;
data1=0;
prec='+';
}
Integer Int1=new Integer(s);

if(radixPointIndicate)
{
radixPointDepth=radixPointDepth/10;
data1=data1+(Int1.intValue())*radixPointDepth;
}
else
{
data1=data1*10+(Int1.intValue());
}
Double displayNumber=new Double(data1);
tf.setText(displayNumber.toString());
resultIndicate=false;
break;
case '+':
case '-':
case '*':
case '/':
case '=':

if(s.charAt(0)!='='&&resultIndicate)
{
prec=s.charAt(0);
resultIndicate=false;
}
else
{

switch(prec)
{
case '+':
result=result+data1;
break;
case '-':
result=result-data1;
break;
case '*':
result=result*data1;
break;
case '/':
result=result/data1;
break;
}
}
radixPointIndicate=false;
radixPointDepth=1;
displayNumber=new Double(result);
tf.setText(displayNumber.toString());

if(s.charAt(0)!='=')
{
data1=0;
prec=s.charAt(0);
}
else
{
resultIndicate=true;
}
break;
case '.':
radixPointIndicate=true;
break;
}

if(s.equals("清零"))
{
result=0;
data1=0;
radixPointDepth=1;
radixPointIndicate=false;
tf.setText("0.0");
}
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}

给分!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

127

头像
babycarolyn
2022-07-04

package com.xuzs.test;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Calculator extends JFrame implements ActionListener {

private static final long serialVersionUID = 1L;
private JTextField txtResult;
private JButton btnZero, btnOne, btnTwo, btnThree, btnFour, btnFive,
btnSix, btnSeven, btnEight, btnNine, btnPlus, btnMinus, btnTimes,
btnDivided, btnEqual, btnPoint, btnC, btnCE, btnSqrt, btnPlusMinus;
int z;
double x, y;
StringBuffer str;

public Calculator() {
super("计算器");
this.setSize(311, 231);
this.setLocation(300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setLayout(new GridLayout(1, 1));// 网格布局
JPanel panel = new JPanel(new GridLayout(6, 1));// 面板 网格布局6行1列
this.add(panel);

txtResult = new JTextField("0");
Color BackColor = new Color(255, 255, 255);
Color ForeColor = new Color(0, 0, 0);
txtResult.setBackground(BackColor);
txtResult.setForeground(ForeColor);

panel.add(txtResult);
txtResult.setHorizontalAlignment(JTextField.RIGHT);
txtResult.setEnabled(false);
// text.setEnabled(true);

JPanel panel_1 = new JPanel(new GridLayout(1, 4));
panel.add(panel_1);

btnSqrt = new JButton("sqrt");
panel_1.add(btnSqrt);
btnSqrt.addActionListener(this);
btnPlusMinus = new JButton("+/-");
panel_1.add(btnPlusMinus);
btnPlusMinus.addActionListener(this);
btnCE = new JButton("CE");
panel_1.add(btnCE);
btnCE.addActionListener(this);
btnC = new JButton("C");
panel_1.add(btnC);
btnC.addActionListener(this);

JPanel panel_2 = new JPanel(new GridLayout(1, 4));
panel.add(panel_2);

btnSeven = new JButton("7");
panel_2.add(btnSeven);
btnSeven.addActionListener(this);
btnEight = new JButton("8");
panel_2.add(btnEight);
btnEight.addActionListener(this);
btnNine = new JButton("9");
panel_2.add(btnNine);
btnNine.addActionListener(this);
btnDivided = new JButton("/");
panel_2.add(btnDivided);
btnDivided.addActionListener(this);

JPanel panel_3 = new JPanel(new GridLayout(1, 4));
panel.add(panel_3);

btnFour = new JButton("4");
panel_3.add(btnFour);
btnFour.addActionListener(this);
btnFive = new JButton("5");
panel_3.add(btnFive);
btnFive.addActionListener(this);
btnSix = new JButton("6");
panel_3.add(btnSix);
btnSix.addActionListener(this);
btnTimes = new JButton("*");
panel_3.add(btnTimes);
btnTimes.addActionListener(this);

JPanel panel_4 = new JPanel(new GridLayout(1, 4));
panel.add(panel_4);

btnOne = new JButton("1");
panel_4.add(btnOne);
btnOne.addActionListener(this);
btnTwo = new JButton("2");
panel_4.add(btnTwo);
btnTwo.addActionListener(this);
btnThree = new JButton("3");
panel_4.add(btnThree);
btnThree.addActionListener(this);
btnMinus = new JButton("-");
panel_4.add(btnMinus);
btnMinus.addActionListener(this);

JPanel panel_5 = new JPanel(new GridLayout(1, 4));
panel.add(panel_5);

btnZero = new JButton("0");
panel_5.add(btnZero);
btnZero.addActionListener(this);
btnPoint = new JButton(".");
panel_5.add(btnPoint);
btnPoint.addActionListener(this);
btnEqual = new JButton("=");
panel_5.add(btnEqual);
btnEqual.addActionListener(this);
btnPlus = new JButton("+");
panel_5.add(btnPlus);
btnPlus.addActionListener(this);

str = new StringBuffer();

this.setVisible(true);

}

public void windowClosing(WindowEvent a) {
System.exit(0);
}

public void actionPerformed(ActionEvent e) {

try {
if (e.getSource() == btnC) {
txtResult.setText("0");
str.setLength(0);
} else if (e.getSource() == btnCE) {
txtResult.setText("0.");
str.setLength(0);
} else if (e.getSource() == btnPlusMinus) {
x = Double.parseDouble(txtResult.getText().trim());
txtResult.setText("" + (-x));
}

else if (e.getSource() == btnPlus) {
x = Double.parseDouble(txtResult.getText().trim());
str.setLength(0);
y = 0d;
z = 1;
} else if (e.getSource() == btnMinus) {
x = Double.parseDouble(txtResult.getText().trim());
str.setLength(0);
y = 0d;
z = 2;
} else if (e.getSource() == btnTimes) {
x = Double.parseDouble(txtResult.getText().trim());
str.setLength(0);
y = 0d;
z = 3;
} else if (e.getSource() == btnDivided) {
x = Double.parseDouble(txtResult.getText().trim());
str.setLength(0);
y = 0d;
z = 4;
} else if (e.getSource() == btnEqual) {
str.setLength(0);
switch (z) {
case 1:
txtResult.setText("" + (x + y));
break;
case 2:
txtResult.setText("" + (x - y));
break;
case 3:
txtResult.setText("" + (x * y));
break;
case 4:
txtResult.setText("" + (x / y));
break;
}
}

else if (e.getSource() == btnPoint) {
if (txtResult.getText().trim().indexOf('.') != -1)// 判断字符串中是否已经包含了小数点
{

} else// 如果没数点有小
{
if (txtResult.getText().trim().equals("0"))// 如果初时显示为0
{
str.setLength(0);
txtResult.setText((str.append("0"
+ e.getActionCommand())).toString());
} else if (txtResult.getText().trim().equals(""))// 如果初时显示为空则不做任何作
{
} else {
txtResult.setText(str.append(e.getActionCommand())
.toString());
}
}

y = 0d;
}

else if (e.getSource() == btnSqrt)// 求平方根
{
x = Double.parseDouble(txtResult.getText().trim());
txtResult.setText("数字格式异常");
if (x < 0)
txtResult.setText("负数没有平方根");
else
txtResult.setText("" + Math.sqrt(x));
str.setLength(0);
y = 0d;
}

else if (e.getSource() == btnZero)// 如果选择的是"0"这个数字键
{
if (txtResult.getText().trim().equals("0"))// 如果显示屏显示的为零不做作
{
} else {
txtResult.setText(str.append(e.getActionCommand())
.toString());
y = Double.parseDouble(txtResult.getText().trim());
}
}

else// 其他的数字键
{
txtResult.setText(str.append(e.getActionCommand()).toString());
y = Double.parseDouble(txtResult.getText().trim());
}
} catch (NumberFormatException ae) {
txtResult.setText("数字格式异常");
} catch (StringIndexOutOfBoundsException ae) {
txtResult.setText("字符串索引越界");
}

}

public static void main(String arg[]) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
new Calculator();
}

}

82

相关问题

java计算器课设
天使之懿727 1970-01-01

Java设计计算器
fionazhang77 1970-01-01

急求java计算器 课程设计报告
起舞徘徊风露下 1970-01-01

急求java计算器 课程设计报告

用java设计计算器
非非1227 1970-01-01

急求java 计算器课程设计报告,有源码,
疯疯丫头315 1970-01-01

急求java 计算器课程设计报告,有源码要求内容,程序开发背景,设计过程,重点难点,设计说明,程序调试,程序运行,那位高手帮我写写呀!谢谢了!!!

用Java设计一个简单的计算器。
MyronKiven 1970-01-01

设计一个简单的计算器,能够对两个数据进行“加、减、乘、除”运算。要求:合理应用布局设计,注重界面美观、友好,要求处理NumberFormatException异常。

高手帮忙用Java设计个计算器程序!
CSYMiracle 1970-01-01

用Java实现一个applet计算器程序,要有+,-,*,/功能。会做的朋友帮帮忙,小弟先谢过各位了!还有更简单点的答案吗?

java应用小程序计算器
欧阳安Muse 1970-01-01

是期末的课程设计阿!或是什么日历阿,画图程序等等一类都是可以的能较差就行啊

vb计算器设计
天道酬勤1212 1970-01-01

vb计算器设计要加减乘除,小数点,倒数,还有清零

最新问答

排水论文在哪发?
伊兰0518 2021-09-19

小区市外排水论文发哪个杂志可以呢?我需要发表一篇这方面的论文。

word转pdf,为什么不显示图片图片?
花花的老妈 2021-09-19

我想把论文从word格式转换成PDF格式,用的金山WPS,可转换完成之后,里面的流程图就不见了,空白~~这是为什么呢?谁能帮我解决一下!谢谢!

公众号与小程序有什么区别
汤糖躺烫湯 2021-09-19

公众号与小程序有什么区别

如何制作电子小报
dream959595 2021-09-19

镀铬什么意思
autumngold 2021-09-19

镀铬什么意思

中国电影艺术的思想
幸福顺延 2021-09-19

中国针灸大纲作者是谁?
王子麻麻 2021-09-19

热门问答

排水论文在哪发?
伊兰0518 2021-09-19

小区市外排水论文发哪个杂志可以呢?我需要发表一篇这方面的论文。

word转pdf,为什么不显示图片图片?
花花的老妈 2021-09-19

我想把论文从word格式转换成PDF格式,用的金山WPS,可转换完成之后,里面的流程图就不见了,空白~~这是为什么呢?谁能帮我解决一下!谢谢!

公众号与小程序有什么区别
汤糖躺烫湯 2021-09-19

公众号与小程序有什么区别

如何制作电子小报
dream959595 2021-09-19

镀铬什么意思
autumngold 2021-09-19

镀铬什么意思

中国电影艺术的思想
幸福顺延 2021-09-19

中国针灸大纲作者是谁?
王子麻麻 2021-09-19

Coptyright © www.lw85.com电脑版