2013-02-14 35 views
0

我已經從netbeans 7.2中給出的示例項目創建了javafx webview瀏覽器012 我試圖從另一個Jframe表單調用此瀏覽器。 但它給錯誤的調用jframe不是應用程序的子類從另一個swing jframe表單調用javafx webview瀏覽器

以下是Java fx WebviewBrowser的代碼。

import java.util.List; 
import javafx.application.Application; 
import javafx.beans.value.ChangeListener; 
import javafx.beans.value.ObservableValue; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.geometry.HPos; 
import javafx.geometry.Insets; 
import javafx.geometry.VPos; 
import javafx.scene.Node; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.TextField; 
import javafx.scene.layout.ColumnConstraints; 
import javafx.scene.layout.GridPane; 
import javafx.scene.layout.Pane; 
import javafx.scene.layout.Priority; 
import javafx.scene.layout.VBox; 
import javafx.scene.web.WebEngine; 
import javafx.scene.web.WebView; 
import javafx.stage.Stage; 

public class WebViewBrowser extends Application { 

@Override public void start(Stage primaryStage) throws Exception { 
    Pane root = new WebViewPane(); 
    primaryStage.setScene(new Scene(root, 1024, 768)); 
    primaryStage.show(); 
    } 


public static void main(String[] args) { 
    launch(args); 
} 


    public class WebViewPane extends Pane { 

    public WebViewPane() { 
     VBox.setVgrow(this, Priority.ALWAYS); 
     setMaxWidth(Double.MAX_VALUE); 
     setMaxHeight(Double.MAX_VALUE); 

     WebView view = new WebView(); 
     view.setMinSize(500, 400); 
     view.setPrefSize(500, 400); 
     final WebEngine eng = view.getEngine(); 
     eng.load("http://www.lawcrux.com/"); 
     // final TextField locationField = new TextField("http://www.oracle.com/us/index.html"); 
     // locationField.setMaxHeight(Double.MAX_VALUE); 
     Button goButton = new Button("Go"); 
     goButton.setDefaultButton(true); 
     EventHandler<ActionEvent> goAction = new EventHandler<ActionEvent>() { 
      @Override public void handle(ActionEvent event) { 
     //  eng.load(locationField.getText().startsWith("http://") ? locationField.getText() : 
      //    "http://" + locationField.getText()); 
      } 
     }; 
    // goButton.setOnAction(goAction); 
     // locationField.setOnAction(goAction); 
     eng.locationProperty().addListener(new ChangeListener<String>() { 
      @Override public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) { 
     //  locationField.setText(newValue); 
      } 
     }); 
     GridPane grid = new GridPane(); 
     grid.setVgap(5); 
     grid.setHgap(5); 
     // GridPane.setConstraints(locationField, 0, 0, 1, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.SOMETIMES); 
     GridPane.setConstraints(goButton,1,0); 
     GridPane.setConstraints(view, 0, 1, 2, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.ALWAYS); 
     grid.getColumnConstraints().addAll(
       new ColumnConstraints(100, 100, Double.MAX_VALUE, Priority.ALWAYS, HPos.CENTER, true), 
       new ColumnConstraints(40, 40, 40, Priority.NEVER, HPos.CENTER, true) 
     ); 
    // grid.getChildren().addAll(locationField, goButton, view); 
     getChildren().add(grid); 
    } 

    @Override protected void layoutChildren() { 
     List<Node> managed = getManagedChildren(); 
     double width = getWidth(); 
     double height = getHeight(); 
     double top = getInsets().getTop(); 
     double right = getInsets().getRight(); 
     double left = getInsets().getLeft(); 
     double bottom = getInsets().getBottom(); 
     for (int i = 0; i < managed.size(); i++) { 
      Node child = managed.get(i); 
      layoutInArea(child, left, top, 
          width - left - right, height - top - bottom, 
          0, Insets.EMPTY, true, true, HPos.CENTER, VPos.CENTER); 
     } 
    } 
} 

}

在另一個JFrame的按鈕上點擊我打電話這webviewbrowser如下面的代碼。

WebViewBrowser frm= new WebViewBrowser(); 
    WebViewBrowser.launch(); 
    frm.launch(); 
+1

你能在這裏提供一個異常的堆棧跟蹤嗎? – 2013-02-14 21:54:25

回答

0

如果您使用的是JFrame,那麼您必須使用Swing。所以這聽起來像你試圖直接從Swing調用這個JavaFX應用程序。這不起作用。

看看在SimpleSwingBrowser演示,甲骨文發佈:

代碼:http://docs.oracle.com/javafx/2/swing/SimpleSwingBrowser.java.htm

教程:http://docs.oracle.com/javafx/2/swing/swing-fx-interoperability.htm

主要的想法是,你必須來包裝JFXPanel,WebEngine,場景,和在JFrame中的WebView。所以你可以在Swing應用中使用JavaFX,但它必須保持在Swing窗口中。您不能顯示JavaFX應用程序窗口(即沒有Swing JFrame或其他窗口封裝它)。