JavaFX Comment définir l'image d'arrière-plan de la scène
Comment puis-je définir l'image d'arrière-plan d'une scène?
3 answers
Une des approches peut être comme ceci:
1) Créer un fichier CSS avec le nom "style.css " et y définir un sélecteur d'id:
#pane{ -fx-background-image: url("background_image.jpg"); -fx-background-repeat: stretch; -fx-background-size: 900 506; -fx-background-position: center center; -fx-effect: dropshadow(three-pass-box, black, 30, 0.5, 0, 0); }
2) Définissez l'id du contrôle le plus haut (ou de tout contrôle) de la scène avec une valeur définie en CSS et chargez ce fichier CSS dans la scène:
public class Test extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
StackPane root = new StackPane();
root.setId("pane");
Scene scene = new Scene(root, 300, 250);
scene.getStylesheets().addAll(this.getClass().getResource("style.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
}
}
Vous pouvez également donner un id au contrôle dans un fichier FXML:
<StackPane id="pane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml" fx:controller="demo.Sample">
<children>
</children>
</StackPane>
Pour plus d'informations sur le style CSS JavaFX, reportez-vous à ce guide.
je sais que c'est une vieille Question
Mais au cas où vous voudriez le faire par programmation ou à la manière java
Pour les Arrière-plans D'image; vous pouvez utiliser BackgroundImage class
BackgroundImage myBI= new BackgroundImage(new Image("my url",32,32,false,true),
BackgroundRepeat.REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.DEFAULT,
BackgroundSize.DEFAULT);
//then you set to your node
myContainer.setBackground(new Background(myBI));
Pour Peindre ou Remplir les arrière-plans; vous pouvez utiliser BackgroundFill class
BackgroundFill myBF = new BackgroundFill(Color.BLUEVIOLET, new CornerRadii(1),
new Insets(0.0,0.0,0.0,0.0));// or null for the padding
//then you set to your node or container or layout
myContainer.setBackground(new Background(myBF));
Maintient votre java vivant && votre css mort..
Vous pouvez changer le style directement pour la scène en utilisant .root
class:
.root {
-fx-background-image: url("https://www.google.com/images/srpr/logo3w.png");
}
Ajoutez ceci à CSS et chargez-le comme "Uluk Biy" décrit dans sa réponse.