例子1:显示4个狗头。正常显示左上角、右下角的狗头;右上角的狗头旋转180°,并设置了透明度;左下角的狗头旋转90°,也设置了透明度。

 1 import javafx.application.Application;
2 import javafx.geometry.Insets;
3 import javafx.scene.Scene;
4 import javafx.scene.image.Image;
5 import javafx.scene.image.ImageView;
6 import javafx.scene.layout.GridPane;
7 import javafx.stage.Stage;
8
9 public class Main extends Application {
10
11 public static void main(String[] args) {
12 launch(args);
13 }
14
15 @Override
16 public void start(Stage primaryStage) throws Exception {
17
18 // Create a pane to hold the images
19 GridPane pane = new GridPane();
20 pane.setStyle("-fx-border-color: green;");
21 pane.setPadding(new Insets(10));
22 pane.setHgap(10); // The width of the horizontal gaps between columns.
23 pane.setVgap(10); // The height of the vertical gaps between rows.
24
25 // Create a image view to hold a image
26 ImageView ivGamer1 = new ImageView("image/Gamer1.jpg");
27 ivGamer1.setFitWidth(100);
28 ivGamer1.setFitHeight(100);
29 pane.add(ivGamer1, 0, 0); // Add image view to pane
30
31 // Create a image view to hold a image
32 ImageView ivGamer2 = new ImageView("image/Gamer1.jpg");
33 ivGamer2.setFitWidth(100);
34 ivGamer2.setFitHeight(100);
35 ivGamer2.setStyle("-fx-rotate: 180; -fx-opacity: 0.6;");
36 pane.add(ivGamer2, 1, 0);
37
38 // Create a image view to hold a image
39 ImageView ivGamer3 = new ImageView("image/Gamer1.jpg");
40 ivGamer3.setFitWidth(100);
41 ivGamer3.setFitHeight(100);
42 ivGamer3.setStyle("-fx-rotate: 90; -fx-opacity: 0.2;");
43 pane.add(ivGamer3, 0, 1);
44
45 // Create a image view to hold a image
46 Image iGamer4 = new Image("image/Gamer2.jpg");
47 ImageView ivGamer4 = new ImageView(iGamer4);
48 ivGamer4.setFitWidth(100);
49 ivGamer4.setFitHeight(100);
50 pane.add(ivGamer4, 1, 1);
51
52 // Create a scene
53 Scene scene = new Scene(pane);
54
55 primaryStage.setScene(scene);
56 primaryStage.setTitle("Gamers");
57 primaryStage.setResizable(true);
58 primaryStage.show();
59 }
60 }

运行效果:

例子2:以半透明的状态显示两个狗头,当点击其中某一个狗头,就让它变成不透明,另一个保持半透明。当将鼠标移进某一狗头所在的区域,而不点击时,该狗头变成不透明,另一个狗头变成半透明;当鼠标移出该狗头所在的区域,恢复两个狗头原来的状态。

  1 import javafx.application.Application;
2 import javafx.event.EventHandler;
3 import javafx.geometry.Insets;
4 import javafx.scene.Scene;
5 import javafx.scene.image.Image;
6 import javafx.scene.image.ImageView;
7 import javafx.scene.input.MouseEvent;
8 import javafx.scene.layout.GridPane;
9 import javafx.stage.Stage;
10
11 public class Main extends Application {
12
13 String opacityOfGamer1 = "-fx-opacity: 0.5;";
14 String opacityOfGamer2 = "-fx-opacity: 0.5;";
15
16 public static void main(String[] args) {
17 launch(args);
18 }
19
20 @Override
21 public void start(Stage primaryStage) throws Exception {
22 // Create a pane to hold the images
23 GridPane pane = new GridPane();
24 pane.setStyle("-fx-border-color: green;");
25 pane.setPadding(new Insets(10));
26 pane.setHgap(10); // The width of the horizontal gaps between columns.
27 pane.setVgap(10); // The height of the vertical gaps between rows.
28
29 // Create a image view to hold a image
30 ImageView ivGamer1 = new ImageView("image/Gamer1.jpg");
31 ivGamer1.setFitWidth(100);
32 ivGamer1.setFitHeight(100);
33 ivGamer1.setStyle("-fx-opacity: 0.5;");
34 pane.add(ivGamer1, 0, 0); // Add image view to pane
35
36 // Create a image view to hold a image
37 Image iGamer2 = new Image("image/Gamer2.jpg");
38 ImageView ivGamer2 = new ImageView(iGamer2);
39 ivGamer2.setFitWidth(100);
40 ivGamer2.setFitHeight(100);
41 ivGamer2.setStyle("-fx-opacity: 0.5;");
42 pane.add(ivGamer2, 1, 0);
43
44
45 ivGamer1.setOnMouseClicked(new EventHandler<MouseEvent>() {
46 @Override
47 public void handle(MouseEvent event) {
48 ivGamer1.setStyle("-fx-opacity: 1;");
49 ivGamer2.setStyle("-fx-opacity: 0.5;");
50 opacityOfGamer1 = ivGamer1.getStyle();
51 opacityOfGamer2 = ivGamer2.getStyle();
52 System.out.println("The opacity of the Gamer 1: " + opacityOfGamer1);
53 System.out.println("The opacity of the Gamer 2: " + opacityOfGamer2);
54 System.out.println();
55 }
56 });
57
58 ivGamer1.setOnMouseEntered(new EventHandler<MouseEvent>() {
59 @Override
60 public void handle(MouseEvent event) {
61 ivGamer1.setStyle("-fx-opacity: 1;");
62 ivGamer2.setStyle("-fx-opacity: 0.5;");
63 }
64 });
65
66 ivGamer1.setOnMouseExited(new EventHandler<MouseEvent>() {
67 @Override
68 public void handle(MouseEvent event) {
69 ivGamer1.setStyle(opacityOfGamer1);
70 ivGamer2.setStyle(opacityOfGamer2);
71 }
72 });
73
74 ivGamer2.setOnMouseClicked(new EventHandler<MouseEvent>() {
75 @Override
76 public void handle(MouseEvent event) {
77 ivGamer1.setStyle("-fx-opacity: 0.5;");
78 ivGamer2.setStyle("-fx-opacity: 1;");
79 opacityOfGamer1 = ivGamer1.getStyle();
80 opacityOfGamer2 = ivGamer2.getStyle();
81 System.out.println("The opacity of the Gamer 1: " + opacityOfGamer1);
82 System.out.println("The opacity of the Gamer 2: " + opacityOfGamer2);
83 System.out.println();
84 }
85 });
86
87 ivGamer2.setOnMouseEntered(new EventHandler<MouseEvent>() {
88 @Override
89 public void handle(MouseEvent event) {
90 ivGamer1.setStyle("-fx-opacity: 0.5;");
91 ivGamer2.setStyle("-fx-opacity: 1;");
92 }
93 });
94
95 ivGamer2.setOnMouseExited(new EventHandler<MouseEvent>() {
96 @Override
97 public void handle(MouseEvent event) {
98 ivGamer1.setStyle(opacityOfGamer1);
99 ivGamer2.setStyle(opacityOfGamer2);
100 }
101 });
102
103
104 // Create a scene
105 Scene scene = new Scene(pane);
106
107 primaryStage.setScene(scene);
108 primaryStage.setTitle("Gamers");
109 primaryStage.setResizable(true);
110 primaryStage.show();
111 }
112 }

运行效果:

ImageView's CSS Property

JavaFX ImageView的更多相关文章

  1. 问题记录:JavaFx 鼠标滑轮滚动事件监听!

    问题描述: 在listview的item里面添加鼠标拖拽排序功能.代码如下: setOnMouseDragged(event -> { //设定鼠标长按0.3秒后才可拖拽 防止误操作 isCan ...

  2. javafx之CSS初探

    文档:http://www.haogongju.net/art/1807238 javafx中的css元素必须有-fx-前缀. 一.介绍 java8中新增了javafx.css开放了css相关api. ...

  3. JavaFX Application应用实例

    下面代码演示的是JavaFX进程命令行参数的实例.大家可以参阅一下. /*原文地址:http://www.manongjc.com/article/134.html */ import java.ut ...

  4. JavaFX引入资源问题

    描述 - 使用javafx 引入资源的时候 抛出异常 在swing引入资源 采取相对路径即可,而javafx不是 ImageView imageNode = (ImageView) root.look ...

  5. 【javaFX学习】(二) 控件手册

    这里写的控件可能不是所有的控件,但是应该是比较齐全并足够用的了,后面还有图表类的,3d模型类,放在后面来写吧,太多了.javafx的功能比以前想象中的要强大.而且也很方便,所有的控件写完后再用Scen ...

  6. javafx由浅到深的 认识(一)

    javafx是一款比较新兴的语言框架,随着javafx越来越实用,估计许多程序员也会慢慢接触它,故我在这里对它由浅到深进行介绍一下. 首先,要了解javafx,就应该先知道.xml文件的布局软件,以往 ...

  7. JavaFX——简单的日记系统

    前言 在学习Swing后,听老师说使用Java写界面还可以使用JavaFX.课后,便去了解.JavaFX是甲骨文公司07年推出的期望应用于桌面开发领域的技术.在了解了这个技术几天后,便使用它完成Jav ...

  8. JavaFx 中常见的包和类(javafx笔记 )

    JavaFx 中常见的包和类(javafx笔记 ) 更多详细内容请参考<Pro JavaFX 8>. javafx.stage 包包含以下类: Stage 类 ​ Stage 类是任何 J ...

  9. JAVAFX 2.0 javascript中调用java代码

    现在你已经知道如何在JavaFX中调用JavaScript.在本章中,你将了解到相反的功能——在web页面中调用JavaFX. 大体上的理念是在JavaFX程序中创建一个接口对象,并通过调用JSObj ...

随机推荐

  1. Vuex 注入 Vue 生命周期的过程

    首先我们结合 Vue 和 Vuex 的部分源码,来说明 Vuex 注入 Vue 生命周期的过程. 说到源码,其实没有想象的那么难.也和我们平时写业务代码差不多,都是方法的调用.但是源码的调用树会复杂很 ...

  2. Unable to add window -- token null is not for an application错误的解决方法 android开发

    Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not f ...

  3. Unity动态绑定按钮触发方法

    在使用unity制作UI的过程中,基本都需要接触到按钮,然后按钮要起作用的话,那么就需要为按钮绑定响应方法. 为按钮绑定触发的方法,我知道的有两种方法,第一种:手动使用unityEditor 绑定,另 ...

  4. java初探(1)之秒杀中的rabbitMQ

    rabbitMQ 消息队列,通过一定的通信协议,生产者和消费者在应用程序内传递通信. 主要的作用,提高负载,减耦合. 场景描述:当点击秒杀按钮的那个时刻,有很高的并发量,客户端发出请求之后,会判断库存 ...

  5. 洛谷 P4093 [HEOI2016/TJOI2016]序列 CDQ分治优化DP

    洛谷 P4093 [HEOI2016/TJOI2016]序列 CDQ分治优化DP 题目描述 佳媛姐姐过生日的时候,她的小伙伴从某宝上买了一个有趣的玩具送给他. 玩具上有一个数列,数列中某些项的值可能会 ...

  6. Ajax请求携带Cookie

    目录 xhr ajax cookie跨域处理 客户端 服务端 服务端设置跨域的几种方式 方式一 重写addCorsMappings方法 方式二 对单个接口处理 方式三 @CrossOrigin注解 方 ...

  7. Java里一个线程两次调用start()方法会出现什么情况

    Java的线程是不允许启动两次的,第二次调用必然会抛出IllegalThreadStateException,这是一种运行时异常,多次调用start被认为是编程错误. 如果业务需要线程run中的代码再 ...

  8. roarctf_2019_easy_pwn

    这篇博客主要记录当直接在malloc_hook中直接写入one_gadget不起作用时的一些处理方法.题目附件:https://buuoj.cn/challenges#roarctf_2019_eas ...

  9. [03] C# Alloc Free编程

    C# Alloc Free编程 首先Alloc Free这个词是我自创的, 来源于Lock Free. Lock Free是说通过原子操作来避免锁的使用, 从而来提高并行程序的性能; 与Lock Fr ...

  10. 【小白学PyTorch】9 tensor数据结构与存储结构

    文章来自微信公众号[机器学习炼丹术]. 上一节课,讲解了MNIST图像分类的一个小实战,现在我们继续深入学习一下pytorch的一些有的没的的小知识来作为只是储备. 参考目录: @ 目录 1 pyto ...