unit Panel1;interfaceuses
  Windows, Messages, SysUtils, Classes, Controls, ExtCtrls;type
  TPanel1 = class(TPanel)
  private
    FShape1: TShape;
    FShape2: TShape;
  protected
    { Protected declarations }
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property Shape1: TShape read FShape1 write FShape1;
    property Shape2: TShape read FShape2 write FShape2;
  end;procedure Register;implementationconstructor TPanel1.Create(AOwner: TComponent);
begin
    inherited Create(AOwner);
    FShape1 := TShape.Create(self);
    FShape2 := TShape.Create(self);
end;destructor TPanel1.Destroy;
begin
    FShape1.Free;
    FShape2.Free;
    inherited Destroy;
end;procedure Register;
begin
  RegisterComponents('System', [TPanel1]);
end;end.以上代码不能在Panel 上显示出Shape1和Shape2,为什么?

解决方案 »

  1.   

    constructor TPanel1.Create(AOwner: TComponent);
    begin
        inherited Create(AOwner);
        FShape1 := TShape.Create(self);
        FShape2 := TShape.Create(self);
        FShape1.parent := panel1;
        FShape2.parent := panel1;end;
      

  2.   

    FShape1 := TShape.Create(self);
    FShape1.Parent:=Self;
    FShape2 := TShape.Create(self);
    FShape2.Parent:=Self;
      

  3.   

    原因有两个:
    其一、没有指定 TShape.Parent 属性;
      FShape1.Parent := Self;
      FShape2.Parent := Self;
    其二、没有指定他们的 BoundsRect 属性,也就是没有指定他们的位置和大小;
      with FShape1 do
      begin
        Left := 10;
        Top := 10;
        Width := 30;
        Height := 30;
      end;
      with FShape2 do
      begin
        Left := 60;
        Top := 10;
        Width := 30;
        Height := 30;
      end;
    或者:
      FShape1.SetBounds(10, 10, 30, 30);
      FShpae2.SetBounds(60, 10, 30, 30);