When Japan attacked the US in 1941, why did the US go to war against all the Axis powers? I know Hitler declared war against the US, and that the Allies were much closer to the US. But shouldn't the US have used its resources to defeat the attacker first?
You have to understand that the Americans and British had a mutual understanding on the severity of the German threat well before America's formal entry into the war, with the former having been in what amounted to an undeclared war with Germany for some time prior to the events of December 7th, 1941.
American merchantmen sailing for Europe were sometimes attacked by German submarines. By mid-1941, President Roosevelt had declared most of the Atlantic (out to Iceland) to be part of the Pan-American Security Zone, and American warships were sailing alongside Commonwealth vessels in joint convoy structures and had absolutely been involved in combat with German naval forces prior to the formal entry of the United States into the war. Even before this, American warships that encountered German subs on the open ocean would report their positions on open radio channels, providing intelligence to British warships while avoiding overt acts of hostility. Additionally, American forces established garrisons on Greenland and Iceland (with the latter island having previously bee preemptively occupied by the British) to gather intelligence on both weather conditions and German naval movements.
There had been a substantial amount of diplomatic communication between the Churchill and Roosevelt governments regarding the German threat prior to any formal American declaration of war. The Atlantic Charter, signed three months before Pearl, clearly included a vision of a post-war world with the Anglo-American led Allies as the victors. There had been substantial strategic coordination between American and Commonwealth staff officers well prior to that, as many in both military establishments saw American entry into the war as inevitable.
Even though Roosevelt knew that the American public did not have the stomach for direct war (prior to Pearl Harbor), he still pulled a lot of strings to assist the British in their fight against Germany. As for the adoption of a "Europe First" strategy (your original question), the dire strategic situation of the British Isles (surrounded by a hostile occupied Europe save for the embattled and somewhat untrustworthy Soviets in the East) meant that Allied offensive efforts would be focused on defeating Germany in Africa and later Europe.
Japan, while considered a strategic threat to Western holdings in the Far East, was not a direct threat to the home territory of one the leading members of the Allies and would warrant a holding action and limited counter-attacks until the upper-hand could be regained against the Germans. Still, in practice, the most fierce fighting of the initial American entry into the war would be over her Pacific holdings (many of which would, as you know, fall into Japanese hands) as well as naval actions against the Japanese at Coral Sea and Midway.
Even though Japan had been the one to directly assault American holdings, Britain's desperate need for relief from German aggression meant that America would gear her efforts towards assisting her rather than immediately going on the offensive against Japan. Remember, the US had been knocked strategically off balance in the opening salvos of the Pacific War, with many of its important bases and facilities in enemy hands and much her immediately available naval assets destroyed or damaged. If the US waited to shore up her Pacific resources and regain the upper-hand against Japan, they might risk a British collapse and the partial or complete loss of their major ally.
On December 8th 1941. President Franklin Roosevelt asked Congress to declare war on Japan. American public opinion was behind concentrating on Japan first. Hitler then did something completely out of character. He decided to abide by a treaty for once and declared war on the United States. Ironically, since his alliance with Japan was a defensive alliance, Hitler was under no legal obligation to declare war on the United States, since Japan started that war.
The German declaration of war allowed FDR to do what he really wanted to do, which was concentrate on the strongest Axis power (Germany) first. He had already got Lend Lease passed and had instituted a shoot on sight policy against German U-boats in the western Atlantic, but isolationist sentiment was to strong for him to ask congress to declare war on Germany, prior to December 8th 1941. He did not even do it then either, but Hitler declared war anyway.
Roosevelt and Winston Churchill met at Placenta Bay, Newfoundland in August 1941. They signed the Atlantic Charter and both leaders decided Germany was the biggest threat. They decided they should defeat Germany first. This was reaffirmed at the Acadia conference in late December 1941. It was not a popular decision in the United States.
Sources; "The Grand Alliance" Vol. 3 of Winston Churchill's history of World War II
"The Oxford Companion to World War II" by I.C.B Dear, also has an article about the Atlantic Charter that details why the Anglo-American powers decided on a Germany first strategy.