我正在使用JavaFX来创建应用程序。 SplashScreen正常打开,即使在应用程序打开后也会在屏幕上显示。
以下是我的代码:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class SplashScreen extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root=FXMLLoader.load(getClass().getResource("SplashScreenFXML.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
package splashscreen;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class SplashScreenFXMLController implements Initializable
{
@FXML
private AnchorPane rootPane;
@Override
public void initialize(URL url, ResourceBundle rb)
{
new SplashScreen().start();
}
class SplashScreen extends Thread
{
@Override
public void run()
{
try
{
Thread.sleep(5000);
Platform.runLater(new Runnable(){
@Override
public void run()
{
try {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
Stage stage=new Stage();
stage.setScene(scene);
stage.show();
rootPane.getScene().getWindow().hide();
} catch (IOException ex) {
Logger.getLogger(SplashScreenFXMLController.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
} catch (InterruptedException ex) {
Logger.getLogger(SplashScreenFXMLController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
任何人都可以帮我这个吗?
我认为rootPane.getScene().getWindow().hide();
这不起作用。还有其他方法我可以隐藏SplashScreen
舞台吗?