java - 如何在桌面应用程序中使用 CSS 样式使 Javafx 中的文本或图像闪烁?

标签 java javafx javafx-2 javafx-8 fxml

我正在尝试在 JavaFX 中闪烁标签和图像。

如何在 JavaFX 中实现?

我是这个平台的新手,给我一些指导。

最佳答案

我引用了以上我开发的简单示例。 下面是我的 Fxml 文件:

FxmlDocument.fxml:
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="AnchorPane" prefHeight="283.0" prefWidth="320" stylesheets="@application.css" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="blinkimagecolor.FXMLDocumentController">
   <children>
      <Label fx:id="label" alignment="CENTER" layoutX="40.0" layoutY="36.0" prefHeight="131.0" prefWidth="232.0" style="-fx-background-image: url('/images/Printer_T.png'); -fx-background-position: center; -fx-background-repeat: no-repeat;" stylesheets="@application.css" />
      <Button fx:id="button" layoutX="134.0" layoutY="209.0" mnemonicParsing="false" onAction="#buttonpressAction" text="Button" />
   </children>
</AnchorPane>

下面是我的 Controller 类:

FXMLDocumentController.java:-
import java.net.URL;
import java.time.Duration;
import java.util.ResourceBundle;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.css.PseudoClass;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;


public class FXMLDocumentController implements Initializable {

    @FXML
    private Label label;
    @FXML
    private Button button;

    Timeline flasher ;
    PseudoClass flashHighlight;
    int i = 0;
    @Override


    public void initialize(URL url, ResourceBundle rb) {
        // TODO
        flashHighlight = PseudoClass.getPseudoClass("flash-highlight");
        flasher = new Timeline(new KeyFrame(javafx.util.Duration.seconds(0.5), e -> {
            label.pseudoClassStateChanged(flashHighlight, true);
        }),
                new KeyFrame(javafx.util.Duration.seconds(1.0), e -> {
            label.pseudoClassStateChanged(flashHighlight, false);
        })
    );
    flasher.setCycleCount(Animation.INDEFINITE);

    }    

    @FXML
    private void buttonpressAction(ActionEvent event) {

        if(i == 0){
            flasher.play();
            i = 1;
            System.out.println("start method");
        }else{
            flasher.stop();
            i = 0;
            System.out.println("stop method");
            label.pseudoClassStateChanged(flashHighlight, false);
        }
        System.out.println("Out of button function");

    }

}

下面是我的主类:

public class BlinkImageColor extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

CSS 文件是:

application.css:-
:flash-highlight{
    -fx-background-color:red;
}

这个工作正常,但是如果我想多次应用相同的东西,我如何在不创建多个 Timeline 的情况下做到这一点我如何同时对多个标签使用相同的时间轴独立的时间。

Output of above code

当我们点击按钮时,背景颜色会闪烁红色,如果我们点击同一个按钮,它就会停止闪烁。

像上面一样,如果我们有多个按钮和多个标签,它们需要像上面的按钮点击一样独立工作。具有共同的时间表。我们该怎么做?

关于java - 如何在桌面应用程序中使用 CSS 样式使 Javafx 中的文本或图像闪烁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42314086/

相关文章:

java - 您将使用哪种数据结构 : TreeMap or HashMap? (Java)

JavaFX 图形故障

java - 使用 GroovyFX 实现新的 JavaFX 组件?

tableview - 如何禁用 tableView 中表列的重新排序?

java - IP 地址问题 java 在 Web 浏览器中部署(使用 dtjava.js 部署)

java http请求所需的字符串参数 'Username'不存在

java - 删除多行Edittext android中的下一行可见性

java - 无法将整数文件写入java中的文件

JavaFX Spinner 抛出 IllegalArgumentException : The start must be <= the end

带有 ChangeListener 的 JavaFX TableView 用于多选