Example 3 A right-angled triangle with integer side lengths, if the lengths of its two legs are the roots of the equation
$$
x^{2}-(k+2) x+4 k=0
$$

find the value of $k$ and determine the lengths of the three sides of the right-angled triangle. ${ }^{[2]}$
(2010, National Junior High School Mathematics League, Jiangxi Province Preliminary)
【Analysis】From the known conditions, first convert the relationship between the roots and coefficients of a quadratic equation into the difference of squares formula, then solve by factorization.
Solution Let the lengths of the legs be $a, b(a \leqslant b)$. Then
$$
\begin{array}{l}
a+b=k+2, a b=4 k \\
\Rightarrow a b-4 a-4 b=-8 \\
\Rightarrow(a-4)(b-4)=8=1 \times 8=2 \times 4 \\
\Rightarrow(a-4, b-4)=(1,8) \text { or }(2,4) \\
\Rightarrow(a, b)=(5,12) \text { or }(6,8) .
\end{array}
$$