2017-08-22 4 views
0

私は大学向けのJavaプログラムをやっています。 私は、メニュー項目のボタンをクリックすると、新しい段階を開こうとしています。 この段階では、htmlファイルの内容を表示する必要があります。JavaFxアプリケーションでWebEngineのhtmlファイルをロード

ステージを開くには問題はありませんが、問題はステージが空であることです(ステージを開いている間にエラーは発生しません)。 Javaプログラムのメインコントローラで

html stage (picture)

これは、HTMLステージを開くためのコードです:

@FXML 
public void showBrowser(ActionEvent event) throws IOException { 
    Stage primaryStage = new Stage(); 
    Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("othello/view/browser.fxml")); 
    Scene scene = new Scene(root); 

    primaryStage.setScene(scene); 
    primaryStage.setResizable(false); 
    primaryStage.sizeToScene(); 
    primaryStage.setTitle("Team Background"); 
    primaryStage.show(); 
} } 

これは私が表示したいHTML段階のFXMLファイルです(browser.fxml):これは、FXMLファイルのコントローラのコードである

<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.scene.layout.AnchorPane?> 
<?import javafx.scene.web.WebView?> 

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="othello.controller.WebViewController"> 
    <children> 
     <WebView fx:id="webView" layoutX="100.0" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" /> 
    </children> 
</AnchorPane> 

(WebViewController):

package othello.controller; 

import javafx.fxml.FXML; 
import javafx.scene.web.WebEngine; 
import javafx.scene.web.WebView; 

public class WebViewController { 
    @FXML 
    public WebView webView; 
    public WebEngine webEngine; 

    private void initialize() { 

webEngine = webView.getEngine(); 
webEngine.load(getClass().getResource("/Othello/src/othello/html/TeamBackground/history.html").toExternalForm()); 



    } 
} 

私はURLとして、または結果として.hmtlファイルのパスを使用してファイルとして読み込みを試みました。 私を助けることができますか?

よろしくお願いいたします。

+1

HTMLファイルへのパスをほぼ確実に間違っている: 'src'フォルダには、実行時にアクセスできるようにすることが非常にほとんどありません。 –

+0

これは正しいパスでしょうか? ありがとうございます。 – giasco

+1

あなたのプロジェクトレイアウトは表示されていません。おそらく 'getClass()。getResource("/othello/html/... ")'は、他のすべてがそれがそうであるように設定されていれば動作します。 –

答えて

0

あなたのコードのように実装しようとしましたが、あなたの間違いを見つけました。srcフォルダから開始したhtmlファイルの間違ったパスを使用しましたが、getClass()。getResource()を使用すると現在リソースフォルダにありますsrcそうに自分のパス訂正:

webEngine.load(getClass().getResource("/othello/html/TeamBackground/history.html").toExternalForm()); 
+1

ok私は間違いを理解して、ありがとう! – giasco

関連する問題