2

I am trying to create a program that launches any number of sub windows. How can I make it that each subsequent window opened is opened shifted to the bottom right so all the windows can be seen at the same time?

I have been able to make this offset with a static variable but windows will eventually be put off screen. How can I prevent that?

To fully understand my question try launching multiple calculator windows and watch the behavior. Is there a way to emulate that in Java?

Here's what I'm doing currently

private static int locationOfset = 0;

public ATMWindow(ATM atm) {
    ...
    setBounds(500 + (50 * locationOfset), 300 + (50 * locationOfset), 450, 300);
    locationOfset++;
    ...
}

Before you suggest CardLayout or something similar, I truly do want multiple JFrames if you can believe it.

Ryan Jackman
  • 780
  • 2
  • 8
  • 24

1 Answers1

2

First read and understand The Use of Multiple JFrames: Good or Bad Practice?

The easiest way would be to use a public static field to maintain the current (or future) position, so that when ever you created a new frame you could use something like...

setLocation((currentX += LOCATION_OFFSET), (currentY += LOCATION_OFFSET));

Where currentX and currentY are public static int values that hold the current offset position and LOCATION_OFFSET is a public static final int value that maintains the amount of the offset.

currentX and currentY could be child values of the parent class or a utility class as you see fit

The most significant issue with this is that it's now possible to run a window off the users screen...

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366