在多显示器环境中保存JFrame位置 [英] Save JFrame location in multi-display environment
问题描述
当用户关闭JFrame时,我想存储它的位置(bounds,extendedState).但是,当用户将框架移到第二屏幕并将其最大化时,如何存储该信息?我的幼稚(和单个显示)实现是这样的:
I want to store a JFrame's location (bounds, extendedState) when the user closes it. However, when the user moves the frame onto the 2nd screen and maximizes it, how can I store that information? My naive (and single display) implementation is like this:
void saveFrame(JFrame frame) throws IOException {
Properties props = new Properties();
props.setProperty("State", String.valueOf(frame.getExtendedState()));
props.setProperty("X", String.valueOf(frame.getX()));
props.setProperty("Y", String.valueOf(frame.getY()));
props.setProperty("W", String.valueOf(frame.getWidth()));
props.setProperty("H", String.valueOf(frame.getHeight()));
props.storeToXML(new FileOutputStream("config.xml"), null);
}
void loadFrame(JFrame frame) throws IOException {
Properties props = new Properties();
props.loadFromXML(new FileInputStream("config.xml"));
int extendedState = Integer.parseInt(props.getProperty("State", String.valueOf(frame.getExtendedState())));
if (extendedState != JFrame.MAXIMIZED_BOTH) {
frame.setBounds(
Integer.parseInt(props.getProperty("X", String.valueOf(frame.getX()))),
Integer.parseInt(props.getProperty("Y", String.valueOf(frame.getY()))),
Integer.parseInt(props.getProperty("W", String.valueOf(frame.getWidth()))),
Integer.parseInt(props.getProperty("H", String.valueOf(frame.getHeight())))
);
} else {
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
}
如何找到框架在哪个屏幕上? 如何将框架移动到第二个屏幕并将其最大化?
How can I discover on which screen the frame is located? How can I move a frame to the second screen and maximize it there?
推荐答案
查找使用的图形设备的ID:
To find the ID of the used graphics device:
frame.getGraphicsConfiguration().getDevice().getIDString()
反之,则可以通过以下方式找到图形设备:
Going the other way, you can find the graphics devices with:
GraphicsEnvironment.getLocalGraphicsEnvironment().getDevices()
然后可以在JFrame
构造函数中使用设备中的配置.我不相信您可以在构建后进行设置.
You can then use a configuration from the device in the JFrame
constructor. I don't believe you can set it after construction.
当然,您应该小心,例如,不要改变屏幕的框架,因为分辨率已更改.
Of course, you should be careful not to, say, open the frame off screen because the resolution has change.
这篇关于在多显示器环境中保存JFrame位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!