九.计算器

Counter.java

package cn.jiao.counter;


import java.awt.FlowLayout;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

//登陆界面开发,让Counter类继承JFrame类
public class Counter extends JFrame {

// 主函数
public static void main(String[] args) {

// 实例化Counter累的对象
Counter counter = new Counter();
// 调用初始化计算其界面的方法
counter.init();

}

// 初始化界面的方法
public void init() {

this.setTitle("计算器2011");
this.setSize(200, 200);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(3);

// 设置窗体的布局为流式布局
this.setLayout(new FlowLayout());

// 实例化一个图标对象
ImageIcon image = new ImageIcon("counter.jpg");
// 实例化一个标签对象,并且将图标对象作为参数
JLabel lblImage = new JLabel(image);
// 将标签对象添加到窗体上
this.add(lblImage);

// 实例化一个文本输入框对象
JTextField txtNumber = new JTextField(15);
//设置输入框的默认值
txtNumber.setText("0.");
//禁止窗体编辑
txtNumber.setEditable(false);
//让文本框中的内容右对齐
txtNumber.setHorizontalAlignment(JTextField.RIGHT);
// 将输入框对象添加到窗体上
this.add(txtNumber);

// 实例化一个按钮对象
JButton button1 = new JButton("1");
// 将按钮对象添加到窗体上
this.add(button1);

// 实例化一个按钮对象
JButton button2 = new JButton("2");
// 将按钮对象添加到窗体上
this.add(button2);

// 实例化一个按钮对象
JButton button3 = new JButton("3");
// 将按钮对象添加到窗体上
this.add(button3);

// 实例化一个按钮对象
JButton button4 = new JButton("4");
// 将按钮对象添加到窗体上
this.add(button4);

// 实例化一个按钮对象
JButton button5 = new JButton("5");
// 将按钮对象添加到窗体上
this.add(button5);

// 实例化一个按钮对象
JButton button6 = new JButton("6");
// 将按钮对象添加到窗体上
this.add(button6);

// 实例化一个按钮对象
JButton button7 = new JButton("7");
// 将按钮对象添加到窗体上
this.add(button7);

// 实例化一个按钮对象
JButton button8 = new JButton("8");
// 将按钮对象添加到窗体上
this.add(button8);

// 实例化一个按钮对象
JButton button9 = new JButton("9");
// 将按钮对象添加到窗体上
this.add(button9);

// 实例化一个按钮对象
JButton button0 = new JButton("0");
// 将按钮对象添加到窗体上
this.add(button0);

// 实例化一个按钮对象
JButton buttonA = new JButton("+");
// 将按钮对象添加到窗体上
this.add(buttonA);

// 实例化一个按钮对象
JButton buttonB = new javax.swing.JButton("-");
// 将按钮对象添加到窗体上
this.add(buttonB);

// 实例化一个按钮对象
javax.swing.JButton buttonC = new javax.swing.JButton("*");
// 将按钮对象添加到窗体上
this.add(buttonC);

// 实例化一个按钮对象
javax.swing.JButton buttonD = new javax.swing.JButton("/");
// 将按钮对象添加到窗体上
this.add(buttonD);

// 实例化一个按钮对象
javax.swing.JButton buttonM = new javax.swing.JButton("=");
// 将按钮对象添加到窗体上
this.add(buttonM);

// 实例化一个按钮对象
javax.swing.JButton buttonN = new javax.swing.JButton("ON");
// 将按钮对象添加到窗体上
this.add(buttonN);


//实例化一个事件处理者对象
CounterListener cl = new CounterListener(txtNumber);
//登录按钮是事件源,给事件源添加一个监听动作事件的方法
button1.addActionListener(cl);
button2.addActionListener(cl);
button3.addActionListener(cl);
button4.addActionListener(cl);
button5.addActionListener(cl);
button6.addActionListener(cl);
button7.addActionListener(cl);
button8.addActionListener(cl);
button9.addActionListener(cl);
button0.addActionListener(cl);
buttonA.addActionListener(cl);
buttonB.addActionListener(cl);
buttonC.addActionListener(cl);
buttonD.addActionListener(cl);
buttonM.addActionListener(cl);
buttonN.addActionListener(cl);

// 设置窗体可见
this.setVisible(true);
}

}


CounterListener.java

package cn.jiao.counter;



import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JTextField;

public class CounterListener implements ActionListener {

private JTextField txtNumber;
private String[]array = new String[4];
private int i,count,jia,jian,cheng,chu;


public CounterListener(JTextField txtNumber) {
this.txtNumber = txtNumber;
}

public void actionPerformed(ActionEvent e) {
System.out.print(e.getActionCommand());
String value = e.getActionCommand();
//如果点击的是ON按钮,那么将所有的信息全部还原到最初始化的位置
if(value.equals("ON")){
//开始还原到初始位置
txtNumber.setText("0.");
for(int i=0;i<array.length;i++){
array[i]="";
}
}
else if(value.equals("+")){
i=0;
//判断是否等于"",如果是空,则表示数组中还没有数据
if(array[1]==null||array[1].equals("")){
//存储之前的数据
array[0] = txtNumber.getText();
//存储当前的运算符号
array[1] = value;
}
else{
int temp = Integer.parseInt(txtNumber.getText());
int temp2 = Integer.parseInt(array[0]);
String str = array[1];
if(str.equals("+")){
int count = temp + temp2;
txtNumber.setText(count+"");
array[0] = count+"";

}
else if(str.equals("*")){
int count = temp * temp2;
txtNumber.setText(count+"");
array[0] =count+"";
}
else if(str.equals("-")){
int count = temp2 - temp;
txtNumber.setText(count+"");
array[0] = count+"";
}
else if(str.equals("/")){
int count = temp2 / temp;
txtNumber.setText(count+"");
array[0] = count+"";
}
array[1] = value;
}
}
else if(value.equals("-")){
i=0;
//判断是否等于"",如果是空,则表示数组中还没有数据
if(array[1]==null||array[1].equals("")){
//存储之前的数据
array[0] = txtNumber.getText();
//存储当前的运算符号
array[1] = value;
}
else{
int temp = Integer.parseInt(txtNumber.getText());
int temp2 = Integer.parseInt(array[0]);
String str = array[1];
if(str.equals("+")){
int count = temp + temp2;
txtNumber.setText(count+"");
array[0] = count+"";

}
else if(str.equals("*")){
int count = temp * temp2;
txtNumber.setText(count+"");
array[0] = count+"";
}
else if(str.equals("-")){
int count = temp2 - temp;
txtNumber.setText(count+"");
array[0] = count+"";
}
else if(str.equals("/")){
int count = temp2 / temp;
txtNumber.setText(count+"");
array[0] = count+"";
}
array[1] = value;
}

}
else if(value.equals("*")){
i=0;
//判断是否等于"",如果是空,则表示数组中还没有数据
if(array[1]==null||array[1].equals("")){
//存储之前的数据
array[0] = txtNumber.getText();
//存储当前的运算符号
array[1] = value;
}
else{
int temp = Integer.parseInt(txtNumber.getText());
int temp2 = Integer.parseInt(array[0]);
String str = array[1];
if(str.equals("+")){
int count = temp + temp2;
txtNumber.setText(count+"");
array[0] = count+"";

}
else if(str.equals("*")){
int count = temp * temp2;
txtNumber.setText(count+"");
array[0] = count+"";
}
else if(str.equals("-")){
int count= temp2 - temp;
txtNumber.setText(count+"");
array[0] = count+"";
}
else if(str.equals("/")){
int count = temp2 / temp;
txtNumber.setText(count+"");
array[0] = count+"";
}
array[1] = value;
}

}
else if(value.equals("/")){
i=0;
//判断是否等于"",如果是空,则表示数组中还没有数据
if(array[1]==null||array[1].equals("")){
//存储之前的数据
array[0] = txtNumber.getText();
//存储当前的运算符号
array[1] = value;
}
else{
int temp = Integer.parseInt(txtNumber.getText());
int temp2 = Integer.parseInt(array[0]);
String str = array[1];
if(str.equals("+")){
int count = temp + temp2;
txtNumber.setText(count+"");
array[0] = count+"";

}
else if(str.equals("*")){
int count = temp * temp2;
txtNumber.setText(count+"");
array[0] = count+"";
}
else if(str.equals("-")){
int count = temp2 - temp;
txtNumber.setText(count+"");
array[0] = count+"";
}
else if(str.equals("/")){
int count = temp2 / temp;
txtNumber.setText(count+"");
array[0] = count+"";
}
array[1] = value;
}

}
else if(value.equals("=")){
i=0;
//判断是否等于"",如果是空,则表示数组中还没有数据
if(array[1]==null||array[1].equals("")){
//存储之前的数据
array[0] = txtNumber.getText();
//存储当前的运算符号
array[1] = value;
}
else{
int temp = Integer.parseInt(txtNumber.getText());
int temp2 = Integer.parseInt(array[0]);
String str = array[1];
if(str.equals("+")){
int count = temp + temp2;
txtNumber.setText(count+"");
array[0] = count+"";

}
else if(str.equals("*")){
int count = temp * temp2;
txtNumber.setText(count+"");
array[0] = count+"";
}
else if(str.equals("-")){
int count = temp2 - temp;
txtNumber.setText(count+"");
array[0] = count+"";
}
else if(str.equals("/")){
int count = temp2 / temp;
txtNumber.setText(count+"");
array[0] = count+"";
}
array[1] = value;
}

}
else{
if(i==0){
txtNumber.setText(value);
i++;
}
else{
txtNumber.setText(txtNumber.getText()+value);
}

}
}

}

你可能感兴趣的:(java)