试试以下代码: procedure TForm1.Button1Click(Sender: TObject); var i:integer; sPnl:TPanel; begin for i := 1 to 2 do begin sPnl := TPanel.Create(Self); sPnl.Parent := Panel1; if i=1 then sPnl.Align := alLeft else begin sPnl.left := 400; sPnl.Align := alClient; end; sPnl.Caption := IntToStr(i); end; end;
再修改代码: procedure TForm1.Button1Click(Sender: TObject); var i:integer; sPnl:TPanel; begin for i := 1 to 2 do begin sPnl := TPanel.Create(Self); sPnl.Parent := Panel1; if i=1 then begin sPnl.Align := alLeft; sPnl.Width := sPnl.Parent.Width div 2; end else begin sPnl.left := sPnl.Parent.Width div 2 +10; sPnl.Align := alClient; end; sPnl.Caption := IntToStr(i); end; end;
设置下spnl2的Left 就可以了
看来只有先设置left位置,在设置align。for i := 1 to 2 do begin sPnl := TPanel.Create(Self); sPnl.Parent := Panel1; sPnl.Left := i * sPnl.Widths; sPnl.Align := alLeft; sPnl.Caption := IntToStr(i); end;
不要这个,自己写sPnl.left:=
procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
sPnl:TPanel;
begin
for i := 1 to 2 do
begin
sPnl := TPanel.Create(Self);
sPnl.Parent := Panel1;
if i=1
then sPnl.Align := alLeft
else begin
sPnl.left := 400;
sPnl.Align := alClient;
end;
sPnl.Caption := IntToStr(i);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
sPnl:TPanel;
begin
for i := 1 to 2 do
begin
sPnl := TPanel.Create(Self);
sPnl.Parent := Panel1;
if i=1
then begin
sPnl.Align := alLeft;
sPnl.Width := sPnl.Parent.Width div 2;
end
else begin
sPnl.left := sPnl.Parent.Width div 2 +10;
sPnl.Align := alClient;
end;
sPnl.Caption := IntToStr(i);
end;
end;
begin
sPnl := TPanel.Create(Self);
sPnl.Parent := Panel1;
sPnl.Left := i * sPnl.Widths;
sPnl.Align := alLeft;
sPnl.Caption := IntToStr(i);
end;