問題描述
- 今天使用javafx 構(gòu)建 程序時, 在界面設(shè)置了一張圖片資源,但是雙擊程序,不顯示主界面,在cmd下發(fā)現(xiàn)提示 無Preloader.class ,我在網(wǎng)絡(luò)翻遍了狠毒資料還是沒有發(fā)現(xiàn),很多資料都說不需要寫這個類類繼承,的確也是這樣,不利用資源的情況下是不需要設(shè)置這個類的 ,但是建議還是加上,
package GameApplication;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("TractorGame.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add(
getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
initScene(root);
primaryStage.setTitle("拖拉機(jī)小游戲");
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
/*
* 初始化主要場景
*/
private void initScene(Parent root) {
ImageView imageNode = (ImageView) root.lookup("#myimage");
imageNode.setImage(new Image("resouces/標(biāo)題畫面.jpg"));
imageNode.setVisible(true);
}
public static void main(String[] args) {
launch(args);
}
}
- 加上Preloader
package GameApplication;
import javafx.application.Preloader;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
public class prepare extends Preloader{
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("TractorGame.fxml"));
ImageView imageNode = (ImageView) root.lookup("#myimage");
imageNode.setImage(new Image("resouces/標(biāo)題畫面.jpg"));
imageNode.setVisible(true);
}
}
- 這樣寫的目地是由于類加載器架構(gòu)的寫法,作者寫架構(gòu)實(shí)際采用;