Java:尚未加载屏幕

时间:2016-05-31 11:32:44

标签: java javafx javafx-2

我创建了一个差不多完成的JavaFX应用程序。我将它导出为可运行的JAR。打开这个JAR时,我只看到一个空白窗口。我跟着stackoverflow的其他一些答案,但我没有得到它的工作。 仅适用于Eclipse IDE!

我的屏幕控制器:

package gui;

import java.io.File;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;

public class ScreensController extends StackPane {

	private HashMap<String, Node> screens = new HashMap<>();
	public static String sourcePath = "";
	private CoreService coreService;
	
	public ScreensController(){
		super();
	}
	
	
	public void addScreen(String name, Node screen) { 
	       screens.put(name, screen); 
	   } 
	
	public boolean loadScreen(String name, String resource) { 
		System.out.println("ID: "+name);
		System.out.println("Resource: "+resource);

		String file = System.getProperty("user.dir")+"\\bin\\"+resource;
//		System.out.println(file);
		
	    try { 
	       FXMLLoader myLoader = new FXMLLoader();
	       File f = new File(file);
	       URL url = f.toURI().toURL();
	       myLoader.setLocation(url);
//	       System.out.println("Location: "+myLoader.getLocation());
	       
	       Parent loadScreen = (Parent) myLoader.load(); 
	       ControlledScreen myScreenControler = 
	              ((ControlledScreen) myLoader.getController()); 
	       myScreenControler.setScreenParent(this); 
	       addScreen(name, loadScreen); 
	       System.out.println("Anzahl Screens: "+screens.size());
	       return true; 
	     }catch(Exception e) { 
	    	 System.out.println("Fehler beim Laden von "+file);
	    	 System.out.println(e.getMessage()); 
	    	 return false; 
	     }
	     
	} 

	
	public boolean setScreen(final String name) { 
		@SuppressWarnings("unused")
		Node screenToRemove;
        if(screens.get(name) != null){   //screen loaded
        	 if(!getChildren().isEmpty()){    //if there is more than one screen
        		 	getChildren().add(0, screens.get(name));     //add the screen
        		 	screenToRemove = getChildren().get(1);
        		 	getChildren().remove(1);                    //remove the displayed screen
        	 }else{
        		 getChildren().add(screens.get(name));       //no one else been displayed, then just show
        	}
        	return true;
         }else {
        	 System.out.println("Screen hasn't been loaded!!! \n");
        	 return false;
         }     
	}
	
	public boolean unloadScreen(String name) { 
		if(screens.remove(name) == null) { 
			System.out.println("Screen didn't exist!!!"); 
	        return false; 
	      } else { 
	           return true; 
	       } 
	}

	public void print() {
		Set<String> keys = screens.keySet();
		Iterator<String> it = keys.iterator();
		while (it.hasNext()){
			System.out.println("Key: "+it.next());
		} 
		
	} 
	
	public CoreService getCoreService(){
		return this.coreService;
	}
	
	public void setCoreService(CoreService coreService){
		this.coreService = coreService;
	}
	
}

我在这里使用它:

package gui;


import java.util.Optional;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

public class MainMenu extends Application {

    private Stage mainStage;
    private static CoreService coreService;
	
	public static final String MAIN_SCREEN = "main";
	public static final String MAIN_SCREEN_FXML = "gui\\MainMenu.fxml";	
	
	@Override
	public void start(Stage primaryStage) {
		
		this.mainStage = primaryStage;
	
		ScreensController mainContainer = new ScreensController();
		
		mainContainer.loadScreen(MainMenu.MAIN_SCREEN, MainMenu.MAIN_SCREEN_FXML);	
		
		mainContainer.setCoreService(MainMenu.coreService);
		
		mainContainer.setScreen(MainMenu.MAIN_SCREEN);
		Group root = new Group();
		root.getChildren().addAll(mainContainer);
		Scene scene = new Scene(root);
		primaryStage.setScene(scene);
		primaryStage.setOnCloseRequest(confirmCloseEventHandler);
		primaryStage.show();
		
	}
	
    private EventHandler<WindowEvent> confirmCloseEventHandler = event -> {
    	//Source: http://stackoverflow.com/questions/29710492/javafx-internal-close-request
        Alert closeConfirmation = new Alert(
                Alert.AlertType.CONFIRMATION,
                "Are you sure you want to exit?"
        );
        Button exitButton = (Button) closeConfirmation.getDialogPane().lookupButton(
                ButtonType.OK
        );
        exitButton.setText("Exit");
        closeConfirmation.setHeaderText("Confirm Exit");
        closeConfirmation.initModality(Modality.APPLICATION_MODAL);
        closeConfirmation.initOwner(mainStage);

        closeConfirmation.setX(mainStage.getX() + 150);
        closeConfirmation.setY(mainStage.getY() - 300 + mainStage.getHeight());

        Optional<ButtonType> closeResponse = closeConfirmation.showAndWait();
        if (!ButtonType.OK.equals(closeResponse.get())) {
            event.consume();
        }
    };
	
	public static void main(String[] args, CoreService aService) {	
		// Weitergeben des CoreServices
		coreService = aService;
		launch(args);
	}

}

我没看到错误在哪里。 当我从命令行启动程序时,它说无法找到MainMenu.fxml文件。 在我的应用程序中,它在包gui中。 - &GT; GUI / MainMenu.fxml

如果有人发现我的错误会很好!

1 个答案:

答案 0 :(得分:0)

错误消息告诉您,无法找到FXML文件。

您可以尝试:

改变这个......

<div class="article-additional-info">
  <div>
    <strong>Author(s): </strong>Andrzej Antoszewski
    <br />
    <strong>Subject(s): </strong>Politics / Political Sciences
    <br />
    <strong>Published by: </strong>Łódzkie Towarzystwo Naukowe
    <br/>
    <strong>Keywords: </strong>East-Central Europe; early election; parliament; normative model
    <br/>
  </div>
</div>

...到......

public static final String MAIN_SCREEN_FXML = "gui\\MainMenu.fxml"; 

要改变这个......

public static final String MAIN_SCREEN_FXML = "/gui/MainMenu.fxml"; 

...到(并且您不需要变量FXMLLoader myLoader = new FXMLLoader(); File f = new File(file); URL url = f.toURI().toURL(); myLoader.setLocation(url); file)...

f

一些参考文献: