To address this issue, you can try adjusting the display settings on your iMac. First, ensure that your displays are configured correctly in the System Preferences. You can access this by clicking on the Apple menu in the top-left corner of your screen, then selecting "System Preferences," and choosing "Displays." Here, you can arrange the displays to match their physical layout on your desk.
Once you've arranged the displays correctly, you can also check the "Mission Control" preferences. Mission Control allows you to manage multiple desktops and displays. In System Preferences, navigate to "Mission Control" and ensure that the option "Displays have separate Spaces" is enabled. This setting will ensure that each monitor retains its own set of desktop spaces and windows, helping to prevent windows from moving between monitors unexpectedly.
Additionally, be mindful of how you're interacting with windows and applications. Sometimes, accidentally dragging a window to the edge of a screen can cause it to switch to another display.
By adjusting these settings and being mindful of your interactions, you should be able to keep your windows and applications where you want them on your multiple monitors setup.