Javafx我想加个背景
时间: 2023-08-22 19:10:08 浏览: 211
javafx_tableview设置背景透明和滚动条
5星 · 资源好评率100%
可以在Pane中添加一个Background来设置背景,具体操作如下:
1. 定义一个背景:
```
BackgroundFill backgroundFill = new BackgroundFill(Color.LIGHTBLUE, CornerRadii.EMPTY, Insets.EMPTY);
Background background = new Background(backgroundFill);
```
这里定义的背景是浅蓝色的。
2. 在Pane中设置背景:
```
Pane rootNode = new Pane();
rootNode.setBackground(background);
```
将上面定义好的背景设置给Pane即可。
完整代码如下:
```
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.PathTransition;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Ellipse;
import javafx.stage.Stage;
import javafx.util.Duration;
public class FXTest6 extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
// 定义背景
BackgroundFill backgroundFill = new BackgroundFill(Color.LIGHTBLUE, CornerRadii.EMPTY, Insets.EMPTY);
Background background = new Background(backgroundFill);
Image image = new Image(getClass().getResourceAsStream("previewFix.jpg"), 600, 400, true, true);
ImageView iv = new ImageView();
Pane rootNode = new Pane();
Ellipse ellipse = new Ellipse(300, 200, 100, 50);
ellipse.setFill(Color.WHITE);
ellipse.setStroke(Color.BLACK);
Circle circle = new Circle(250, 200, 20);
circle.setFill(Color.BLACK);
PathTransition pt = new PathTransition(Duration.millis(4500), ellipse, circle);
pt.play();
Timeline timeline = new Timeline();
timeline.setCycleCount(Animation.INDEFINITE);
timeline.setAutoReverse(true);
KeyValue keyValue = new KeyValue(circle.rotateProperty(), 360);
KeyFrame keyFrame = new KeyFrame(Duration.seconds(2), keyValue);
timeline.getKeyFrames().add(keyFrame);
timeline.play();
rootNode.getChildren().addAll(ellipse, circle, iv);
rootNode.setBackground(background);
Scene scene = new Scene(rootNode, 600, 400);
primaryStage.setTitle("移动动画");
primaryStage.setScene(scene);
primaryStage.show();
}
}
```
运行后,应该就能看到带有背景的动画界面了。
阅读全文