So korrigieren Sie das Mittelfenster auf einem Bildschirm in Java 11 mit Temurin VM/Oracle VMJava

Java-Forum
Anonymous
 So korrigieren Sie das Mittelfenster auf einem Bildschirm in Java 11 mit Temurin VM/Oracle VM

Post by Anonymous »

Ich möchte meinen JFrame in einem 4K -Bildschirm (3840x2400) mit 250% Skalierung für Java 11 zentrieren. Dieser Bildschirm ist ein sekundärer Bildschirm in Windows und links an dem Hauptbildschirm (Full HD, 1920x1080, 100% Skalierung). Ich habe keine weiteren Bildschirme in meinem System./**
* Locates the given window at the center of its current graphics device.
*
* @param aWindow window to center.
*/
public static void center(Window aWindow) { // window was created with a correct GraphicsConfiguration
final Dimension paneSize = aWindow.getSize();
Rectangle screenBounds = aWindow.getGraphicsConfiguration().getBounds();
subtractInsets(aWindow.getGraphicsConfiguration(), screenBounds);
aWindow.setLocation(screenBounds.x + (screenBounds.width - paneSize.width) / 2,
screenBounds.y + (screenBounds.height - paneSize.height) / 2);
}

private static void subtractInsets(GraphicsConfiguration display, Rectangle bounds) {
Insets ins = Toolkit.getDefaultToolkit().getScreenInsets(display);
bounds.x += ins.left;
bounds.y += ins.top;
bounds.width -= ins.left + ins.right;
bounds.height -= ins.top + ins.bottom;
}
< /code>
Wenn ich diesen Code auf Open JDK (Temurin) ausführe, funktioniert dieser Code korrekt. Und wenn ich meine Bildschirmgrenzen drucke, bekomme ich Java.awt.Rectangle [x = -1536, y = 0, Breite = 1536, Höhe = 960]. Aber wenn ich es auf Oracle JDK ausführe, wird mein Fenster auf beide Bildschirme platziert (hässlich) und wenn ich meine Bildschirmgrenzen drucke, bekomme ich Java.awt.Rectangle [x = -3840, y = 0, width = 1536, Höhe = 960]. Hier ist es: < /p>
/**
* Locates the given window at the center of its current graphics device.
*
* @param aWindow window to center.
*/
public static void center(Window aWindow) {
final Dimension paneSize = aWindow.getSize();
Rectangle screenBounds = aWindow.getGraphicsConfiguration().getBounds();
updateBoundsForDisplayIfRequired(screenBounds, aWindow.getGraphicsConfiguration());
subtractInsets(aWindow.getGraphicsConfiguration(), screenBounds);
aWindow.setLocation(screenBounds.x + (screenBounds.width - paneSize.width) / 2,
screenBounds.y + (screenBounds.height - paneSize.height) / 2);
}

private static void subtractInsets(GraphicsConfiguration display, Rectangle bounds) {
Insets ins = Toolkit.getDefaultToolkit().getScreenInsets(display);
bounds.x += ins.left;
bounds.y += ins.top;
bounds.width -= ins.left + ins.right;
bounds.height -= ins.top + ins.bottom;
}

private static void updateBoundsForDisplayIfRequired(@Nonnull Rectangle bounds, @Nonnull GraphicsConfiguration display) {
if (isOracleVM()) {
bounds.x = (int) Math.round(bounds.x * display.getDefaultTransform().getScaleX());
bounds.y = (int) Math.round(bounds.y * display.getDefaultTransform().getScaleY());
bounds.width = (int) Math.round(bounds.width * display.getDefaultTransform().getScaleX());
bounds.height = (int) Math.round(bounds.height * display.getDefaultTransform().getScaleY());
}
}

/**
* Checks whether we are in the Oracle VM. Some AWT features works different with Oracle vs. Open JDK.
*
* @return true if we are in oracle VM.
*/
public static boolean isOracleVM() {
return System.getProperty("java.vendor", "").contains("Oracle");
}
< /code>
Ich bin mir jedoch nicht sicher, ob es sich um eine korrekte Lösung für alle Bildschirmkombinationen und VMs handelt. Kann jemand meinen Code überprüfen/verbessern?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post