图形界面计算器(JAVA-swing)

JAVA作业:图形界面计算器

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame{
      private final JTextField textField;
      private String num="0";//操作数
      private String operator="+";//运算符
      private String result="0";//被操作数
      public static void main(String[] args){
        Calculator frame=new Calculator();
        frame.setVisible(true);//设置窗体可见,默认为不可见
    }
    public Calculator(){
        super();
        setTitle("计算器图形界面-作业11");
        setResizable(false);//设置窗体大小不可改变
        setBounds(200,200,320,242);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final JPanel viewPanel=new JPanel();//创建“显示器面板,采用默认的流布局
        getContentPane().add(viewPanel, BorderLayout.NORTH);//添加到窗体顶部
        textField=new JTextField();//创建显示器
        textField.setText(num);//设置显示器默认文本
        textField.setColumns(25);//设置显示器宽度
        textField.setEditable(false);//设置显示器不可编辑
        textField.setHorizontalAlignment(SwingConstants.RIGHT);//靠右侧对齐
        viewPanel.add(textField);//将显示器添加到显示器面板中
        getContentPane().add(viewPanel,BorderLayout.NORTH);//添加到窗体顶部
        final JPanel clearButtonPanel=new JPanel();
        final GridLayout gridLayout1=new GridLayout(1,0);
        gridLayout1.setVgap(10);
        gridLayout1.setHgap(10);
        clearButtonPanel.setLayout(gridLayout1);//按钮面板采用网格布局
        getContentPane().add(clearButtonPanel,BorderLayout.CENTER);
        String[] clearButtonNames={"BACK","CE","C","+/-"};
        for(int i=0;i<clearButtonNames.length;i++){
            final JButton button=new JButton(clearButtonNames[i]);//创建清除按钮
            button.addActionListener(new ClearButtonActionListener());//添加监视器
            clearButtonPanel.add(button);//将清除按钮添加到清除按钮面板中
        }
        
        final JPanel inputButtonPanel=new JPanel();//创建输入按钮面板
        final GridLayout gridLayout=new GridLayout(4,0);
        gridLayout.setVgap(10);
        gridLayout.setHgap(10);
        inputButtonPanel.setLayout(gridLayout);//输入按钮面板采用网格布局
        getContentPane().add(inputButtonPanel,BorderLayout.SOUTH);
        String[][] inputButtonNames={
        		{"1","2","3","+"},
                {"4","5","6","-"},
                {"7","8","9","*"},
                {".","0","=","/"}
                };//定义输入按钮名称数组
        for(int row=0;row<inputButtonNames.length;row++){
            for(int col=0;col<inputButtonNames.length;col++){
                final JButton button=new JButton(inputButtonNames[row][col]);
                button.setName(row+""+col);//输入按钮的名称由其所在行和列的索引组成
                button.addActionListener(new InputButtonActionListener());
                inputButtonPanel.add(button);//将按钮添加到按钮面板中
            }
        }
    }
    class InputButtonActionListener implements ActionListener{//输入按钮事件监听器
        public void actionPerformed(ActionEvent e){
            JButton button=(JButton)e.getSource();//获得触发此次事件的按钮对象
            String name=button.getName();//获得按钮名称
            int row=Integer.valueOf(name.substring(0,1));//解析其所在行
            int col=Integer.valueOf(name.substring(1,2));//解析所在列
            if(col==3){//此次输入的为运算符
                count();//计算结果
                textField.setText(result);//修改显示器文本
                operator=button.getText();//获得输入的运算符
            }else if(row==3){
              if(col==0){
                  if(num.indexOf(".")<0){//查看是否输入了小数点
                    num=num+button.getText();
                    textField.setText(num);
                  }
              }else if(col==1){//此次输入的为0
                  if(num.indexOf(".")>0){//查看是否为小数
                      num=num+button.getText();
                      textField.setText(num);
                  }else{
                      if(!num.substring(0,1).equals("0")){//查看第一位是否为0
                          num=num+button.getText();
                          textField.setText(num);
                      }
                  }
                }else{//此次输入的为=
                    count();
                    textField.setText(result);//修改显示器文本
                    operator="+";
                }
            }else{//此次输入的为数字
                if(num.equals("0"))
                    num=button.getText();
                else
                    num=num+button.getText();
                textField.setText(num);
            }

        }
        private void count(){
            Double n=Double.valueOf(num);
            Double r=Double.valueOf(result);
            if(r==0){
                result=num;
                num="0";
            }else{
                if(operator.equals("+")){
                    r=r+n;
                }else if(operator.equals("-")){
                    r=r-n;
                }else if(operator.equals("*")){
                    r=r*n;
                }else{
                    r=r/n;
                }
                num="0";
                result=r+"";
            }
        }
    }
    class ClearButtonActionListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
          JButton button=(JButton)e.getSource();
          String text=button.getText().trim();
          if(text.equals("BACK")){
              int length=num.length();
              if(length==1)//当输入的数值为1位数字时
                  num="0";//回退为默认值0
              else
                  num=num.substring(0,length-1);//否则去掉输入数值的最后一个数字
          }else if(text.equals("CE")){//清除当前输入数值按钮
              num="0";
          }else if(text.equals("+/-")) { 
        	  if(Double.parseDouble(num)>0) num="-"+num;
        	  else if(Double.parseDouble(num)<0) num=String.valueOf(Math.abs(Double.parseDouble(num)));
          }else if(text.equals("C")) {//执行新的计算按钮
              num="0";
              operator="+";
              result="0";
          }
          textField.setText(num);//修改显示器文本
        }
    }
}

发表评论

电子邮件地址不会被公开。