//全写了注释了, 不懂追问 unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Buttons, ExtCtrls; //新建一工程, 放进15个TSpeedButton和一个TEdit然后按照下边的说明设置一下. type TForm1 = class(TForm) //把窗体的KeyPreview属性设置为TRUE SpeedButton1: TSpeedButton; //表示数字键1, Caption属性设置为1 SpeedButton2: TSpeedButton; //表示数字键2, Caption属性设置为2 SpeedButton3: TSpeedButton; //表示数字键3, Caption属性设置为3 SpeedButton4: TSpeedButton; //表示数字键4, Caption属性设置为4 SpeedButton5: TSpeedButton; //表示数字键5, Caption属性设置为5 SpeedButton6: TSpeedButton; //表示数字键6, Caption属性设置为6 SpeedButton7: TSpeedButton; //表示数字键7, Caption属性设置为7 SpeedButton8: TSpeedButton; //表示数字键8, Caption属性设置为8 SpeedButton9: TSpeedButton; //表示数字键9, Caption属性设置为9 SpeedButton10: TSpeedButton; //表示数字键0, Caption属性设置为0 SpeedButton11: TSpeedButton; //表示小数点., , Caption属性设置为 . 小数点 SpeedButton12: TSpeedButton; //计算结果, 等于(=)号, Caption属性设置为 = SpeedButton13: TSpeedButton; //加号, Caption属性设置为 + , Tag属性设置为 1 SpeedButton14: TSpeedButton; //减号, Caption属性设置为 - , Tag属性设置为 2 Edit1: TEdit; //显示计算结果 SpeedButton15: TSpeedButton; //归零(复位)处理 procedure SpeedButton1Click(Sender: TObject); procedure SpeedButton12Click(Sender: TObject); procedure SpeedButton13Click(Sender: TObject); procedure SpeedButton15Click(Sender: TObject); procedure FormKeyPress(Sender: TObject; var Key: Char); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} var Flag: Boolean = True; //。标记位 Flag1: Integer = 1; //计算方法标志位 num1, num2, result: Real; flagresult: Boolean = False; procedure TForm1.SpeedButton1Click(Sender: TObject); //按住SHIFT键, 再用鼠标点SpeedButton1 至 SpeedButton11(多选), 然后在属性面板双击onClick //以后11个SpeedButton共用一个OnClick事件, var str: string; begin str := Edit1.Text; if (Length(str) = 1) and (str = '0') then Edit1.Clear; Edit1.Color := clBlue; if ((Sender as TSpeedButton).Caption = '.') then //小数点处理 begin if Flag then begin Edit1.Text := Edit1.Text + (sender as TSpeedButton).Caption; Flag := False; end end else Edit1.Text := Edit1.Text + (sender as TSpeedButton).Caption; end; procedure TForm1.SpeedButton12Click(Sender: TObject); //计算并显示结果 begin Edit1.Color := clRed; num2 := StrToFloatDef(Edit1.Text, 0.00); case Flag1 of 1: result := num1 + num2; 2: result := num1 - num2; Edit1.Text := FloatToStr(result); end; procedure TForm1.SpeedButton13Click(Sender: TObject); //按住SHIFT, 用鼠标点SpeedButton13, SpeedButton14(多选), 然后在属性面板双击onClick //以便2个SpeedButton共用一个OnClick事件, begin Flag1 := (Sender as TSpeedButton).Tag; num1 := StrToFloatDef(Edit1.Text, 0.00); Edit1.Text := '0'; end; procedure TForm1.SpeedButton15Click(Sender: TObject); //归零(复位) begin Flag := True; //。标记位 Flag1 := 1; //计算方法标志位 num1 := 0; num2 := 0; result := 0; Edit1.Text := '0'; end; procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char); //对窗口按键盘上的数字键时, 做输入操作数处理 begin case key of '1': SpeedButton1.Click; '2': SpeedButton2.Click; '3': SpeedButton3.Click; '4': SpeedButton4.Click; '5': SpeedButton5.Click; '6': SpeedButton6.Click; '7': SpeedButton7.Click; '8': SpeedButton8.Click; '9': SpeedButton9.Click; '0': SpeedButton10.Click; '.': SpeedButton11.Click; '+': SpeedButton13.Click; '-': SpeedButton14.Click; end; end; end.
采纳哦