不建议在Frame里面释放自己,然后再加载其他窗体到主窗体。<br>非要这样做,可以把这部分代码提出来,放到外部单元,如公共单元,或者主窗体。<br>//主窗体<br>unit Unit3;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs;<br><br>type<br> TFrameClass = class of TFrame;<br><br> TForm3 = class(TForm)<br> procedure FormCreate(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>procedure ShowFrame(AParent: TWinControl; AFrame: TFrameClass);<br><br>var<br> Form3: TForm3;<br> FCurrFrame: TFrame;//当前加载的FRAME<br><br>implementation<br><br>uses<br> Unit1, Unit2;<br><br>{$R *.dfm}<br><br>{ TForm3 }<br><br>procedure TForm3.FormCreate(Sender: TObject);<br>begin<br> ShowFrame(self, TFrame1);<br>end;<br><br>procedure ShowFrame(AParent: TWinControl; AFrame: TFrameClass);<br>begin<br> if Assigned(FCurrFrame) then FreeAndNil(FCurrFrame);<br><br> FCurrFrame := AFrame.Create(nil);<br> FCurrFrame.Parent := AParent;<br> FCurrFrame.Show;<br>end;<br><br>end.<br>//Frame1<br>unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, <br> Dialogs, StdCtrls;<br><br>type<br> TFrame1 = class(TFrame)<br> Button1: TButton;<br> Label1: TLabel;<br> procedure Button1Click(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>implementation<br><br>uses<br> Unit3, Unit2;<br><br>{$R *.dfm}<br><br>procedure TFrame1.Button1Click(Sender: TObject);<br>begin<br> ShowFrame(Form3, TFrame2);<br>end;<br><br>end.<br><br>//Frame2<br>unit Unit2;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, <br> Dialogs, StdCtrls;<br><br>type<br> TFrame2 = class(TFrame)<br> Button2: TButton;<br> Label1: TLabel;<br> procedure Button2Click(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>implementation<br><br>uses<br> Unit3, Unit1;<br><br>{$R *.dfm}<br><br>procedure TFrame2.Button2Click(Sender: TObject);<br>begin<br> ShowFrame(Form3, TFrame1);<br>end;<br><br>end.