重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
不仅可以加减还可以乘除,代码如下:
创新互联建站是一家集网站建设,七台河企业网站建设,七台河品牌网站建设,网站定制,七台河网站建设报价,网络营销,网络优化,七台河网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
package 计算器;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator extends JFrame implements ActionListener {
boolean flag = false;
JTextField jtf = new JTextField();
public static void main(String[] args) {
new Calculator();
}
public Calculator() {
setSize(300, 300);
setTitle("计算器");
Container c = getContentPane();
jtf.setHorizontalAlignment(JTextField.RIGHT);
JButton jb = new JButton("=");
jb.addActionListener(this);
JPanel jp = new JPanel();
c.add(jtf, BorderLayout.NORTH);
c.add(jp, BorderLayout.CENTER);
c.add(jb, BorderLayout.SOUTH);
jp.setLayout(new GridLayout(4, 4));
addButton(jp);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
}
public void addButton(JPanel jp) {
JButton b = null;
b = new JButton("1");
b.addActionListener(this);
jp.add(b);
b = new JButton("2");
b.addActionListener(this);
jp.add(b);
b = new JButton("3");
b.addActionListener(this);
jp.add(b);
b = new JButton("+");
b.addActionListener(this);
jp.add(b);
b = new JButton("4");
b.addActionListener(this);
jp.add(b);
b = new JButton("5");
b.addActionListener(this);
jp.add(b);
b = new JButton("6");
b.addActionListener(this);
jp.add(b);
b = new JButton("-");
b.addActionListener(this);
jp.add(b);
b = new JButton("7");
b.addActionListener(this);
jp.add(b);
b = new JButton("8");
b.addActionListener(this);
jp.add(b);
b = new JButton("9");
b.addActionListener(this);
jp.add(b);
b = new JButton("*");
b.addActionListener(this);
jp.add(b);
b = new JButton("0");
b.addActionListener(this);
jp.add(b);
b = new JButton(".");
b.addActionListener(this);
jp.add(b);
b = new JButton("C");
b.addActionListener(this);
jp.add(b);
b = new JButton("/");
b.addActionListener(this);
jp.add(b);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "=") {
if (jtf.getText().matches("\\d+\\.?\\d*[\\+\\-\\*\\/]\\d+\\.?\\d*")) {
String s = jtf.getText();
Pattern p = Pattern.compile("\\d+\\.?\\d*");
Matcher m = p.matcher(s);
m.find();
double firstNum = Double.parseDouble(m.group());
m.find();
double secondNum = Double.parseDouble(m.group());
if (jtf.getText().matches("\\d+\\.?\\d*\\+\\d+\\.?\\d*")) {
jtf.setText("" + (firstNum + secondNum));
} else if (jtf.getText().matches("\\d+\\.?\\d*\\-\\d+\\.?\\d*")) {
jtf.setText("" + (firstNum - secondNum));
} else if (jtf.getText().matches("\\d+\\.?\\d*\\*\\d+\\.?\\d*")) {
jtf.setText("" + (firstNum * secondNum));
} else if (jtf.getText().matches("\\d+\\.?\\d*\\/\\d+\\.?\\d*")) {
jtf.setText("" + (firstNum / secondNum));
}
} else {
JOptionPane.showMessageDialog(null, "请输入正确表达式!");
}
flag = true;
} else if (e.getActionCommand() == "C") {
jtf.setText("");
} else {
if (flag) {
if (e.getActionCommand() != "+" e.getActionCommand() != "-"
e.getActionCommand() != "*"
e.getActionCommand() != "/") {
//System.out.println(e.getActionCommand() != "+");
jtf.setText("");
}
flag = false;
}
jtf.setText(jtf.getText() + e.getActionCommand());
}
}
}
下面文件名要为:JiSuanQi.java
import java.awt.*;
import java.awt.event.*;
public class JiSuanQi
{
String s="",s1=null,s2=null;
Frame f=new Frame("计算器");
TextField tf=new TextField(30);
Panel p1=new Panel();
Panel p2=new Panel();
Panel p3=new Panel();
Button bt1=new Button("=");
Button bt2=new Button("删除");
Button[] bt=new Button[16];
int id=0;
public static void main(String[] args)
{
new JiSuanQi().init();
}
public void init()
{
f.setBackground(new Color(85,247,253));
f.setLayout(new BorderLayout(4,4));
p2.setLayout(new GridLayout(4,4,4,4));
p3.setLayout(new BorderLayout(4,4));
f.setResizable(false);
f.add(p1,BorderLayout.NORTH);
f.add(p2);
p3.add(bt2,BorderLayout.NORTH);
p3.add(bt1);
p1.add(tf);
f.add(p3,BorderLayout.EAST);
String[] b={"1","2","3","+","4","5","6","-","7","8","9","*","0",".","复位","/"};
for(int i=0;i16;i++)
{
bt[i]=new Button(b[i]);
p2.add(bt[i]);
}
bt[0].setForeground(Color.blue);
bt[1].setForeground(Color.blue);
bt[2].setForeground(Color.blue);
bt[4].setForeground(Color.blue);
bt[5].setForeground(Color.blue);
bt[6].setForeground(Color.blue);
bt[8].setForeground(Color.blue);
bt[9].setForeground(Color.blue);
bt[10].setForeground(Color.blue);
bt[12].setForeground(Color.blue);
bt[13].setForeground(Color.blue);
bt[3].setForeground(Color.red);
bt[7].setForeground(Color.red);
bt[11].setForeground(Color.red);
bt[15].setForeground(Color.red);
bt[14].setForeground(Color.red);
bt1.setForeground(Color.red);
bt2.setForeground(Color.red);
f.pack();
f.setVisible(true);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
bt[0].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
s+=1;
s2+=1;
tf.setText(s);
}
}
);
bt[1].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
s+=2;
s2+=2;
tf.setText(s);
}
}
);
bt[2].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
s+=3;
s2+=3;
tf.setText(s);
}
}
);
bt[4].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
s+=4;
s2+=4;
tf.setText(s);
}
}
);
bt[5].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
s+=5;
s2+=5;
tf.setText(s);
}
}
);
bt[6].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
s+=6;
s2+=6;
tf.setText(s);
}
}
);
bt[8].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
s+=7;
s2+=7;
tf.setText(s);
}
}
);
bt[9].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
s+=8;
s2+=8;
tf.setText(s);
}
}
);
bt[10].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
s+=9;
s2+=9;
tf.setText(s);
}
}
);
bt[12].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
s+=0;
s2+=0;
tf.setText(s);
}
}
);
bt[13].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
s+='.';
s2+='.';
tf.setText(s);
}
}
);
bt[3].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
s1=s;
s+='+';
id=1;
s2="";
tf.setText(s);
}
}
);
bt[7].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
s1=s;
s+='-';
id=2;
s2="";
tf.setText(s);
}
}
);
bt[11].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
s1=s;
s+='*';
id=3;
s2="";
tf.setText(s);
}
}
);
bt[14].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
s="";
s2="";
tf.setText(s);
}
}
);
bt[15].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
s1=s;
s+='/';
id=4;
s2="";
tf.setText(s);
}
}
);
bt1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(id1) ;
else{
s+='=';
double a=Double.parseDouble(s1);
double b=Double.parseDouble(s2);
double c=0;
switch(id)
{
case 1:c=a+b; break;
case 2:c=a-b; break;
case 3:c=a*b; break;
case 4:c=a/b; break;
}
s+=c;
tf.setText(s);
}
s="";s1="";s2="";id=0;
}
}
);
bt2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
char[] c1;
char[] c2=new char[s.length()-1];
c1=s.toCharArray();
for(int i=0;is.length()-1;i++)
c2[i]=c1[i];
s=s.valueOf(c2);
if(id1)
{
s1=s;
}
if(s2.length()=1)
{
char[] c3;
char[] c4=new char[s2.length()-1];
c3=s2.toCharArray();
for(int i=0;is2.length()-1;i++)
c4[i]=c3[i];
s2=s2.valueOf(c4);
}
tf.setText(s);
}
}
);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.LinkedList; //工具包
import java.text.NumberFormat; //文本包
/**
* java swing计算器
* @author young
*
*/
public class Calculator extends Frame implements ActionListener // 计算器类
{
JTextField result;
NumberButton numberButton[];
OperatorButton operatorButton[];
Button radixpoint, positiveminus, backspace, reciprocal, equal, clear; // 声明成员变量
// 小数点按钮,正负号按钮,退格按钮,求倒数按钮,等号按钮,清零按钮
Panel panel;
String operator[] = { "+", "-", "*", "/" };
LinkedList linklist;
boolean pressequal = false;
public Calculator() // 构造方法
{
super("计算器");
linklist = new LinkedList();
numberButton = new NumberButton[10];
for (int i = 0; i = 9; i++) {
numberButton[i] = new NumberButton(i);
numberButton[i].addActionListener(this);
}
operatorButton = new OperatorButton[4];
for (int i = 0; i 4; i++) {
operatorButton[i] = new OperatorButton(operator[i]);
operatorButton[i].addActionListener(this);
}
radixpoint = new Button(".");
positiveminus = new Button("+/-");
backspace = new Button("CE");
reciprocal = new Button("1/x");
equal = new Button("=");
clear = new Button("C");
radixpoint.setForeground(Color.red);
positiveminus.setForeground(Color.red);
backspace.setForeground(Color.red);
reciprocal.setForeground(Color.red);
equal.setForeground(Color.red);
clear.setForeground(Color.red);
radixpoint.addActionListener(this);
positiveminus.addActionListener(this);
backspace.addActionListener(this);
reciprocal.addActionListener(this);
equal.addActionListener(this);
clear.addActionListener(this);
result = new JTextField(10);
result.setHorizontalAlignment(JTextField.RIGHT);
result.setForeground(Color.black);
result.setBackground(Color.white);
result.setFont(new Font("TimesRoman", Font.PLAIN, 14));
result.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
result.setEditable(false);
panel = new Panel();
panel.setLayout(new GridLayout(4, 5));
panel.add(numberButton[1]);
panel.add(numberButton[2]);
panel.add(numberButton[3]);
panel.add(backspace);
panel.add(clear);
panel.add(numberButton[4]);
panel.add(numberButton[5]);
panel.add(numberButton[6]);
panel.add(operatorButton[0]);
panel.add(operatorButton[2]);
panel.add(numberButton[7]);
panel.add(numberButton[8]);
panel.add(numberButton[9]);
panel.add(operatorButton[1]);
panel.add(operatorButton[3]);
panel.add(numberButton[0]);
panel.add(positiveminus);
panel.add(reciprocal);
panel.add(radixpoint);
panel.add(equal);
add(result, "North");
add(panel, "Center");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setSize(270, 200);
setLocation(300, 230);
setVisible(true);
}
public void actionPerformed(ActionEvent e) // 按钮的单击事件处理方法
{
if (e.getSource() instanceof NumberButton) // 数字按钮
{
NumberButton b = (NumberButton) e.getSource();
if (linklist.size() == 0) {
int number = b.getNumber();
linklist.add("" + number);
result.setText("" + number);
pressequal = false;
}
else if (linklist.size() == 1 pressequal == false) {
int number = b.getNumber();
String num = (String) linklist.getFirst();
String s = num.concat("" + number);
linklist.set(0, s);
result.setText(s);
} else if (linklist.size() == 1 pressequal == true) {
int number = b.getNumber();
linklist.removeFirst();
linklist.add("" + number);
pressequal = false;
result.setText("" + number);
} else if (linklist.size() == 2) {
int number = b.getNumber();
linklist.add("" + number);
result.setText("" + number);
} else if (linklist.size() == 3) {
int number = b.getNumber();
String num = (String) linklist.getLast();
String s = num.concat("" + number);
linklist.set(2, s);
result.setText(s);
}
} else if (e.getSource() instanceof OperatorButton) // 操作按钮
{
OperatorButton b = (OperatorButton) e.getSource();
if (linklist.size() == 1) {
String fuhao = b.getOperator();
linklist.add(fuhao);
} else if (linklist.size() == 2) {
String fuhao = b.getOperator();
linklist.set(1, fuhao);
} else if (linklist.size() == 3) {
String fuhao = b.getOperator();
String number1 = (String) linklist.getFirst();
String number2 = (String) linklist.getLast();
String operator = (String) linklist.get(1);
try {
double n1 = Double.parseDouble(number1);
double n2 = Double.parseDouble(number2);
double n = 0;
if (operator.equals("+")) {
n = n1 + n2;
} else if (operator.equals("-")) {
n = n1 - n2;
} else if (operator.equals("*")) {
n = n1 * n2;
} else if (operator.equals("/")) {
n = n1 / n2;
}
linklist.clear();
linklist.add("" + n);
linklist.add(fuhao);
result.setText("" + n);
} catch (Exception ee) {
}
}
} else if (e.getSource() == equal) // 等号按钮
{
pressequal = true;
if (linklist.size() == 1 || linklist.size() == 2) {
String num = (String) linklist.getFirst();
result.setText("" + num);
} else if (linklist.size() == 3) {
String number1 = (String) linklist.getFirst();
String number2 = (String) linklist.getLast();
String operator = (String) linklist.get(1);
try {
double n1 = Double.parseDouble(number1);
double n2 = Double.parseDouble(number2);
double n = 0;
if (operator.equals("+")) {
n = n1 + n2;
} else if (operator.equals("-")) {
n = n1 - n2;
} else if (operator.equals("*")) {
n = n1 * n2;
} else if (operator.equals("/")) {
n = n1 / n2;
}
result.setText("" + n);
linklist.set(0, "" + n);
linklist.removeLast();
linklist.removeLast();
} catch (Exception ee) {
}
}
} else if (e.getSource() == radixpoint) // 小数点按钮
{
if (linklist.size() == 0) {
pressequal = false;
} else if (linklist.size() == 1) {
String dot = radixpoint.getLabel();
String num = (String) linklist.getFirst();
String s = null;
if (num.indexOf(dot) == -1) {
s = num.concat(dot);
linklist.set(0, s);
} else {
s = num;
}
linklist.set(0, s);
result.setText(s);
}
else if (linklist.size() == 3) {
String dot = radixpoint.getLabel();
String num = (String) linklist.getLast();
String s = null;
if (num.indexOf(dot) == -1) {
s = num.concat(dot);
linklist.set(2, s);
} else {
s = num;
}
result.setText(s);
}
} else if (e.getSource() == backspace) // 退格按钮
{
if (linklist.size() == 1) {
String num = (String) linklist.getFirst();
if (num.length() = 1) {
num = num.substring(0, num.length() - 1);
linklist.set(0, num);
result.setText(num);
} else {
linklist.removeLast();
result.setText("0");
}
} else if (linklist.size() == 3) {
String num = (String) linklist.getLast();
if (num.length() = 1) {
num = num.substring(0, num.length() - 1);
linklist.set(2, num);
result.setText(num);
} else {
linklist.removeLast();
result.setText("0");
}
}
} else if (e.getSource() == positiveminus) // 正负号按钮
{
if (linklist.size() == 1) {
String number1 = (String) linklist.getFirst();
try {
double d = Double.parseDouble(number1);
d = -1 * d;
String str = String.valueOf(d);
linklist.set(0, str);
result.setText(str);
} catch (Exception ee) {
}
} else if (linklist.size() == 3) {
String number2 = (String) linklist.getLast();
try {
double d = Double.parseDouble(number2);
d = -1 * d;
String str = String.valueOf(d);
linklist.set(2, str);
result.setText(str);
} catch (Exception ee) {
}
}
} else if (e.getSource() == reciprocal) // 求倒数按钮
{
if (linklist.size() == 1 || linklist.size() == 2) {
String number1 = (String) linklist.getFirst();
try {
double d = Double.parseDouble(number1);
d = 1.0 / d;
String str = String.valueOf(d);
linklist.set(0, str);
result.setText(str);
} catch (Exception ee) {
}
} else if (linklist.size() == 3) {
String number2 = (String) linklist.getLast();
try {
double d = Double.parseDouble(number2);
d = 1.0 / d;
String str = String.valueOf(d);
linklist.set(0, str);
result.setText(str);
} catch (Exception ee) {
}
}
} else if (e.getSource() == clear) // 清零按钮
{
pressequal = false;
result.setText("0");
linklist.clear();
}
}
public static void main(String args[]) {
new Calculator();
}
}
class NumberButton extends Button // 数字按钮类
{
int number;
public NumberButton(int number) // 构造方法
{
super("" + number);
this.number = number;
setForeground(Color.blue);
}
public int getNumber() {
return number;
}
}
class OperatorButton extends Button // 运算符号按钮类
{
String operator;
public OperatorButton(String operator) // 构造方法
{
super(operator);
this.operator = operator;
setForeground(Color.red);
}
public String getOperator() {
return operator;
}
}
导包:import java.util.Scanner;
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("请输入第一个数字:");
int one=input.nextInt();
System.out.println("请输入符号:");
String fuhao=input.next();
System.out.println("请输入第二个数字:");
int two=input.nextInt();
if(fuhao.equals("+")){
System.out.println("结果:"+(one+two));
}else if(fuhao.equals("-")){
System.out.println("结果:"+(one-two));
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Vector;
public class calculator
{
String str1="0"; //运算数1 初值一定为0 为了程序的安全
String str2="0"; //运算数2
String fh="+"; //运算符
String jg="";//结果
//状态开关 重要
int k1=1;//开关1 用于选择输入方向 将要写入str2或 str2
int k2=1;//开关2 符号键 次数 k21说明进行的是2+3-9+8 这样的多符号运算
int k3=1;//开关3 str1 是否可以被清0 ==1时可以 !=1时不能被清0
int k4=1;//开关4 str2 同上
int k5=1;//开关5 控制小数点可否被录入 ==1时可以 !=1 输入的小数点被丢掉
JButton jicunqi; //寄存器 记录 是否连续按下符号键
Vector vt=new Vector(20,10);
JFrame frame=new JFrame("sunshine---计算器");
JTextField jg_TextField=new JTextField(jg,20);//20列
JButton clear_Button=new JButton("清除");
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 button_Dian=new JButton(".");
JButton button_jia=new JButton("+");
JButton button_jian=new JButton("-");
JButton button_cheng=new JButton("*");
JButton button_chu=new JButton("/");
JButton button_dy=new JButton("=");
public static void main(String[] args)
{
calculator calculator=new calculator();
}
calculator()
{
jg_TextField.setHorizontalAlignment(JTextField.RIGHT );//文本框 右对齐
JPanel pan=new JPanel();
pan.setLayout(new GridLayout(4,4,5,5));//四行四列 边距为5像素
pan.add(button7);
pan.add(button8);
pan.add(button9);
pan.add(button_chu);
pan.add(button4);
pan.add(button5);
pan.add(button6);
pan.add(button_cheng);
pan.add(button1);
pan.add(button2);
pan.add(button3);
pan.add(button_jian);
pan.add(button0);
pan.add(button_Dian);
pan.add(button_dy);
pan.add(button_jia);
pan.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));//pan对象的边距
JPanel pan2=new JPanel();
pan2.add(jg_TextField);
JPanel pan3=new JPanel(); //为什么要 多此一句呢? 因为我不会设置 按钮的大小
pan3.setLayout(new FlowLayout());
pan3.add(clear_Button);
//clear_Button.setSize(10,10);//设置清零按钮的大小 吗的 不好使 !!
frame.setLocation(300, 200); //主窗口 出现在位置
frame.setResizable(false); //不能调大小
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(pan2,BorderLayout.NORTH);
frame.getContentPane().add(pan,BorderLayout.CENTER);
frame.getContentPane().add(pan3,BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
//以上是 控件 和 布局
//下面是事件处理 程 序
//--------------- 数 字 键 ----------------
class JianTing implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String ss=((JButton)e.getSource()).getText();
jicunqi=(JButton)e.getSource();
vt.add(jicunqi);
if (k1==1)
{
if(k3==1)
{
str1="";
k5=1;//还原开关k5状态
}
str1=str1+ss;
//k2=1;
k3=k3+1;
//System.out.println(str1);
jg_TextField.setText(str1);//显示
}
else if(k1==2)
{
if (k4==1)
{
str2="";
k5=1; //还原开关k5状态
}
str2=str2+ss;
//k2=2;
k4=k4+1;
///////////////测试////////////////
jg_TextField.setText(str2);
}
}
}
//--------符 号-----------
class JianTing_fh implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String ss2=((JButton)e.getSource()).getText();
jicunqi=(JButton)e.getSource();
vt.add(jicunqi);
if(k2==1)
{
k1=2;//开关 k1 为1时,向数1写 为2时,向数2写
k5=1;
fh=ss2;
k2=k2+1;//按符号键的次数
}
else
{
int a=vt.size();
JButton c=(JButton)vt.get(a-2); if(!(c.getText().equals("+"))!(c.getText().equals("-"))!(c.getText().equals("*"))!(c.getText().equals("/")))
{
yuns();
str1=jg;
k1=2;//开关 k1 为1时,向数1写 为2时,向数2写
k5=1;
k4=1;
fh=ss2;
} k2=k2+1;
}
}
}
//--------清除-------
class JianTing_clear implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
jicunqi=(JButton)e.getSource();
vt.add(jicunqi);
k5=1;
k2=1;
k1=1;
k3=1;
k4=1;
str1="0";
str2="0";
fh="";
jg="";
jg_TextField.setText(jg);
vt.clear();
}
}
//----------------等 于 ---------------------
class JianTing_dy implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
jicunqi=(JButton)e.getSource();
vt.add(jicunqi);
yuns();
k1=1; //还原开关k1状态
//str1=jg;
k2=1;
k3=1;//还原开关k3状态
k4=1; //还原开关k4状态
str1=jg; //为7+5=12 +5=17 这种计算做准备
}
}
//----------------小数点 ---------------------
class JianTing_xiaos implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
jicunqi=(JButton)e.getSource();
vt.add(jicunqi);
if(k5==1)
{
String ss2=((JButton)e.getSource()).getText();
if (k1==1)
{
if(k3==1)
{
str1="";
k5=1; //还原开关k5状态
}
str1=str1+ss2;
//k2=1;
k3=k3+1;
//System.out.println(str1);
jg_TextField.setText(str1);//显示
}
else if(k1==2)
{
if (k4==1)
{
str2="";
k5=1; //还原开关k5状态
}
str2=str2+ss2;
//k2=2;
k4=k4+1;
///////////////测试////////////////
jg_TextField.setText(str2);
}
}
k5=k5+1;
}
}
//注册 监听器
JianTing_dy jt_dy=new JianTing_dy();
JianTing jt= new JianTing();//临听数字键
JianTing_fh jt_fh= new JianTing_fh();//临 听符 号键
JianTing_clear jt_c=new JianTing_clear(); //清除键
JianTing_xiaos jt_xs=new JianTing_xiaos();// 小数点 键
button7.addActionListener(jt);
button8.addActionListener(jt);
button9.addActionListener(jt);
button_chu.addActionListener(jt_fh);
button4.addActionListener(jt);
button5.addActionListener(jt);
button6.addActionListener(jt);
button_cheng.addActionListener(jt_fh);
button1.addActionListener(jt);
button2.addActionListener(jt);
button3.addActionListener(jt);
button_jian.addActionListener(jt_fh);
button0.addActionListener(jt);
button_Dian.addActionListener(jt_xs);
button_dy.addActionListener(jt_dy);
button_jia.addActionListener(jt_fh);
clear_Button.addActionListener(jt_c);
//关闭事件处理程序
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
//---------------计 算------------------
public void yuns()
{
double a2,b2;//运算数1,2
String c=fh;// 运算符
double jg2=0 ;//结果
if (c.equals(""))
{
//System.out.println("请输入运算符");
jg_TextField.setText("请输入运算符");
}
else
{
System.out.println("str1:"+str1);//调试时 使 用
System.out.println("str2:"+str2);//调试时 使 用
System.out.println("运算符:"+fh);//调试时 使 用
if (str1.equals(".")) //字符串 "." 转换成double型数据时 会出错 所以手工转
str1="0.0";
if (str2.equals("."))
str2="0.0";
a2=Double.valueOf(str1).doubleValue();
b2=Double.valueOf(str2).doubleValue();
System.out.println("double型的a2:"+a2); //调试时 使 用
System.out.println("double型的b2:"+b2); //调试时 使 用
if (c.equals("+"))
{
jg2=a2+b2;
}
if (c.equals("-"))
{
jg2=a2-b2;
}
if (c.equals("*"))
{
jg2=a2*b2;
}
if (c.equals("/"))
{
if(b2==0)
{
jg2=0;//0000000000000 by 0 cu!
}
else
{
jg2=a2/b2;
}
}
System.out.println("double型a2"+fh+"b2结果:"+jg2);
System.out.println();
jg=((new Double(jg2)).toString());
jg_TextField.setText(jg);
}
}
}