Pregunta

Quiero almacenar la ubicación de un JFrame (límites, estado extendido) cuando el usuario la cierra. Sin embargo, cuando el usuario mueve el marco a la segunda pantalla y lo maximiza, ¿cómo puedo almacenar esa información? Mi implementación ingenua (y de pantalla única) es así:


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);
    }
}

¿Cómo puedo descubrir en qué pantalla está ubicado el marco? ¿Cómo puedo mover un marco a la segunda pantalla y maximizarlo allí?

¿Fue útil?

Solución

Para encontrar la ID del dispositivo de gráficos utilizado:

frame.getGraphicsConfiguration().getDevice().getIDString()

Yendo hacia el otro lado, puedes encontrar los dispositivos gráficos con:

 GraphicsEnvironment.getLocalGraphicsEnvironment().getDevices()

Puede usar una configuración del dispositivo en el constructor JFrame . No creo que pueda configurarlo después de la construcción.

Por supuesto, debe tener cuidado de, por ejemplo, no abrir el marco fuera de la pantalla porque la resolución ha cambiado.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top