The answer to this question has puzzled people for centuries. In fact this is a problem that psychologists are faced with every day as they "try" to help people with their problems. The truth is the answer to this is found in Scripture.
Paul's letter to the Philippian church deals with this very issue. Someone has said that Philippians is God's psychology book. In this letter Paul deals with the topic of joy. Not the kind of joy that the world seeks after, but the kind of joy that can only come to those who know Jesus Christ as their personal savior.
The history behind this book is in Acts 16. Paul and Silas had come to the city of Philippi, which was a Roman colony of foreign soil in the country of Macedonia. The city was named after King Philip who won it. It was primarily a military town whose main business was to ward off attacks from Barbarians and preserve Roman peace on the edges of the empire. Three groups of people constituted the city: Roman colonists who dominated the city, Old Macedonians who were numerically the largest group, and a mixture of Orientals. Very few Jews lived in the city because ot its military emphasis... (not even enough to have a synagogue).
HOW TO HAVE JOY...
The underlying thought in the book of Philippians is that no matter what happens to God's people, no matter what the circumstances are in their lives, God's people can have "real" joy!
Keep in mind... The right kind of thinking will produce joy in any situation for the child of God.