mainClass.java:
public class mainClass extends Application{
Scene scene;
Group group;
@Override
public void start(Stage stg) throws Exception{
stg.setTitle("Hi");
stg.setWidth(600); stg.setHeight(250);
group = new Group();
scene = new Scene(group, 600, 250);
scene.getStylesheets().add("mainClass.css");
stg.setScene(scene);
stg.centerOnScreen();
stg.show();
}
public static void main(String[] args) {
launch(args);
}
}
mainClass.css
.mainClass-scene{
-fx-background-image: url("background.jpeg");
-fx-background-repeat: stretch;
-fx-background-size: 600 250;
-fx-background-position: center center;
-fx-effect: dropshadow(three-pass-box, black, 30, 0.5, 0, 0);
}
应用程序工作通常我显示我的框架,但我无法在场景中看到背景图像。也许css不工作我怎么能解决这个问题?
Sory因为我的英语不好谢谢你。
答案 0 :(得分:3)
您的程序存在很多问题。
mainClass-scene
但从不使用它。-fx-background-image
属性仅适用于Regions,但您没有任何区域。
这是一个适用于我的更新版本并生成上面的图像。
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class MainClass extends Application{
@Override public void start(Stage stage) throws Exception{
stage.setTitle("Hi");
StackPane layout = new StackPane();
layout.getStylesheets().add("mainClass.css");
layout.getStyleClass().add("main-class");
Scene scene = new Scene(layout, 600, 250);
scene.getStylesheets().add(
getClass().getResource("mainClass.css").toExternalForm()
);
stage.setScene(scene);
stage.setResizable(false);
stage.centerOnScreen();
stage.show();
}
public static void main(String[] args) { launch(args); }
}
相关的css:
/**
* file: mainClass.css
* Place in same directory as MainClass.java.
* Have your build system copy this file to your build output directory.
**/
.main-class {
-fx-background-image: url('http://icons.iconarchive.com/icons/mirella-gabriele/fantasy-mediaeval/256/Poison-blue-icon.png');
-fx-background-repeat: stretch;
-fx-background-size: 570 220;
-fx-background-position: center center;
-fx-effect: dropshadow(three-pass-box, black, 30, 0.5, 0, 0);
-fx-background-insets: 30;
}