Losing one's focus throughout a match happens all the time, even with pro players.
One way to combat this is to do your best to refocus as soon as possible so you can get back on track with your match.
By getting angry you cause yourself to continue to be unfocused: take deep breaths, adjust your strings, focus on your game plan, think positive. Whatever you need to do to relax will help. Developing a ritual before the serve and return will help you refocus for the next point.
If you lose focus because of an unforced error or a double fault then all you need to do is change your attitude about them. Unforced errors happen all the time and it's okay to make mistakes. All you need to do is use unforced errors as feedback to adjust your game plan such as:
"If I'm double faulting then I may want to add more topspin on my first serve."
"If I'm missing too many returns then I should try to block the ball first before I take a full swing."
"If I'm hitting my ground strokes out then I should try to hit more cross-court then down the line."
Maintaining a positive, focused attitude throughout the match by relaxing between points and logically using your mistakes as feedback will help.