计算器模拟程序的Java代码
- 格式:doc
- 大小:39.50 KB
- 文档页数:16
计算器模拟程序的Java代码
import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.*;
public class Cal extends JFrame
{
GridLayout gl3;
Panel p0, p3;
JTextField tf1;
JButton b7, b8, b9, b10, b12,b13, b14,b15, b17, b18, b19, b20, b22, b24, b25, b26;
StringBuffer str;// 显示屏所显示的字符串
double x, y;// x和y都是运算数
double sum=0;//加和
double mul=1;//乘积
double sub=0;//减差令其为输入的第一个值
double dev;//除商令其为输入的第一个值
boolean tag= true;
boolean tag1= true;
int z = -1,k;// Z表示单击了那一个运算符.0表示"+",1表示
"-",2表示"*",3表示"/"
//static double m ;// 记忆的数字
public Cal()
{
Container con=getContentPane();
con.setLayout(new FlowLayout());
gl3 = new GridLayout(4, 4, 15, 15);
tf1 = new JTextField(30);// 显示屏
tf1.setHorizontalAlignment(JTextField.RIGHT);
tf1.setEnabled(false);
tf1.setEditable(false);
tf1.setText("0");
tf1.addKeyListener(new Tt());
b7 = new JButton("7");
b7.setForeground(Color.blue);
b7.addActionListener(new Bt());
b8 = new JButton("8");
b8.setForeground(Color.blue);
b8.addActionListener(new Bt());
b9 = new JButton("9");
b9.setForeground(Color.blue);
b9.addActionListener(new Bt());
b10 = new JButton("/");
b10.setForeground(Color.red);
b10.addActionListener(new Bt());
b12 = new JButton("4");
b12.setForeground(Color.blue); b12.addActionListener(new Bt());
b13 = new JButton("5");
b13.setForeground(Color.blue); b13.addActionListener(new Bt());
b14 = new JButton("6");
b14.setForeground(Color.blue); b14.addActionListener(new Bt());
b15 = new JButton("*");
b15.setForeground(Color.red);
b15.addActionListener(new Bt());
b17 = new JButton("1");
b17.setForeground(Color.blue); b17.addActionListener(new Bt());
b18 = new JButton("2");
b18.setForeground(Color.blue); b18.addActionListener(new Bt());
b19 = new JButton("3");
b19.setForeground(Color.blue);
b19.addActionListener(new Bt());
b20 = new JButton("-");
b20.setForeground(Color.red);
b20.addActionListener(new Bt());
b22 = new JButton("0");
b22.setForeground(Color.blue); b22.addActionListener(new Bt());
b24 = new JButton(".");
b24.setForeground(Color.blue); b24.addActionListener(new Bt());
b25 = new JButton("+");
b25.setForeground(Color.red);
b25.addActionListener(new Bt());
b26 = new JButton("=");
b26.setForeground(Color.red);
b26.addActionListener(new Bt());
// 实例化四个面板
p0 = new Panel();
//p1 = new Panel();
//p2 = new Panel();