The king got told by an oracle that his daughter Danae would have a son that would kill him, but he wasn't just going to let her have son that would kill him, so his solution was to lock her up in a room so she never had that child.
Soon the people that brought Danae food started telling other people from the kingdom that she was going the have a child, and soon the king found out about it and decided to put them both inside a box and hoped they'd starve to death or drown.