unico scopo di questa classe è quello di permettere finestra non decorato per essere trascinata. Inoltre, si impegna a garantire che TaskBar rimanga visibile con FullScreen e garantisce che la finestra non decorata non venga trascinata fuori dallo schermo. Infine fornisce una correzione per l'errore "risorsa css non trovata". Basta incollare sotto il codice nella classe principale nel metodo startrecoverdato() proprio quando lo stage è PRONTO per essere mostrato o dopo.
WindowStyle.allowDrag(root, stage);
WindowStyle.stageDimension(stage.getWidth(), stage.getHeight());
NOTA: Incollare quanto sopra quando lo stage è PRONTO per essere visualizzato o dopo. Per uso completo finestra a schermo:
WindowStyle.fullScreen(Stage stage);
Per ridimensionare ritorna al precedente utilizzo:
WindowStyle.restoreScreen(Stage stage);
Per aggiungere fogli di stile personalizzati per la tua scena, Basta incollare sotto codice nella classe principale l'inizio override() metodo dopo aver definito la scena.
scene.getStylesheets().add(WindowStyle.addStyleSheet(String css));
Il nome del CSS da utilizzare per lo styling possono essere sotto forma di: main.css
o styles/main.css
import javafx.geometry.Rectangle2D;
import javafx.scene.Parent;
import javafx.scene.input.MouseEvent;
import javafx.stage.Screen;
import javafx.stage.Stage;
/**
* @author: BENJAH
*/
public class WindowStyle {
private static final Rectangle2D SCREEN_BOUNDS= Screen.getPrimary()
.getVisualBounds();
private static double[] pref_WH, offset_XY;
private static String styleSheet;
private WindowStyle(String css) {
styleSheet= getClass().getResource(css).toString();
}
protected static void allowDrag(Parent root, Stage stage) {
root.setOnMousePressed((MouseEvent p) -> {
offset_XY= new double[]{p.getSceneX(), p.getSceneY()};
});
root.setOnMouseDragged((MouseEvent d) -> {
//Ensures the stage is not dragged past the taskbar
if (d.getScreenY()<(SCREEN_BOUNDS.getMaxY()-20))
stage.setY(d.getScreenY() - offset_XY[1]);
stage.setX(d.getScreenX() - offset_XY[0]);
});
root.setOnMouseReleased((MouseEvent r)-> {
//Ensures the stage is not dragged past top of screen
if (stage.getY()<0.0) stage.setY(0.0);
});
}
//Sets the default stage prefered width and height.
protected static void stageDimension(Double width, Double height) {
pref_WH= new double[]{width, height};
}
protected static void fullScreen(Stage stage) {
stage.setX(SCREEN_BOUNDS.getMinX());
stage.setY(SCREEN_BOUNDS.getMinY());
stage.setWidth(SCREEN_BOUNDS.getWidth());
stage.setHeight(SCREEN_BOUNDS.getHeight());
}
protected static void restoreScreen(Stage stage) {
stage.setX((SCREEN_BOUNDS.getMaxX() - pref_WH[0])/2);
stage.setY((SCREEN_BOUNDS.getMaxY() - pref_WH[1])/2);
stage.setWidth(pref_WH[0]);
stage.setHeight(pref_WH[1]);
}
protected static String addStyleSheet(String css) {
new WindowStyle(css);
return styleSheet;
}
}
fonte
2017-12-21 17:27:16
L'alloggio è al di sopra nasconde la barra del titolo tutti insieme? Come pensi di trascinare la finestra? – SoulMan
Sì NeilGhosh, la proprietà precedente nasconde l'intera barra del titolo. –