After the war with Midian, God commanded to divide the captive people and animals between the warriors, the people, the Levites, and a "terumah for Hashem" to the kohanim. However, the non-living spoils did not need to be divided (Bemidbar 31:53). Nevertheless, the princes decided on their own to offer the captured gold to the mishkan. Why did they do this, and why only the gold?
The war with Midian was in retaliation for Midian's role in the Baal Peor episode. This episode was very similar to the golden calf episode. Both involved idolatry as well as dissolute behavior. Both had a plague break out as punishment. In both the uncorrupted religious leaders (Moshe and Pinchas) had people killed.
With the golden calf, the people took their gold and decided to make it into an idol. I think that when the princes here dedicated their gold to the mishkan, they were making a statement: "This time is not like the previous time. This time, we are not using our gold to make an idol, instead we are using it to serve God. The people sinned, but now we are taking responsibility and fixing that ourselves."
(As I wrote in the past, I think with the Golden Calf too the people eventually made a gesture of repentance.)