javaFX 中给label 加背景图片

从fxml文件里获取Label(标签) manageIcon,并为label 添加背景
@FXML   private Label manageIcon;
//fxml文件里的label ,fxid:manageIcon
Image image;
image = new Image(getClass().getResourceAsStream("manage.png"),40,40,false,false);   
//图片为manage.png  设置为40*40 的大小显示     
manageIcon.setGraphic(new ImageView(image));

效果图如下:

 


  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以在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(); } } ``` 运行后,应该就能看到带有背景的动画界面了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值