Java课程设计 计算器simplecalculator

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

class simplecalculator
{
static String point=new String();
static String Amal=new String();
static String ONE=new String();
static String TWO=new String();
static String THREE=new String();
static String FOUR=new String();
static String FIVE=new String();
static String SIX=new String();
static String SEVEN=new String();
static String EIGHT=new String();
static String NINE=new String();
static String ZERO=new String();
static String ResultState=new String();

static Double QF;
static JButton zero=new JButton("0");
static JButton one=new JButton("1");
static JButton two=new JButton("2");
static JButton three=new JButton("3");
static JButton four=new JButton("4");
static JButton five=new JButton("5");
static JButton six=new JButton("6");
static JButton seven=new JButton("7");
static JButton eight=new JButton("8");
static JButton nine=new JButton("9");
static JButton add=new JButton("+");
static JButton sub=new JButton("-");
static JButton mul=new JButton("*");
static JButton div=new JButton("/");
static JButton QuFan=new JButton("+/-");
static JButton Dian=new JButton(".");
static JButton equal=new JButton("=");
static JButton clear=new JButton("C");
static JButton BaiFen=new JButton("%");
static JButton FenZhiYi=new JButton("1/x");
static int i=0;
static Double addNumber;
static Double subNumber;
static Double mulNumber;
static Double divNumber;
static Double equalNumber;
static Double temp;
static JTextArea result=new JTextArea(1,20);

public static void main(String[] args)
{
JFrame frame=new JFrame("计算器");
result.setEditable(false);
result.setText("");
ResultState="窗口空";
JPanel ForResult=new JPanel();
JPanel ForButton7_clear=new JPanel();
JPanel ForButton4_mul=new JPanel();
JPanel ForButton1_sub=new JPanel();
JPanel ForButton0_equal=new JPanel();

FlowLayout FLO=new FlowLayout();
ForResult.add(result);

ForButton7_clear.setLayout(FLO);
ForButton7_clear.add(seven);
ForButton7_clear.add(eight);
ForButton7_clear.add(nine);
ForButton7_clear.add(div);
ForButton7_clear.add(clear);

ForButton4_mul.setLayout(FLO);
ForButton4_mul.add(four);
ForButton4_mul.add(five);
ForButton4_mul.add(six);
ForButton4_mul.add(mul);
ForButton4_mul.add(BaiFen);

ForButton1_sub.setLayout(FLO);
ForButton1_sub.add(one);
ForButton1_sub.add(two);
ForButton1_sub.add(three);
ForButton1_sub.add(sub);
ForButton1_sub.add(FenZhiYi);

ForButton0_equal.setLayout(FLO);
ForButton0_equal.add(zero);
ForButton0_equal.add(QuFan);
ForButton0_equal.add(Dian);
ForButton0_equal.add(add);
ForButton0_equal.add(equal);

frame.getContentPane().setLayout(FLO);
frame.getContentPane().add(ForResult);
frame.getContentPane().add(ForButton7_clear);
frame.getContentPane().add(ForButton4_mul);
frame.getContentPane().add(ForButton1_sub);
frame.

getContentPane().add(ForButton0_equal);

frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setBounds(250,250,245,245);
frame.setResizable(false);
frame.setVisible(true);

clear.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
result.setText("");
ZERO="";
ONE="";
TWO="";
THREE="";
FOUR="";
FIVE="";
SIX="";
SEVEN="";
EIGHT="";
NINE="";
ResultState="窗口空";
point="";
i=0;

}
});
zero.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
ZERO="已经点击";
ResultState="窗口不为空";
if(ONE=="已经点击"||TWO=="已经点击"||THREE=="已经点击"||FOUR=="已经点击"||FIVE=="已经点击"||SIX=="已经点击"||SEVEN=="已经点击"||EIGHT=="已经点击"||NINE=="已经点击")
{
result.append("0");
}
if(ResultState=="窗口空")
{
result.setText("0");
}

}
});
one.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
ONE="已经点击";
ResultState="窗口不为空";
if(point=="已经点击"||ZERO!="已经点击"||ONE=="已经点击"||TWO=="已经点击"||THREE=="已经点击"||FIVE=="已经点击"||SIX=="已经点击"||SEVEN=="已经点击"||EIGHT=="已经点击"||NINE=="已经点击"&&result.getText()!="0")
{
result.append("1");
}
if(ResultState=="窗口空")
{
result.setText("1");
}
}
});
two.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
TWO="已经点击";
ResultState="窗口不为空";
if(point=="已经点击"||ZERO!="已经点击"||ONE=="已经点击"||TWO=="已经点击"||THREE=="已经点击"||FIVE=="已经点击"||SIX=="已经点击"||SEVEN=="已经点击"||EIGHT=="已经点击"||NINE=="已经点击"&&result.getText()!="0")
{
result.append("2");
}
if(ResultState=="窗口空")
{
result.setText("2");
}
}
});
three.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
THREE="已经点击";
ResultState="窗口不为空";
if(point=="已经点击"||ZERO!="已经点击"||ONE=="已经点击"||TWO=="已经点击"||THREE=="已经点击"||FIVE=="已经点击"||SIX=="已经点击"||SEVEN=="已经点击"||EIGHT=="已经点击"||NINE=="已经点击"&&result.getText()!="0")
{
result.append("3");
}
if(ResultState=="窗口空")
{
result.setText("3");
}
}
});
four.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
FOUR="已经点击";
ResultState="窗口不为空";
if(point=="已经点击"||ZERO!="已经点击"||ONE=="已经点击"||TWO=="已经点击"||THREE=="已经点击"||FIVE=="已经点击"||SIX=="已经点击"|

|SEVEN=="已经点击"||EIGHT=="已经点击"||NINE=="已经点击"&&result.getText()!="0")
{
result.append("4");
}
if(ResultState=="窗口空")
{
result.setText("4");
}
}
});
five.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
FIVE="已经点击";
ResultState="窗口不为空";
if(point=="已经点击"||ZERO!="已经点击"||ONE=="已经点击"||TWO=="已经点击"||THREE=="已经点击"||FIVE=="已经点击"||SIX=="已经点击"||SEVEN=="已经点击"||EIGHT=="已经点击"||NINE=="已经点击"&&result.getText()!="0")
{
result.append("5");
}
if(ResultState=="窗口空")
{
result.setText("6");
}
}
});
six.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
SIX="已经点击";
ResultState="窗口不为空";
if(point=="已经点击"||ZERO!="已经点击"||ONE=="已经点击"||TWO=="已经点击"||THREE=="已经点击"||FIVE=="已经点击"||SIX=="已经点击"||SEVEN=="已经点击"||EIGHT=="已经点击"||NINE=="已经点击"&&result.getText()!="0")
{
result.append("6");
}
if(ResultState=="窗口空")
{
result.setText("6");
}
}
});
seven.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
SEVEN="已经点击";
ResultState="窗口不为空";
if(point=="已经点击"||ZERO!="已经点击"||ONE=="已经点击"||TWO=="已经点击"||THREE=="已经点击"||FIVE=="已经点击"||SIX=="已经点击"||SEVEN=="已经点击"||EIGHT=="已经点击"||NINE=="已经点击"&&result.getText()!="0")
{
result.append("7");
}
if(ResultState=="窗口空")
{
result.setText("7");
}
}
});
eight.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
EIGHT="已经点击";
ResultState="窗口不为空";
if(point=="已经点击"||ZERO!="已经点击"||ONE=="已经点击"||TWO=="已经点击"||THREE=="已经点击"||FIVE=="已经点击"||SIX=="已经点击"||SEVEN=="已经点击"||EIGHT=="已经点击"||NINE=="已经点击"&&result.getText()!="0")
{
result.append("8");
}
if(ResultState=="窗口空")
{
result.setText("8");
}
}
});
nine.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
NINE="已经点击";
ResultState="窗口不为空";
if(point=="已经点击"||ZERO!="已经点击"||ONE=="已经点击"||TWO=="已经点击"||THREE=="已经点击"||FIVE=="已经点击"||SIX=="已经点击"||SEVEN=="已经点击"||EIGHT=="已经点击"||NINE=="已经点击"&&result.getText()!="0")
{
result.append("9");
}
if(ResultState=="窗口空")
{
result.setText("9");
}
}
});

Dian.addActionListener( n

ew ActionListener()
{
public void actionPerformed(ActionEvent e)
{
point="已经点击";

i=i+1;
if(ResultState=="窗口不为空"&&i==1)
{
result.append(".");
}
}
});


add.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Amal="已经选择加号";
addNumber=Double.valueOf(result.getText()).doubleValue();
result.setText("");
i=0;
}
});
sub.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Amal="已经选择减号";
subNumber=Double.valueOf(result.getText()).doubleValue();
result.setText("");
i=0;
}
});
mul.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Amal="已经选择乘号";
mulNumber=Double.valueOf(result.getText()).doubleValue();
result.setText("");
i=0;
}
});
div.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Amal="已经选择除号";
divNumber=Double.valueOf(result.getText()).doubleValue();
result.setText("");
i=0;
}
});
QuFan.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{


QF=new Double(Double.valueOf(result.getText()).doubleValue());
QF=QF*(-1);
result.setText(QF.toString());
}
});

equal.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
equalNumber=Double.valueOf(result.getText()).doubleValue();
if(Amal=="已经选择加号")
{
temp=addNumber+equalNumber;
result.setText(temp.toString());
}
if(Amal=="已经选择减号")
{
temp=subNumber-equalNumber;
result.setText(temp.toString());
}
if(Amal=="已经选择乘号")
{
temp=mulNumber*equalNumber;
result.setText(temp.toString());
}
if(Amal=="已经选择除号")
{
temp=divNumber/equalNumber;
result.setText(temp.toString());
}
}
});

BaiFen.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(ResultState=="窗口不为空")
{
temp=Double.valueOf(result.getText()).doubleValue()/100;
result.setText(temp.toString());
}
}
});

FenZhiYi.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
temp=1/(Double.valueOf(result.getText()).doubleValue());
result.setText(temp.toString());
}
});

}

}


相关文档
最新文档