使用此代码将按钮涂成红色。
try {
if (thePane.getChildren() != null) {
for (Node node : thePane.getChildren()) {
if (node instanceof GridPane) {
ObservableList<Node> nodes = ((GridPane) node).getChildren();
for (Node node1 : nodes) {
if (node1 instanceof ToggleButton) {
ToggleButton toggle= ((ToggleButton) node1);
if (toggle!= null) {
if (toggle.getId().equals(id)) {
if (toggle.isSelected()) {
toggle.styleProperty().setValue("-fx-background-color: #ff0000,linear-gradient(#a81010 0%, #ba2525 20%, #ea2525 100%),linear-gradient(#a81010, #ea2525),radial-gradient(center 50% 0%, radius 100%, rgba(214,21,21,0.9), rgba(255,255,255,0));");
} else {
toggle.styleProperty().setValue("-fx-background-color: #a50000,linear-gradient(#870909 0%, #c93434 20%, #a00303 100%), linear-gradient(#870909, #a00303), radial-gradient(center 50% 0%, radius 100%, rgba(148,114,114,0.9), rgba(255,255,255,0));");
}
}
}
}
}
}
}
}
}
并使用以下几行代码将一些按钮添加到 FadeTransition 作为 Paralleltransition 的子级以制作 flash。
try{
ToggleButton toggle= getToggleButton();
FadeTransition fadeTransition = new FadeTransition(Duration.seconds(0.9), toggle);
fadeTransition.setNode(toggle);
fadeTransition.setFromValue(10.0);
fadeTransition.setToValue(0.0);
fadeTransition.setCycleCount(Animation.INDEFINITE);
if (fadeTransitionList.get(toggle.getId()) == null) {
parallelTransition.stop();
parallelTransition.getChildren().add(fadeTransition);
parallelTransition.play();
fadeTransitionList.put(toggle.getId(), fadeTransition);
}
} catch (Exception e){
e.printStackTrace();
}
正如我所说,不相关的按钮(尚未添加到 ParallelTransition(也称为 FadeTransition)的)不透明度会发生变化。
我认为 JavaFX 中的淡入淡出过渡针对屏幕中的某个区域运行,并且该区域会影响不相关的按钮
如有任何帮助,我们将不胜感激。
/**
* Flash a health tree node using the FadeTransition animation
*
* @param item The TreeCell item
* @param rate Flashing rate per second
* @param mins Number of mins to flash
*/
private void flash(HealthItem item, int rate, int mins) {
FadeTransition fadeTransition = new FadeTransition(Duration.seconds(1.0 / rate), this);
fadeTransition.setFromValue(1.0);
fadeTransition.setToValue(0.0);
fadeTransition.setCycleCount(mins * rate * 60);
fadeTransition.setAutoReverse(true);
fadeTransition.setOnFinished(event -> {
item.stopFlash();
});
item.setFlash(fadeTransition); // This method will call the fadeTransition.play()
}
根据我对 FadeTransition 的观察,我将研究实现目标的替代方法。