java计算代码器编写 java编写简单计算器代码

用Java或C++写一个计算器 , 如图所示的?采用java编写一个简单计算器,使用awt和swing
代码如下:
import java.awt.Color;
import java.awt.Font;
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;
/*用java做一个计算器
* 实现整数和小数的加减乘除
* 具有删除和复位功能
*/
public class CaculatorDemo extends JFrame {
private static final long serialVersionUID = 1L;
private StringBuilder sBuilder = new StringBuilder();
private Double a;//中间变量用于存储输入的第一个数
private Double b;//中间变量,用于存储输入的第二个数
private Double double1;//用于接收计算结果
private Integer i;// i用于表示加减乘除
public CaculatorDemo() {
this.setTitle("计算器");
this.setSize(318, 457);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
this.getContentPane().add(panel);
panel.setLayout(null);
// 定义一个label用于显示输入数据和计算结果,这里不用textfield,可以避免用户输入造成bug , 简化程序开发
final JLabel label = new JLabel();
label.setBounds(0, 0, 300, 50);
label.setFont(new Font("dialog", 1, 30));
label.setOpaque(true);// 由于jlabel默认透明 , 直接设置背景色无效,需要先将不透明设置为true
label.setBackground(Color.white);
panel.add(label);
// 定义按钮组件
JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
JButton button3 = new JButton("3");
JButton button4 = new JButton("4");
JButton button5 = new JButton("5");
JButton button6 = new JButton("6");
JButton button7 = new JButton("7");
JButton button8 = new JButton("8");
JButton button9 = new JButton("9");
JButton button0 = new JButton("0");
JButton buttonadd = new JButton("+");// 加
JButton buttonminus = new JButton("-");// 减
JButton buttontime = new JButton("×");// 乘
JButton buttondivid = new JButton("÷");// 除
JButton buttonequal = new JButton("=");// 等于
JButton buttondecimal = new JButton(".");// 小数点
JButton buttondelet = new JButton("←");// 删除
JButton buttonclear = new JButton("C");// 清除
// 定义按钮组件位置
button0.setBounds(0, 50, 100, 60);
button0.setFont(new Font("dialog", 1, 30));
panel.add(button0);
button1.setBounds(100, 50, 100, 60);
button1.setFont(new Font("dialog", 1, 30));
panel.add(button1);
button2.setBounds(200, 50, 100, 60);
button2.setFont(new Font("dialog", 1, 30));
panel.add(button2);
button3.setBounds(0, 110, 100, 60);
button3.setFont(new Font("dialog", 1, 30));
panel.add(button3);
button4.setBounds(100, 110, 100, 60);
button4.setFont(new Font("dialog", 1, 30));
panel.add(button4);
button5.setBounds(200, 110, 100, 60);
button5.setFont(new Font("dialog", 1, 30));
panel.add(button5);
button6.setBounds(0, 170, 100, 60);
button6.setFont(new Font("dialog", 1, 30));
panel.add(button6);
button7.setBounds(100, 170, 100, 60);
button7.setFont(new Font("dialog", 1, 30));
panel.add(button7);
button8.setBounds(200, 170, 100, 60);
button8.setFont(new Font("dialog", 1, 30));
panel.add(button8);
button9.setBounds(0, 230, 100, 60);
button9.setFont(new Font("dialog", 1, 30));
panel.add(button9);
buttonadd.setBounds(100, 230, 100, 60);
buttonadd.setFont(new Font("dialog", 1, 30));
panel.add(buttonadd);//加
buttonminus.setBounds(200, 230, 100, 60);

推荐阅读