"//Gets called once when JApplet is created public Jisuanqi() { super("计算器"); Container container = getContentPane(); container.setLayout(new GridLayout(4,4)); String buttonName[] = {"7","8","9","/","4","5","6","*","1","2","3","-","0",".","=","+"}; JButton button[] = new JButton[16]; for (int i=0; i<16; i++) { button[i] = new JButton(buttonName[i]); button[i].addActionListener(this); container.add(button[i]); } text = new JTextField(); container.add(text); } //Invoked when an action occurs public void actionPerformed(ActionEvent event) { String label = ((JButton)event.getSource()).getText(); if(label.charAt(0)>='0' && label.charAt(0)<='9' || label.equals(".")) { input+=label; text.setText(input); } else if(label.equals("+")||label.equals("-")||label.equals("*")||label.equals("/")||label.equals("=")) { if(!input.equals("")) // 判断是否输入数字 { num1 = Double.valueOf(input).doubleValue(); //取得第一个操作数 input = ""; // 保存运算符 if(label.equals("+")) { type = 1; text.setText("+"); } if(label.equals("-")) { type = 2; text.setText("-"); } if(label.equals("*")) { type = 3; text.setText("*"); } if(label.equals("/")) { type = 4; text.setText("/"); } if(label.equals("=")) { type = 5; text.setText("="); } } else { text.setText("err"); } } if(label.equals("C")) { result = 0; num1 = 0; num2 = 0; //清零 input = ""; text.setText(""); flag = 0; } else if(label.equals("CE")) { input = ""; text.setText(""); } } }}" 这是一个用Java编写的简单计算器程序,它可以进行加法、减法、乘法、除法和取余操作。程序采用图形用户界面,使用了Swing组件,并且继承了JFrame类和实现了ActionListener接口。用户可以点击按钮输入数字和运算符,显示结果的文本框会实时更新。程序还含有清零和清除功能,可以清除输入的数字和运算符。 程序定义了私有变量result、num1、num2、input、type和flag等,分别用于记录计算结果、操作数、输入的字符串和运算类型。程序还包括一个JTextField用于显示计算结果。在actionPerformed方法中,根据按钮输入的不同进行相应的计算和显示操作,同时处理清零和清除操作。 总的来说,这个Java计算器程序通过图形用户界面实现了简单的数学计算功能,并在用户友好的同时具有一定的功能完备性。
![](https://csdnimg.cn/release/download_crawler_static/1817189/bg7.jpg)
剩余34页未读,继续阅读
![text/x-java](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/release/wenkucmsfe/public/img/green-success.6a4acb44.png)