简单的小计算器

简单的计算器

package start;

import util.Const;

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

public class Caculator extends JFrame implements ActionListener {
   //北面控件
   private JPanel jp_north=new JPanel();
   private JTextField input_text=new JTextField();
   private JButton c_Btn=new JButton("C");
   //中间控件
   private JPanel jp_center=new JPanel();

   //基础框体
   public Caculator() throws HeadlessException{
       this.init();
       this.addNorthComponent();
       this.addCentorButton();
  }
   //基础框体
   public void init(){
       this.setTitle(Const.TITLE);//设置标题
       this.setSize(Const.FRAME_W,Const.FRAME_H);//设置大小
       this.setLayout(new BorderLayout());//边框布局
       this.setResizable(false);//窗体不可由用户调整大小
       this.setLocation(Const.FRAME_X,Const.FRAME_Y);
       this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//可以关闭
  }
   //北面控件 文本框和按钮
   public void addNorthComponent(){
       this.input_text.setPreferredSize(new Dimension(230,30));
       jp_north.add(input_text);
       jp_north.add(c_Btn);
       this.c_Btn.setForeground(Color.RED);

       c_Btn.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
               input_text.setText("");
          }
      });
       this.add(jp_north,BorderLayout.NORTH);
  }
   //中间按钮
   public void addCentorButton(){
       String btn_text="123+456-789*0.=/";
       String regex="[\\+\\-*/.=]";
       this.jp_center.setLayout(new GridLayout(4,4));
       for (int i = 0; i < 16; i++) {
           String temp=btn_text.substring(i,i+1);
           JButton btn=new JButton();
           btn.setText(temp);
           if(temp.matches(regex)){
               btn.setFont(new Font("粗体",Font.BOLD,16));
               btn.setForeground(Color.RED);
          }
           /*if(temp.equals("+")||
                   temp.equals("-")||
                   temp.equals("*")||
                   temp.equals("/")||
                   temp.equals("=")||
                   temp.equals(".")){
               btn.setFont(new Font("粗体",Font.BOLD,16));
               btn.setForeground(Color.RED);
           }*/
           btn.addActionListener(this);
           jp_center.add(btn);
      }
       this.add(jp_center,BorderLayout.CENTER);
  }
   public static void main(String[] args) {
       Caculator caculator = new Caculator();
       caculator.setVisible(true);
  }
   private String firstInput=null;
   private String operator=null;
   //给中间的按钮添上动作
   @Override
   public void actionPerformed(ActionEvent e) {
       String clickStr=e.getActionCommand();
       if(".0123456789".indexOf(clickStr)!=-1){
           this.input_text.setText(input_text.getText()+clickStr);//把点击的数字累计显示
           this.input_text.setHorizontalAlignment(JTextField.RIGHT);
      }else if(clickStr.matches("[\\+\\-*/]{1}")){
           operator=clickStr;
           firstInput=this.input_text.getText();
           this.input_text.setText("");
      }else if(clickStr.equals("=")){
           Double a=Double.valueOf(firstInput);
           Double b=Double.valueOf(this.input_text.getText());
           Double result=null;
           switch (operator){
               case "+":
                   result=a+b;
                   break;
               case "-":
                   result=a-b;
                   break;
               case "*":
                   result=a*b;
                   break;
               case "/":
                   if(b!=0){
                   result = a / b;
              }
                   break;
          }
           this.input_text.setText(result.toString());
      }
       //JOptionPane.showMessageDialog(this,clickStr);//显示消息对话框
  }
}

posted on 2022-03-05 20:15  吴XX  阅读(99)  评论(0)    收藏  举报