java计算器课设
最新问答
- bluebirdtang
我有,给你一个
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);
}
}
给分!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!浏览 150赞 157时间 2023-08-31 - babycarolyn
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();
}
}浏览 493赞 130时间 2023-05-22
相关问答
-
java计算器课设
-
Java设计计算器
-
急求java计算器 课程设计报告
-
用java设计计算器
-
急求java 计算器课程设计报告,有源码,
-
JAVA 用JAVA设计程序,实现一个计算器,并能实现计算
-
用Java设计一个简单的计算器。
-
如何java设计计算器并实现功能
-
高手帮忙用Java设计个计算器程序!
-
作业求解--Java设计Windows计算器模拟程序
-
java应用小程序计算器
-
vb计算器设计
-
用Java设计一个图形界面(GUI)的计算器应用程序。
-
用java模拟模拟windows中计算器?
-
用java程序编写一个计算器
-
JAVA课程设计天数计算器(输入年月日等到是当年的第几天)Would文档
-
java课程设计报告只是某种算法怎么写
-
哪有java课程设计论文?
-
(图形界面的应用)用JAVA语言设计一个计算器程序???
-
java设计中国象棋课程设计怎么做