java编写简单计算器代码,Java实现计算器
计算器实现了大部分基础功能:基本运算,菜单栏选项,并且拓展了普通型和科学兴选项等等,读者可以在此基础上进行修改和拓展。其他具体实现方法可以看源码,里面有详细的概述,代码框架清晰。
运行环境:面向爪哇开发人员的win 10 Eclipse IDE-2020-06
下面是计算器的视图:
导入Java。awt。*;导入Java。awt。事件。动作事件;导入Java。awt。事件。动作监听器;导入javax。挥棒。*;/** 计算器*/公共类计算试验实现ActionListener { //初始框架搭建JFrame框架=新JFrame(计算器);JTextField area=new JTextField( 0 );JPanel panel 1=new JPanel();JPanel panel 2=new JPanel();JButton[]buttons=new JButton[20];String[] buttonsText={ sqrt ,退格 C ,/, 7 , 8 , 9 , * , 4 , 5 , 6 ,-, 1 , 2 , 3 ,, 0 ,。, /-, = };布尔点=假;//用于判断是否输入多位小数点布尔键=真;//做完运算(=)后继续输入数字字符串符号="";//用于判断和记录运算符号双温度=0;//多次连续运算时,值的寄存处公共计算器test(){ init menu();initText();初始化extend();初始化框架();initBorderLayout();} //初始化菜单private void init menu(){ JMenuBar MB=new JMenuBar();JMenu m1=新JMenu(选项);JMenu m2=新JMenu(编辑);JMenu m3=新JMenu(帮助);JMenuItem m11=新的JMenuItem(普通型计算器);JMenuItem m12=新的JMenuItem(科学型计算器);m1。添加(m11);m1。加法(M12);m11。addactionlistener(new action listener(){ @ Override public void action performed(action event e){ boolean flag=false;第二小组。设置可见(标志);框架。pack();} });M12。addactionlistener(new action listener(){ @ Override public void action performed(action event e){ boolean flag=true;第二小组。设置可见(标志);框架。pack();} });MB。加(m1);MB。加(m2);MB。加(m3);框架。setjmenubar(MB);} //初始化输出文本域private void initText(){ area。设置字体(新字体( TimesRoman ,字体.平原,20));area.setSize(400,100);面积。设置水平对齐(JTextField .对);//向右显示} //初始化拓展功能私有void initExtend() {
panel2.setLayout(new GridLayout(1,4,1,1)); JButton b1 = new JButton("sin"); JButton b2 = new JButton("cos"); JButton b3 = new JButton("exp"); JButton b4 = new JButton("ln"); b1.setFont(new Font("TimesRoman", Font.PLAIN, 20)); b2.setFont(new Font("TimesRoman", Font.PLAIN, 20)); b3.setFont(new Font("TimesRoman", Font.PLAIN, 20)); b4.setFont(new Font("TimesRoman", Font.PLAIN, 20)); b1.setSize(100, 100); b1.addActionListener(this); b2.setSize(100, 100); b2.addActionListener(this); b3.setSize(100, 100); b3.addActionListener(this); b4.setSize(100, 100); b4.addActionListener(this); panel2.add(b1); panel2.add(b2); panel2.add(b3); panel2.add(b4); } // 初始化计算器基本界面 private void initFrame() { panel1.setLayout(new GridLayout(5, 4, 1, 1)); for (int i = 0; i < buttonsText.length; i++) { JButton button = new JButton(buttonsText[i]); button.setSize(100, 100); button.setFont(new Font("TimesRoman", Font.PLAIN, 20)); button.addActionListener(this); panel1.add(button); } } // 初始化计算器总基本界面 private void initBorderLayout() { frame.setLayout(new BorderLayout()); frame.add(panel1, BorderLayout.SOUTH); // 插入组件 frame.add(area, BorderLayout.NORTH); frame.add(panel2, BorderLayout.CENTER); frame.setLocation(700, 400); frame.setSize(400, 700); frame.setVisible(true); // 设置可见 panel2.setVisible(false); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 可以关闭 frame.pack(); } public static void main(String[] args) { new CaculatorTest(); } @Override // 事件监听 public void actionPerformed(ActionEvent e) { String str = e.getActionCommand(); String str2 = area.getText(); if (str == "0" str == "1" str == "2" str == "3" str == "4" str == "5" str == "6" str == "7" str == "8" str == "9") { if (key == false) { area.setText(str2 + str); } else { area.setText(str); key = false; } } else if (str == "C") { area.setText("0"); sign = " "; key = true; } else if (str == ".") { if (point == false) { area.setText(str2 + str); point = true; } else { area.setText("double poits!press C to update!"); point = false; } } else if (str == "+/-") { double num = Double.valueOf(str2); num = -num; area.setText(String.valueOf(num)); } else if (str == "退格") { if (str2.length() == 0) { area.setText("cant be deleted!please press C!"); } else { str2 = str2.substring(0, str2.length() - 1); area.setText(str2); } } else if (str == "sqrt") { area.setText(""); sign = "s"; } else if (str == "sin") { area.setText(""); sign = "sin"; } else if (str == "cos") { area.setText(""); sign = "cos"; } else if (str == "exp") { area.setText(""); sign = "exp"; } else if (str == "ln") { area.setText(""); sign = "ln"; } else { if (str == "+") { if (sign == " ") { sign = "+"; temp = Double.valueOf(str2); area.setText(""); } else if (sign == "-") { if (str2.length() == 0) { sign = "+"; } else { temp = temp - Double.valueOf(str2); sign = "+"; area.setText(""); key = true; } } else if (sign == "+") { if (str2.length() == 0) { sign = "+"; } else { temp = temp + Double.valueOf(str2); sign = "+"; area.setText(""); key = true; } } else if (sign == "*") { if (str2.length() == 0) { sign = "+"; } else { temp = temp * Double.valueOf(str2); sign = "+"; area.setText(""); key = true; } } else if (sign == "/") { if (str2.length() == 0) { sign = "+"; } else if (Double.valueOf(str2) == 0) { area.setText("除数不能为0哦!按 C"); } else { temp = temp / Double.valueOf(str2); area.setText(""); sign = "+"; key = true; } } else if (sign == "s") { if (str2.length() == 0) { sign = "+"; } else { temp = Math.sqrt(Double.valueOf(str2)); area.setText(""); sign = "+"; } } else if (sign == "sin") { if (str2.length() == 0) { sign = "+"; } else { temp = Math.sin(Double.valueOf(str2)); area.setText(""); sign = "+"; } } else if (sign == "cos") { if (str2.length() == 0) { sign = "+"; } else { temp = Math.cos(Double.valueOf(str2)); area.setText(""); sign = "+"; } } else if (sign == "exp") { if (str2.length() == 0) { sign = "+"; } else { temp = Math.exp(Double.valueOf(str2)); area.setText(""); sign = "+"; } } else if (sign == "ln") { if (str2.length() == 0) { sign = "+"; } else { temp = Math.log(Double.valueOf(str2)); area.setText(""); sign = "+"; } } } else if (str == "-") { if (sign == " ") { sign = "-"; temp = Double.valueOf(str2); area.setText(""); } else if (sign == "-") { if (str2.length() == 0) { sign = "-"; } else { temp = temp - Double.valueOf(str2); sign = "-"; area.setText(""); key = true; } } else if (sign == "+") { if (str2.length() == 0) { sign = "-"; } else { temp = temp + Double.valueOf(str2); sign = "-"; area.setText(""); key = true; } } else if (sign == "*&am
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。