---------------------- ASP.Net+Unity开发.Net培训、期待与您交流! ----------------------
package JiSuanQi;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import javax.swing.*;
public class JiSuanQi extends JFrame implements ActionListener {
JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15,
b16;
JFrame f;
JPanel p1=new JPanel();
GridLayout c = new GridLayout(4, 4);
BorderLayout b= new BorderLayout(0,0);
JTextField shuru = new JTextField();
JTextField xianshi = new JTextField();
String str3="";
String str1="";
String str2="";
String str4="";
String str5="";
double value1,value2,resoult=0;
public JiSuanQi(){
init();
}
public void init() {
f = new JFrame("计算器");
Container ct = f.getContentPane();
shuru.setHorizontalAlignment(JTextField.RIGHT);
xianshi.setHorizontalAlignment(JTextField.LEFT);
ct.add(p1,"North");
p1.setLayout(b);
p1.add(xianshi, "North");
p1.add(shuru,"Center");
JPanel p2 = new JPanel();
ct.add(p2, "Center");
GridLayout c = new GridLayout(4, 4,4,4);
p2.setLayout(c);
JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16;
b1 = new JButton("1");
b2 = new JButton("2");
b3 = new JButton("3");
b4 = new JButton("4");
b5 = new JButton("5");
b6 = new JButton("6");
b7 = new JButton("7");
b8 = new JButton("8");
b9 = new JButton("9");
b10 = new JButton("0");
b11 = new JButton(".");
b12 = new JButton("=");
b13 = new JButton("+");
b14 = new JButton("-");
b15 = new JButton("*");
b16 = new JButton("/");
p2.add(b1);
b1.addActionListener(this);
p2.add(b2);
b2.addActionListener(this);
p2.add(b3);
b3.addActionListener(this);
p2.add(b13);
b13.addActionListener(this);
p2.add(b4);
b4.addActionListener(this);
p2.add(b5);
b5.addActionListener(this);
p2.add(b6);
b6.addActionListener(this);
p2.add(b14);
b14.addActionListener(this);
p2.add(b7);
b7.addActionListener(this);
p2.add(b8);
b8.addActionListener(this);
p2.add(b9);
b9.addActionListener(this);
p2.add(b15);
b15.addActionListener(this);
p2.add(b10);
b10.addActionListener(this);
p2.add(b11);
b11.addActionListener(this);
p2.add(b12);
b12.addActionListener(this);
p2.add(b16);
b16.addActionListener(this);
/* f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});*/
f.setSize(300, 250);
f.setVisible(true);
}
// double sum = 0;
// Vector v = new Vector(1, 1);
// Vector v2 = new Vector(1, 1);
/* public JiSuanQi() {
v2.addElement(new String("init"));
v.addElement(new String("init"));
}
public void actionPerformed(ActionEvent e) {
String str = shuru.getText();
if (e.getActionCommand() == "+") // 加号的实现;
{
if (("+").equals((String) v2.lastElement())
|| ("-").equals((String) v2.lastElement())
|| ("x").equals((String) v2.lastElement())
|| ("/").equals((String) v2.lastElement())
|| ("=").equals((String) v2.lastElement())) {
shuru.setText(String.valueOf(sum));
} else if (("init").equals((String) v2.lastElement())) {
shuru.setText(String.valueOf(sum));
v2.addElement(new String("="));
} else {
double d = Double.parseDouble(str);
if (("+").equals((String) v.lastElement())) {
sum = sum + d;
shuru.setText(String.valueOf(sum));
} else if (("-").equals((String) v.lastElement())) {
sum = sum - d;
shuru.setText(String.valueOf(sum));
} else if (("x").equals((String) v.lastElement())) {
sum = sum * d;
shuru.setText(String.valueOf(sum));
} else if (("/").equals((String) v.lastElement())) {
sum = sum / d;
shuru.setText(String.valueOf(sum));
} else if (("=").equals((String) v.lastElement())) {
sum = sum + d;
shuru.setText(String.valueOf(sum));
} else {
sum = sum + d;
shuru.setText(String.valueOf(sum));
}
}
v.addElement(new String("+"));
v2.addElement(new String("+"));
}
if (e.getActionCommand() == "-") // 减号的实现;
{
if (("+").equals((String) v2.lastElement())
|| ("-").equals((String) v2.lastElement())
|| ("x").equals((String) v2.lastElement())
|| ("/").equals((String) v2.lastElement())
|| ("=").equals((String) v2.lastElement())) {
shuru.setText(String.valueOf(sum));
} else if (("init").equals((String) v2.lastElement())) {
shuru.setText(String.valueOf(sum));
v2.addElement(new String("="));
} else {
double d = Double.parseDouble(str);
if (("+").equals((String) v.lastElement())) {
sum = sum + d;
shuru.setText(String.valueOf(sum));
} else if (("-").equals((String) v.lastElement())) {
sum = sum - d;
shuru.setText(String.valueOf(sum));
} else if (("x").equals((String) v.lastElement())) {
sum = sum * d;
shuru.setText(String.valueOf(sum));
} else if (("/").equals((String) v.lastElement())) {
sum = sum / d;
shuru.setText(String.valueOf(sum));
} else if (("=").equals((String) v.lastElement())) {
sum = sum + d;
shuru.setText(String.valueOf(sum));
} else {
sum = sum + d;
shuru.setText(String.valueOf(sum));
}
}
v.addElement(new String("-"));
v2.addElement(new String("-"));
}
if (e.getActionCommand() == "x") // 乘号的实现;
{
if (("+").equals((String) v2.lastElement())
|| ("-").equals((String) v2.lastElement())
|| ("x").equals((String) v2.lastElement())
|| ("/").equals((String) v2.lastElement())
|| ("=").equals((String) v2.lastElement())) {
shuru.setText(String.valueOf(sum));
} else if (("init").equals((String) v2.lastElement())) {
shuru.setText(String.valueOf(sum));
v2.addElement(new String("="));
} else {
double d = Double.parseDouble(str);
if (("+").equals((String) v.lastElement())) {
sum = sum + d;
shuru.setText(String.valueOf(sum));
} else if (("-").equals((String) v.lastElement())) {
sum = sum - d;
shuru.setText(String.valueOf(sum));
} else if (("x").equals((String) v.lastElement())) {
sum = sum * d;
shuru.setText(String.valueOf(sum));
} else if (("/").equals((String) v.lastElement())) {
sum = sum / d;
shuru.setText(String.valueOf(sum));
} else if (("=").equals((String) v.lastElement())) {
sum = sum + d;
shuru.setText(String.valueOf(sum));
} else {
sum = sum + d;
shuru.setText(String.valueOf(sum));
}
}
v.addElement(new String("x"));
v2.addElement(new String("x"));
}
if (e.getActionCommand() == "/") // 除号的实现;
{
if (("+").equals((String) v2.lastElement())
|| ("-").equals((String) v2.lastElement())
|| ("x").equals((String) v2.lastElement())
|| ("/").equals((String) v2.lastElement())
|| ("=").equals((String) v2.lastElement())) {
shuru.setText(String.valueOf(sum));
} else if (("init").equals((String) v2.lastElement())) {
shuru.setText(String.valueOf(sum));
v2.addElement(new String("="));
} else {
double d = Double.parseDouble(str);
if (("+").equals((String) v.lastElement())) {
sum = sum + d;
shuru.setText(String.valueOf(sum));
} else if (("-").equals((String) v.lastElement())) {
sum = sum - d;
shuru.setText(String.valueOf(sum));
} else if (("x").equals((String) v.lastElement())) {
sum = sum * d;
shuru.setText(String.valueOf(sum));
} else if (("/").equals((String) v.lastElement())) {
sum = sum / d;
shuru.setText(String.valueOf(sum));
} else if (("=").equals((String) v.lastElement())) {
sum = sum + d;
shuru.setText(String.valueOf(sum));
} else {
sum = sum + d;
shuru.setText(String.valueOf(sum));
}
}
v.addElement(new String("/"));
v2.addElement(new String("/"));
}
if (e.getActionCommand() == "=") // 等号实现;
{
if (("+").equals((String) v2.lastElement())
|| ("-").equals((String) v2.lastElement())
|| ("x").equals((String) v2.lastElement())
|| ("/").equals((String) v2.lastElement())
|| ("=").equals((String) v2.lastElement())) {
shuru.setText(String.valueOf(sum));
} else if (("init").equals((String) v2.lastElement())) {
shuru.setText(String.valueOf(sum));
} else {
double d = Double.parseDouble(str);
if (("+").equals((String) v.lastElement())) {
sum = sum + d;
shuru.setText(String.valueOf(sum));
} else if (("-").equals((String) v.lastElement())) {
sum = sum - d;
shuru.setText(String.valueOf(sum));
} else if (("x").equals((String) v.lastElement())) {
sum = sum * d;
shuru.setText(String.valueOf(sum));
} else if (("/").equals((String) v.lastElement())) {
sum = sum / d;
shuru.setText(String.valueOf(sum));
} else if (("=").equals((String) v.lastElement())) {
sum = sum + d;
shuru.setText(String.valueOf(sum));
// shuru.setText(String.valueOf(sum));
} else {
sum = sum + d;
shuru.setText(String.valueOf(sum));
}
}
v.addElement(new String("="));
v2.addElement(new String("="));
}
if (e.getActionCommand() == "1" || e.getActionCommand() == "2"
|| e.getActionCommand() == "3" || e.getActionCommand() == "4"
|| e.getActionCommand() == "5" || e.getActionCommand() == "6"
|| e.getActionCommand() == "7" || e.getActionCommand() == "8"
|| e.getActionCommand() == "9" || e.getActionCommand() == "0"
|| e.getActionCommand() == ".") // 1,2,3,4,5,6,7,8,9,0,.的实现;
{
if (("+").equals((String) v2.lastElement())
|| ("-").equals((String) v2.lastElement())
|| ("x").equals((String) v2.lastElement())
|| ("/").equals((String) v2.lastElement())
|| ("init").equals((String) v2.lastElement())) {
shuru.setText("");
shuru.setText(e.getActionCommand());
} else if (("=").equals((String) v2.lastElement())) {
sum = 0;
shuru.setText("");
shuru.setText(e.getActionCommand());
} else {
shuru.setText(shuru.getText() + e.getActionCommand());// 调用其父类中的getText()方法
}
v2.addElement(new String(e.getActionCommand()));
}
}*/
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "1" || e.getActionCommand() == "2" // 1,2,3,4,5,6,7,8,9,0的实现;
|| e.getActionCommand() == "3" || e.getActionCommand() == "4"
|| e.getActionCommand() == "5" || e.getActionCommand() == "6"
|| e.getActionCommand() == "7" || e.getActionCommand() == "8"
|| e.getActionCommand() == "9" || e.getActionCommand() == "0"){
if(str3 == ""){
str1+=e.getActionCommand();
shuru.setText(str1);
}else {
str2+=e.getActionCommand();
shuru.setText(str2);
}
}else if(e.getActionCommand() == "." ){ //.实现
if(str3 == ""){
if(str1.contains(".")){}
else {
if(str1 ==""){
str1+="0";
str1+=e.getActionCommand();
shuru.setText(str1);
}else{
str1+=e.getActionCommand();
shuru.setText(str1);
}
}
}
else{
if(str2.contains(".")){}
else{
if(str2 ==""){
str2+="0";
str2+=e.getActionCommand();
shuru.setText(str2);
}else{
str2+=e.getActionCommand();
shuru.setText(str2);
}
}
}
}else if(e.getActionCommand() == "+" || e.getActionCommand() == "-" //加减乘除实现
|| e.getActionCommand() == "*" || e.getActionCommand() == "/"){
str3=e.getActionCommand();
shuru.setText("");
}else if(e.getActionCommand() == "="){
value1=Double.parseDouble(str1);
value2=Double.parseDouble(str2);
if(str3 == "+"){resoult=value1+value2;}
if(str3 == "-"){resoult=value1-value2;}
if(str3 == "*"){resoult=value1*value2;}
if(str3 == "/"){resoult=value1/value2;}
str4=Double.toString(resoult);
str5=str1+str3+str2+"="+str4;
shuru.setText(str4);
xianshi.setText(str5);
str3="";
str1="";
str2="";
}
}
public static void main(String args[]) {
new JiSuanQi();
}
}
----------------------
ASP.Net+Unity开发.Net培训、期待与您交流! ----------------------