java 计算器

  

【实验要求】编写一个模拟计算器的程序,使用面板和网格布局,添加一个文本框,10个数字按钮(0-9),4个加减乘除按钮,一个等号按钮,一个清除按钮,要求将计算公式和结果显示在文本框中。 

【实验代码】package Calculator;

import java.awt.BorderLayout;
import java.awt.Dimension;
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.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class CalculatorInterface extends JFrame {
 String buttonStr1 = "";
 String buttonStr2 = "";
 String buttonchar = "";
 JTextField textField;
 public CalculatorInterface(){
  super();
  setTitle("计算器HY49");
  setResizable(false);
  setBounds(100,100,240,240);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  final JPanel viewPanel = new JPanel();
  getContentPane().add(viewPanel,BorderLayout.NORTH);
  textField = new JTextField();
  textField.setEditable(false);
  textField.setHorizontalAlignment(SwingConstants.RIGHT);
  textField.setColumns(18);
  viewPanel.add(textField);
  final JPanel buttonPanel = new JPanel();
  final GridLayout gridLayout = new GridLayout(4,4);
  gridLayout.setVgap(10);
  gridLayout.setHgap(10);
  buttonPanel.setLayout(gridLayout);
  getContentPane().add(buttonPanel,BorderLayout.CENTER);
  String [][]names = {{"1","2","3","+"},{"4","5","6","-"},{"7","8","9","*"},{"cl","0","=","/"}};
  JButton [][]buttons = new JButton[4][4];
  for (int row = 0;row <4;row++){
   for (int col = 0;col<4;col++){
    buttons[row][col] = new JButton(names[row][col]);
    //建立一个监听机制类ClickedButton
    buttons[row][col].addActionListener(new ClickedButton());
    buttonPanel.add(buttons[row][col]);
   }
  }
  final JLabel leftLabel =new JLabel();
  final JLabel rightLabel =new JLabel();
  leftLabel.setPreferredSize(new Dimension(10,0));
  getContentPane().add(leftLabel,BorderLayout.WEST);
  rightLabel.setPreferredSize(new Dimension(10,0));
  getContentPane().add(rightLabel,BorderLayout.EAST);
 }
  class ClickedButton implements ActionListener{
   @Override
   public void actionPerformed(ActionEvent e) {
    JButton whichButton = (JButton) e.getSource();//获取点击的事件,即是点下了哪个按钮
    String whichName = whichButton.getActionCommand();
    
    if (!(whichName.equals("+")||whichName.equals("-")||whichName.equals("*")||whichName.equals("/")||whichName.equals("cl")||whichName.equals("="))&&buttonchar==""){
     buttonStr1+=whichName;
     textField.setText(buttonStr1);
    }
    else if (!(whichName.equals("+")||whichName.equals("-")||whichName.equals("*")||whichName.equals("/")||whichName.equals("cl")||whichName.equals("="))&&buttonchar!=""){
     buttonStr2+=whichName;
     textField.setText(buttonStr1+"  "+buttonchar+"  "+buttonStr2);
    }
    else{
     if (whichName.equals("cl")){
      if (buttonStr2==""){
       buttonStr1 = buttonStr1.substring(0, buttonStr1.length()-1);
       textField.setText(buttonStr1);
      }
      else{
       buttonStr2 = buttonStr2.substring(0, buttonStr2.length()-1);
       textField.setText(buttonStr1+"  "+buttonchar+"  "+buttonStr2);
      }
     }
     else if (whichName.equals("=")){
      textField.setText(buttonStr1+"  "+buttonchar+"  "+buttonStr2+"  "+"=");
      int first = Integer.parseInt(buttonStr1);
      int second = Integer.parseInt(buttonStr2);
      double result;
      if (buttonchar=="+")
       result = first + second;
      else if (buttonchar=="-")
       result = first - second;
      else if (buttonchar=="*")
       result = first * second;
      else
       result = first / second;
      textField.setText(buttonStr1+"  "+buttonchar+"  "+buttonStr2+"  "+" = "+result);
     }
     else
      buttonchar = whichName;
    }
   
 }
 public static void main(String []args){
  CalculatorInterface cal = new CalculatorInterface();
  cal.setVisible(true);
 }
 
}

【实验效果】

java <wbr>计算器



图1.实现加法


 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值