简陋的计算器(一次只能输入一个符号(+,-,*,\))

package calculator;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class myCalculator {
public static void main(String[] args) {
new calculatorJFrame2();
}
}
class calculatorJFrame2 extends JFrame implements ActionListener{
 //按钮
JButton bu1, bu2, bu3, bu4, bu5, bu6, bu7, bu8, bu9, bu10, bu11, bu12, bu13, bu14, bu15, bu16,bu17;
  //文本域
JTextField text;
//容器
  JPanel jp1,jp2,jp3,jp4;
  
  public void init(){
  //标题
  this.setTitle("简单的计算器");
  //位置
  this.setLocationRelativeTo(null);
  //窗体不可放大
  this.setResizable(false);
  //自动适应窗体大小
  this.pack();
  //关闭
  this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  //窗体可见
  this.setVisible(true);
  }
  public calculatorJFrame2(){
  //初始化按钮
  bu1=new JButton("7");
  bu2=new JButton("8");
  bu3=new JButton("9");
  bu4=new JButton("/");
  bu5=new JButton("4");
  bu6=new JButton("5");
  bu7=new JButton("6");
  bu8=new JButton("*");
  bu9=new JButton("1");
  bu10=new JButton("2");
  bu11=new JButton("3");
  bu12=new JButton("-");
  bu13=new JButton("0");
  bu14=new JButton(".");
  bu15=new JButton("=");
  bu16=new JButton("+");
  bu17=new JButton("清空");
 
  jp1=new JPanel();//实例化容器
  jp1.setLayout(new GridLayout(4,4,5,5));//网格布局,4行4列,每个格子间间隔5
  //把按钮放进容器中
  jp1.add(bu1);
  jp1.add(bu2);
  jp1.add(bu3);
  jp1.add(bu4);
  jp1.add(bu5);
  jp1.add(bu6);
  jp1.add(bu7);
  jp1.add(bu8);
  jp1.add(bu9);
  jp1.add(bu10);
  jp1.add(bu11);
  jp1.add(bu12);
  jp1.add(bu13);
  jp1.add(bu14);
  jp1.add(bu15);
  jp1.add(bu16);
 
  //实例化容器
  jp2=new JPanel();
  text=new JTextField(20);//实例化文本框,20列
  text.setHorizontalAlignment(JTextField.RIGHT);
  jp2.setLayout(new FlowLayout(new FlowLayout().RIGHT));//流式布局
  jp2.add(text);
 
  jp3=new JPanel();//实例化容器
  jp3.add(bu17);
 
  jp4=new JPanel();//实例化容器
  jp4.setLayout(new BorderLayout());//边界布局
  jp4.add(jp2,BorderLayout.NORTH);//把容器jp1,jp2,jp3加入容器jp4
  jp4.add(jp1,BorderLayout.CENTER);
  jp4.add(jp3,BorderLayout.SOUTH);
  this.getContentPane().add(jp4);//把jp4加入面板中
  init();//初始化面吧
 
  //绑定点击事件
  bu1.addActionListener(this);
  bu2.addActionListener(this);
  bu3.addActionListener(this);
  bu4.addActionListener(this);
  bu5.addActionListener(this);
  bu6.addActionListener(this);
  bu7.addActionListener(this);
  bu8.addActionListener(this);
  bu9.addActionListener(this);
  bu10.addActionListener(this);
  bu11.addActionListener(this);
  bu12.addActionListener(this);
  bu13.addActionListener(this);
  bu14.addActionListener(this);
  bu15.addActionListener(this);
  bu16.addActionListener(this);
  bu17.addActionListener(this);
  }
@Override//点击事件处理过程
public void actionPerformed(ActionEvent e) {
 String str=e.getActionCommand();
 if(str.equals("7")){
 String str1=text.getText();//赋值前选取得原有的值,在和新的值一起加入文本框中
 text.setText(str1+str);
 }else if(str.equals("8")){
 String str1=text.getText();
 text.setText(str1+str);
 }else if(str.equals("9")){
 String str1=text.getText();
 text.setText(str1+str);
 }else if(str.equals("/")){
 String str1=text.getText();
 text.setText(str1+str);
 }else if(str.equals("4")){
 String str1=text.getText();
 text.setText(str1+str);
 }else if(str.equals("5")){
 String str1=text.getText();
 text.setText(str1+str);
 }else if(str.equals("6")){
 String str1=text.getText();
 text.setText(str1+str);
 }else if(str.equals("*")){
 String str1=text.getText();
 text.setText(str1+str);
 }else if(str.equals("1")){
 String str1=text.getText();
 text.setText(str1+str);
 }else if(str.equals("2")){
 String str1=text.getText();
 text.setText(str1+str);
 }else if(str.equals("3")){
 String str1=text.getText();
 text.setText(str1+str);
 }else if(str.equals("-")){
 String str1=text.getText();
 text.setText(str1+str);
 }else if(str.equals("0")){
 String str1=text.getText();
 text.setText(str1+str);
 }else if(str.equals(".")){
 String str1=text.getText();
 text.setText(str1+str);
 }else if(str.equals("+")){
 String str1=text.getText();
 text.setText(str1+str);
 }else if(str.equals("清空")){
 text.setText("");
 }else if(str.equals("=")){
 /*String str1;
 str1=text.getText();//取得文本域的值
 char[] num=str1.toCharArray();//转化为字符数组
 char str2=' ';//单个字符,用来存储 符号(+,-,*,/)
 String num1="";//用来存储数字
 String num2="";
 String num3="";
 int j=1;
 for (int i = 0; i <num.length; i++) {
if(Character.isDigit(num[i])||num[i]=='.'){//如果是 数字 或 .  则连起来成字符串
num1=num1+num[i]; //第一个数字
}else{
str2=num[i];//符号(+,-,*,/)
num2=num1;
num1="";
}
}
 num3=num1; //第二个数字*/
 String textContent=text.getText();
 String[] add=textContent.split("\\+");
 String[] subtract=textContent.split("\\-");
 String[] mul=textContent.split("\\*");
 String[] divi=textContent.split("\\/");
 
if(add.length==2) {
double frontNum=Double.valueOf(add[0]);
double backNum=Double.valueOf(add[1]);
double result=frontNum+backNum;
text.setText(String.valueOf(result));
     }else if(subtract.length==2) {
double frontNum=Double.valueOf(subtract[0]);
double backNum=Double.valueOf(subtract[1]);
double result=frontNum-backNum;
text.setText(String.valueOf(result));
}else if(mul.length==2) {
double frontNum=Double.valueOf(mul[0]);
double backNum=Double.valueOf(mul[1]);
double result=frontNum*backNum;
text.setText(String.valueOf(result));
}else if(divi.length==2) {
double frontNum=Double.valueOf(divi[0]);
double backNum=Double.valueOf(divi[1]);
double result=frontNum/backNum;
text.setText(String.valueOf(result));
}
 
 }
}

}


猜你喜欢

转载自blog.csdn.net/weixin_42044486/article/details/80240603