In a multi-screen environment, this can be a little more difficult, as you need to take into a lot of variables. You can think of a multi-screen environment as a "virtual screen", where the default screen may not be at 0x0
. Also, add into the mix things like OS menu/task bars and you have another series of things to juggle.
The following example displays two frames, both aligned along the bottom of the "safe viewable area" of the "default" screen and positioned on the left and right side of the "safe viewable area" of the "default" screen
import java.awt.EventQueue;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
JFrame happyFrame = new JFrame();
happyFrame.add(new HappyPane());
happyFrame.pack();
JFrame sadFrame = new JFrame();
sadFrame.add(new SadPane());
sadFrame.pack();
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice screenDevice = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = screenDevice.getDefaultConfiguration();
Rectangle bounds = screenDevice.getDefaultConfiguration().getBounds();
Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
bounds.x += insets.left;
bounds.y += insets.top;
bounds.width -= insets.left + insets.right;
bounds.height -= insets.top + insets.bottom;
happyFrame.setLocation(bounds.x, (bounds.y + bounds.height) - happyFrame.getSize().height);
sadFrame.setLocation((bounds.x + bounds.width) - sadFrame.getSize().width, (bounds.y + bounds.height) - sadFrame.getSize().height);
happyFrame.setVisible(true);
sadFrame.setVisible(true);
} catch (IOException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
public class HappyPane extends JPanel {
public HappyPane() throws IOException {
setBorder(new EmptyBorder(16, 16, 16, 16));
setLayout(new GridBagLayout());
add(new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("/images/happy.png")))));
}
}
public class SadPane extends JPanel {
public SadPane() throws IOException {
setBorder(new EmptyBorder(16, 16, 16, 16));
setLayout(new GridBagLayout());
add(new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("/images/sad.png")))));
}
}
}
You can also have a look at
for more information then you probably really wanted to know ;)