JavaFX - FadeOut --> 切换阶段
JavaFX - FadeOut --> Switch Stage
我一直在网上搜索,找不到我的问题的答案,所以我会尝试自己做,希望最好!
我有一个提供欢迎然后淡出的主舞台,但我想在淡出消失的那一刻切换舞台!有人对此有答案吗?
下面的小代码可以更好地理解:
import java.util.Timer;
import java.util.TimerTask;
import javafx.animation.FadeTransition;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.util.Duration;
public class TextFileOPG extends Application {
private Stage switchStage;
@Override
public void start(Stage primaryStage) {
try {
switchStage = primaryStage;
GridPane root = new GridPane();
root.setAlignment(Pos.CENTER);
Text scenetitle = new Text("Welcome");
scenetitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 40));
root.add(scenetitle, 3, 2);
FadeTransition ftOUT = new FadeTransition(Duration.millis(3000), root);
ftOUT.setFromValue(1.0);
ftOUT.setToValue(0.0);
ftOUT.play();
if(ftOUT.equals(Duration.millis(3000)))
{
loggedIn();
}
primaryStage.setTitle("Welcome");
Scene scene = new Scene(root,350,400);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public void loggedIn()
{
switchStage.setTitle("Try");
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
Text thisIsSoCoolText = new Text("Welcome Again");
thisIsSoCoolText.setFont(Font.font("Tahoma", FontWeight.NORMAL, 40));
grid.add(thisIsSoCoolText, 3, 2);
Scene scene = new Scene(grid, 350, 400);
switchStage.setScene(scene);
switchStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
尝试使用时间轴而不是淡入淡出过渡。时间轴可用于各种动画和 UI 更新。
这是对我有用的代码:
import java.util.Timer;
import java.util.TimerTask;
import javafx.animation.FadeTransition;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.util.Duration;
public class TextFileOPG extends Application {
private Stage switchStage;
final double opacity=1;
@Override
public void start(Stage primaryStage) {
try {
switchStage = primaryStage;
GridPane root = new GridPane();
root.setAlignment(Pos.CENTER);
Text scenetitle = new Text("Welcome");
scenetitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 40));
root.add(scenetitle, 3, 2);
primaryStage.setTitle("Welcome");
Scene scene = new Scene(root,350,400);
primaryStage.setScene(scene);
primaryStage.show();
Timeline tick0 = new Timeline();
tick0.setCycleCount(Timeline.INDEFINITE);
tick0.getKeyFrames().add(
new KeyFrame(new Duration(30), new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
root.setOpacity(root.getOpacity()-0.01);
if(root.getOpacity()<0.01){//30 divided by 0.01 equals 3000 so you take the duration and divide it be the opacity to get your transition time in milliseconds
loggedIn();
tick0.stop();
}
}}));
tick0.play();
} catch(Exception e) {
e.printStackTrace();
}
}
public void loggedIn()
{
switchStage.setTitle("Try");
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
Text thisIsSoCoolText = new Text("Welcome Again");
thisIsSoCoolText.setFont(Font.font("Tahoma", FontWeight.NORMAL, 40));
grid.add(thisIsSoCoolText, 3, 2);
Scene scene = new Scene(grid, 350, 400);
switchStage.setScene(scene);
switchStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
而不是
if(ftOUT.equals(Duration.millis(3000))) {
loggedIn();
}
(永远不会起作用:FadeTransition
实例不可能等于 Duration
实例),使用 onFinished
淡入淡出过渡处理程序:
ftOUT.setOnFinished(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
loggedIn();
}
});
请注意,您应该在之前将其称为 ftOUT.play()
(尽管它无论如何都会起作用...)。
如果你使用JavaFX 8,你可以这样做
ftOUT.setOnFinished(event -> loggedIn());
我一直在网上搜索,找不到我的问题的答案,所以我会尝试自己做,希望最好!
我有一个提供欢迎然后淡出的主舞台,但我想在淡出消失的那一刻切换舞台!有人对此有答案吗?
下面的小代码可以更好地理解:
import java.util.Timer;
import java.util.TimerTask;
import javafx.animation.FadeTransition;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.util.Duration;
public class TextFileOPG extends Application {
private Stage switchStage;
@Override
public void start(Stage primaryStage) {
try {
switchStage = primaryStage;
GridPane root = new GridPane();
root.setAlignment(Pos.CENTER);
Text scenetitle = new Text("Welcome");
scenetitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 40));
root.add(scenetitle, 3, 2);
FadeTransition ftOUT = new FadeTransition(Duration.millis(3000), root);
ftOUT.setFromValue(1.0);
ftOUT.setToValue(0.0);
ftOUT.play();
if(ftOUT.equals(Duration.millis(3000)))
{
loggedIn();
}
primaryStage.setTitle("Welcome");
Scene scene = new Scene(root,350,400);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public void loggedIn()
{
switchStage.setTitle("Try");
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
Text thisIsSoCoolText = new Text("Welcome Again");
thisIsSoCoolText.setFont(Font.font("Tahoma", FontWeight.NORMAL, 40));
grid.add(thisIsSoCoolText, 3, 2);
Scene scene = new Scene(grid, 350, 400);
switchStage.setScene(scene);
switchStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
尝试使用时间轴而不是淡入淡出过渡。时间轴可用于各种动画和 UI 更新。
这是对我有用的代码:
import java.util.Timer;
import java.util.TimerTask;
import javafx.animation.FadeTransition;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.util.Duration;
public class TextFileOPG extends Application {
private Stage switchStage;
final double opacity=1;
@Override
public void start(Stage primaryStage) {
try {
switchStage = primaryStage;
GridPane root = new GridPane();
root.setAlignment(Pos.CENTER);
Text scenetitle = new Text("Welcome");
scenetitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 40));
root.add(scenetitle, 3, 2);
primaryStage.setTitle("Welcome");
Scene scene = new Scene(root,350,400);
primaryStage.setScene(scene);
primaryStage.show();
Timeline tick0 = new Timeline();
tick0.setCycleCount(Timeline.INDEFINITE);
tick0.getKeyFrames().add(
new KeyFrame(new Duration(30), new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
root.setOpacity(root.getOpacity()-0.01);
if(root.getOpacity()<0.01){//30 divided by 0.01 equals 3000 so you take the duration and divide it be the opacity to get your transition time in milliseconds
loggedIn();
tick0.stop();
}
}}));
tick0.play();
} catch(Exception e) {
e.printStackTrace();
}
}
public void loggedIn()
{
switchStage.setTitle("Try");
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
Text thisIsSoCoolText = new Text("Welcome Again");
thisIsSoCoolText.setFont(Font.font("Tahoma", FontWeight.NORMAL, 40));
grid.add(thisIsSoCoolText, 3, 2);
Scene scene = new Scene(grid, 350, 400);
switchStage.setScene(scene);
switchStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
而不是
if(ftOUT.equals(Duration.millis(3000))) {
loggedIn();
}
(永远不会起作用:FadeTransition
实例不可能等于 Duration
实例),使用 onFinished
淡入淡出过渡处理程序:
ftOUT.setOnFinished(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
loggedIn();
}
});
请注意,您应该在之前将其称为 ftOUT.play()
(尽管它无论如何都会起作用...)。
如果你使用JavaFX 8,你可以这样做
ftOUT.setOnFinished(event -> loggedIn());