一氧化碳有毒吗:一个简单的只有加减的计算器的JAVA程序?

来源:百度文库 编辑:神马品牌网 时间:2024/05/07 15:21:19
就是一个小的计算器,有加减功能,就可以了,

//Dm.java
import java.awt.*;
import java.io.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.lang.*;
import javax.swing.JApplet.*;

public class Dm extends JApplet
{
Mypanel1 panel1=new Mypanel1(this);
Mypanel2 panel2=new Mypanel2(this);
public JTextField textField1=new JTextField(20);
public void init()
{
Container con=getContentPane();
con.setLayout(new BorderLayout());
setSize(360,190);
con.add(textField1,BorderLayout.NORTH);
//textField1.setEditable(false);
con.add(panel1,BorderLayout.CENTER);//CE,C,Backspace,1-9
con.add(panel2,BorderLayout.WEST);//MS,MR,MC,M+
}

class Mypanel1 extends JApplet implements ActionListener
{
double vard,vard1,result=0;//保存中间值(vard)和最后结果(result)
int key,presskey;//用来保存当前所进行的运算(key)和前一次所进行的运算(presskey)
JPanel panel11=new JPanel(new GridLayout(5,5));
//设计
JLabel label=new JLabel(" ");
JLabel label1=new JLabel(" ");
JButton button0=new JButton("0");
JButton button1=new JButton("1");
JButton button2=new JButton("2");
JButton button3=new JButton("3");
JButton button4=new JButton("4");
JButton button5=new JButton("5");
JButton button6=new JButton("6");
JButton button7=new JButton("7");
JButton button8=new JButton("8");
JButton button9=new JButton("9");
JButton buttonAdd=new JButton("+");
JButton buttonSub=new JButton("-");
JButton buttonDiv=new JButton("/");
JButton buttonMul=new JButton("*");
JButton buttonEqual=new JButton("=");
JButton buttonSqrt=new JButton("sqrt");
JButton buttonMod=new JButton("%");
JButton buttonDaoShu=new JButton("1/X ");
JButton buttonAS=new JButton("+/- ");
JButton buttonDot=new JButton(".");
JButton buttonBackspace=new JButton("退格");
JButton buttonCE=new JButton("CE");
JButton buttonC=new JButton("C");
Mypanel1(Container con1){
con1=getContentPane();
con1.setLayout(new FlowLayout());
con1.add(panel11);
panel11.add(label);
panel11.add(label1);
panel11.add(buttonBackspace);
buttonBackspace.addActionListener(this);
buttonBackspace.setForeground(Color.red);
panel11.add(buttonCE);
buttonCE.addActionListener(this);
buttonCE.setForeground(Color.red);
panel11.add(buttonC);
buttonC.addActionListener(this);
buttonC.setForeground(Color.red);
panel11.add(button7);
button7.addActionListener(this);
button7.setForeground(Color.blue);
panel11.add(button8);
button8.addActionListener(this);
button8.setForeground(Color.blue);
panel11.add(button9);
button9.addActionListener(this);
button9.setForeground(Color.blue);
panel11.add(buttonDiv);
buttonDiv.addActionListener(this);
buttonDiv.setForeground(Color.red);
panel11.add(buttonSqrt);
buttonSqrt.addActionListener(this);
buttonSqrt.setForeground(Color.blue);
panel11.add(button4);
button4.addActionListener(this);
button4.setForeground(Color.blue);
panel11.add(button5);
button5.addActionListener(this);
button5.setForeground(Color.blue);
panel11.add(button6);
button6.addActionListener(this);
button6.setForeground(Color.blue);
panel11.add(buttonMul);
buttonMul.addActionListener(this);
buttonMul.setForeground(Color.red);
panel11.add(buttonMod);
buttonMod.addActionListener(this);
buttonMod.setForeground(Color.blue);
panel11.add(button1);
button1.addActionListener(this);
button1.setForeground(Color.blue);
panel11.add(button2);
button2.addActionListener(this);
button2.setForeground(Color.blue);
panel11.add(button3);
button3.addActionListener(this);
button3.setForeground(Color.blue);
panel11.add(buttonSub);
buttonSub.addActionListener(this);
buttonSub.setForeground(Color.red);
panel11.add(buttonDaoShu);
buttonDaoShu.addActionListener(this);
buttonDaoShu.setForeground(Color.blue);
panel11.add(button0);
button0.addActionListener(this);
button0.setForeground(Color.blue);
panel11.add(buttonAS);
buttonAS.addActionListener(this);
buttonAS.setForeground(Color.blue);
panel11.add(buttonDot);
buttonDot.addActionListener(this);
buttonDot.setForeground(Color.blue);
panel11.add(buttonAdd);
buttonAdd.addActionListener(this);
buttonAdd.setForeground(Color.red);
panel11.add(buttonEqual);
buttonEqual.addActionListener(this);
buttonEqual.setForeground(Color.red);
setSize(50,80);
}
public void actionPerformed(ActionEvent e)
{
Object temp=e.getSource();
if(temp==button0){
textField1.setText(textField1.getText()+Integer.toString(0));
}
if(temp==button1){
textField1.setText(textField1.getText()+Integer.toString(1));
}
if(temp==button2){
textField1.setText(textField1.getText()+Integer.toString(2));
}
if(temp==button3){
textField1.setText(textField1.getText()+Integer.toString(3));
}
if(temp==button4){
textField1.setText(textField1.getText()+Integer.toString(4));
}
if(temp==button5){
textField1.setText(textField1.getText()+Integer.toString(5));
}
if(temp==button6){
textField1.setText(textField1.getText()+Integer.toString(6));
}
if(temp==button7){
textField1.setText(textField1.getText()+Integer.toString(7));
}
if(temp==button8){
textField1.setText(textField1.getText()+Integer.toString(8));
}
if(temp==button9){
textField1.setText(textField1.getText()+Integer.toString(9));
}
//按下'.'按钮时,判断当前文本框内字符串中含不含'.',如果已含,则不允许再插入'.'
if(temp==buttonDot){
boolean isDot=false;
if(textField1.getText().length()==0)
isDot=true;
for(int i=0;i<textField1.getText().length();i++){
if('.'==textField1.getText().charAt(i)){
isDot=true;
break;
}
}
if(isDot==false)
textField1.setText(textField1.getText()+'.');
}
if(temp==buttonAdd){
presskey=key=0;
vard=Double.parseDouble(textField1.getText());
textField1.setText("");
result+=vard;
}
if(temp==buttonSub){
presskey=key=1;
vard=Double.parseDouble(textField1.getText());
textField1.setText("");
result-=vard;
}
if(temp==buttonMul){
presskey=key=2;
vard=Double.parseDouble(textField1.getText());
textField1.setText("");
result=vard;
}
if(temp==buttonDiv){
presskey=key=3;
vard=Double.parseDouble(textField1.getText());
textField1.setText("");
result=vard;
}
if((presskey==0||presskey==1||presskey==2||presskey==3)&&presskey!=4){
vard1=Double.parseDouble(textField1.getText());//取第二个操作数
}
if(temp==buttonEqual){
vard=Double.parseDouble(textField1.getText());
if(presskey==0)
result+=vard;
if(presskey==1)
result-=vard;
if(presskey==2)
result*=vard;
if(presskey==3){
if(vard==0)
textField1.setText("除数不能为0");
else
result/=vard;
}
if(presskey==4){
if(key==0)
result+=vard1;
if(key==1)
result-=vard1;
if(key==2)
result*=vard1;
if(key==3){
if(vard1==0)
textField1.setText("除数不能为0");
else
result/=vard1;
}
}
//判断是否输出结果
if(presskey!=3 || key!=3)
textField1.setText(Double.toString(result));
else{
if(vard!=0 || vard1!=0)
textField1.setText(Double.toString(result));
}
presskey=4;
}
//按下'Backspace'键,利用循环将当前字符串中的最后一个字母删除
if (temp == buttonBackspace) {
String s = textField1.getText();
textField1.setText("");
for (int i = 0; i < s.length() - 1; i++) {
char a = s.charAt(i);
textField1.setText(textField1.getText() + a);
}
}
//按下'CE'按钮,将当前文本框内数据清除
if (temp == buttonCE) {
textField1.setText("");
}
//按下'C'按钮,文本框内数据清除,同时var,answer清0
if (temp == buttonC) {
vard =result=vard1=0;
textField1.setText("");
}
if(temp==buttonSqrt){
String s=textField1.getText();
if(s.charAt(0)=='-')
textField1.setText("负数不能开平方");
else
textField1.setText(Double.toString(java.lang.Math.sqrt(Double.parseDouble(textField1.getText()))));
}
if(temp==buttonMod){
boolean isDot=false;
String s=textField1.getText();
for(int i=0;i<s.length();i++)
if(s.charAt(i)=='.'){
isDot=true;
break;
}
if(isDot=true)
textField1.setText(Double.toString(Double.parseDouble(s)/100.0));
else
{
if(Integer.parseInt(s)%100==0)
textField1.setText(Integer.toString(Integer.parseInt(s)/100));
else
textField1.setText(Double.toString(Double.parseDouble(s)/100.0));
}
}
//按下'+/-'按钮时处理
if(temp==buttonAS){
String s = textField1.getText();
//如果当前字符串第一个字符不是符号,则添加一个符号在首字母处
if (s.charAt(0)!='-') {
textField1.setText('-' + s);
}
//如果当前字符串首字母有'-'号,代表现在是个负数,再按下时,则将首符号去掉
if (s.charAt(0) == '-') {
textField1.setText("");
textField1.setText(s.substring(1));
}
}
if(temp==buttonDaoShu){
if(Double.parseDouble(textField1.getText())==0)
textField1.setText("0不能求倒数");
else
textField1.setText(Double.toString(1/Double.parseDouble(textField1.getText())));
}
}
}
class Mypanel2 extends JApplet implements ActionListener
{
double memoryd;//内存中存储的double型数据
int memoryi;//内存中存储的int型数据
JTextField textField2=new JTextField();
JPanel panel22=new JPanel(new GridLayout(5,1));
JButton buttonMR=new JButton("MR");
JButton buttonMC=new JButton("MC");
JButton buttonMS=new JButton("MS");
JButton buttonM=new JButton("M+");
Mypanel2(Container con2){
con2=getContentPane();
con2.setLayout(new FlowLayout());
con2.add(panel22);
panel22.add(textField2);
textField2.setEditable(false);
panel22.add(buttonMC);
buttonMC.addActionListener(this);
buttonMC.setForeground(Color.red);
panel22.add(buttonMR);
buttonMR.addActionListener(this);
buttonMR.setForeground(Color.red);
panel22.add(buttonMS);
buttonMS.addActionListener(this);
buttonMS.setForeground(Color.red);
panel22.add(buttonM);
buttonM.addActionListener(this);
buttonM.setForeground(Color.red);
}
public void actionPerformed(ActionEvent e){
//计算器有关内存操作
Object temp=e.getSource();
//'MC'的操作,将内存清0
if (temp == buttonMC) {
memoryd = memoryi = 0;
textField2.setText("");
}
//'MS'的操作,将当前文本框内容保存入内存,显示'M'
if (temp == buttonMS) {
boolean isDot = false;
textField2.setText(" M");
for (int i = 0; i <textField1.getText().length(); i++)
if ('.' == textField1.getText().charAt(i)) {
isDot = true;
break;
}
//如果是double,则存入memoryd(double存储器)
if (isDot == true) {
memoryd = Double.parseDouble(textField1.getText());
memoryi = 0; //保证存储器中存放最新的值
}
//如果是int,则存入memoryi(int存储器)
else {
memoryi = Integer.parseInt(textField1.getText());
memoryd = 0; //保证存储器中存放最新的值
}
}
//'MR'的操作,将存储器中的信息输出
if (temp == buttonMR) {
if (memoryd != 0)
textField1.setText(Double.toString(memoryd));
if (memoryi != 0)
textField1.setText(Integer.toString(memoryi));
}
//'M+'的功能,将当前文本框里的数据和存储器中数据相加后,再存入存储器
if (temp == buttonM) {
boolean isDot = false;
for (int i = 0; i <textField1.getText().length(); i++)
if ('.' == textField1.getText().charAt(i)) {
isDot = true;
break;
}
if (memoryi != 0)
{ //存储中是一个int型数
if (isDot == false) //被加数是一个int型数
memoryi += Integer.parseInt(textField1.getText());
else
{ //被加数是一个double型数,则将int存储器中数传入double存储器与当前数相加,int存储器清零
memoryd = memoryi + Double.parseDouble(textField1.getText());
memoryi = 0;
}
}
else
memoryd += Double.parseDouble(textField1.getText());
}
}
}
}