I was just discussing this today, it is was one of my faults and have learned to strengthen it. The reason your leg swings is because you are trying to counter balance. Check the length of your stirrups are they appropriate? If I sit on the horses back, I like the stirrup to hit me just at the top of the foot almost to the ankle. Mainly because I have a lot of western back ground. There are several ways and drills to strengthen your muscles and to retrain your legs to stay still.
First, I would do a lot of 2-pointing at the walk and trot. This will build your strength and endurance. If you can get someone to do this on a lunge line that can give you constructive feed back even better. Get into your 2 point and focus on your lower thigh and knees to keep your body up off the saddle keep your shoulders back and eyes up looking forward. Keeping the bend in your knees and hips with your back straight. Your knees and hips should absorb all the impact and movement. If you are more focused on you knees all the way to your eyes and keeping yourself balanced than your lower legs should fall into place.
If you have someone to do this for you on a lunge line keep your arms out to the side and hold your 2-point for a lap or two each direction at the walk and work up to a trot. Focus on your position, are you falling forward or back? You should be able to find a balance where it is not incredibly strenuous to hold but you know when you've lost that center of balance.
Another, drill would be to loose your stirrups and keep your legs in position. However, I wouldn't recommend this for a novice rider. Focus on really drawing your legs down around your horse keeping your heels down and toes in toward the horse as FOXY mentioned.
This drill is one of my favorites because it really helps you feel if you are balanced and it is where you change diagnols in the air rather than sitting a beat. This drill will tell you if you are in front or behind the movement. If your upper and lower body are in sync you will be able to do this with ease it not you will either fall forward or back.
I hope this helps