There are several issues to consider - whether the 2x4 itself is strong enough to bear the load you're putting on it in the direction that you're loading it (so the grade/type of wood is important, not all 2x4's are created equal), whether the fastener/hook you use will overstress the wood in the 2x4 at the point where it attaches, and whether the load on the 2x4 will overstress/exceed the capacity of whatever it is that's attaching the 2x4 to whatever it is that it's attached to/supported by.
My unscientific, short answer is "no", I wouldn't depend on a 2x4 of unknown provenance to support the weight of 2 adults (or any number of children) + the weight of the swing, especially given wear & degradation over time.
If it was a short run of 2x4 and the swing was attached really well and the 2x4 is attached really well to something strong, yeah, it'll probably work, but if it were my house/family involved, I'd look for a stronger way to hang the swing.
A standard double loop chain is often used for porch swings.
Assuming three hefty occupants (~250 lbs each) and a 100 lb swing, you're up to a load of 850 lbs. If you use 4 lengths of chain to connect the swing, that's 212.5 lbs per chain. So you need at least 2/0 chain, which is rated to a working load of 255 lbs and an UTS of 1020 lbs.
EDIT: If you're a nerd/engineer like me, you can get more technical and take into account angle of the chains from vertical and normal acceleration from swinging. Then your equation comes to:
4*T*cos(Θ)-m*g=m*a where a=v^2/(l*cos(Θ))
or
T=m*((v^2/(l*cos(Θ)))+g)/(4*cos(Θ))
where
T is the tension per chain length using a total of 4 chains,
m is the total mass of the system,
a is normal acceleration,
l is the chain length,
v is maximum horizontal velocity you'll be reaching, and
Θ is the angle of the chains from the vertical
which will be a bit more than 255 lbs if you're swinging very fast, or have very short chains or a very wide swing. In that case, you may want to go with 3/0 chain.
Resource
Best Answer
I would be installing 4"x4" joist hangers between the rafters and set in pieces of 4x4 P.T. lumber. If you are hanging the bed with chains, I'd drill through the 4x4's at the appropriate spot and insert bolts with washers and nuts on top. Good luck.