멀티 디스플레이 환경에서 JFrame 위치를 저장하십시오
문제
사용자가 닫을 때 JFrame의 위치 (경계, 확장 상태)를 저장하고 싶습니다. 그러나 사용자가 프레임을 두 번째 화면으로 이동하여 최대화하면 해당 정보를 어떻게 저장할 수 있습니까? 순진한 (및 단일 디스플레이) 구현은 다음과 같습니다.
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);
}
}
프레임이 어떤 화면에서 있는지 어떻게 발견 할 수 있습니까? 프레임을 두 번째 화면으로 이동하고 최대화하려면 어떻게해야합니까?
해결책
사용 된 그래픽 장치의 ID를 찾으려면 :
frame.getGraphicsConfiguration().getDevice().getIDString()
다른 방법으로, 당신은 다음과 같이 그래픽 장치를 찾을 수 있습니다.
GraphicsEnvironment.getLocalGraphicsEnvironment().getDevices()
그런 다음 장치에서 구성을 사용할 수 있습니다. JFrame
건설자. 나는 당신이 시공 후 그것을 설정할 수 있다고 믿지 않습니다.
물론 해상도가 변경되어 있으므로 화면에서 프레임을 열지 않도록주의해야합니다.
제휴하지 않습니다 StackOverflow