2015-09-05 84 views
-1

我開始研究基於Java的瀏覽器。我有它,所以它創建一個標籤等。但現在,我需要它,以便您可以創建新的JBrowser,並創建一個新窗口。當你右鍵點擊一個鏈接時,它可以讓你打開一個新窗口。這是我的「窗口」類。我應該如何改變它以使其成爲無限的。Javafx動態窗口數量

public class JBrowser { 

    private Scene scene; 
    private AnchorPane pane; 
    private TabPane tabPane; 
    private Button addButton; 

    public JBrowser(Stage stage) { 
     tabPane = new TabPane(); 
     pane = new AnchorPane(); 
     addButton = new Button("+"); 

     tabPane.getTabs().add(JTab.getInstance().addTab()); 


     addButton.setOnAction(addTab); 

     AnchorPane.setTopAnchor(tabPane, 5.0); 
     AnchorPane.setLeftAnchor(tabPane, 5.0); 
     AnchorPane.setRightAnchor(tabPane, 5.0); 
     AnchorPane.setTopAnchor(addButton, 10.0); 
     AnchorPane.setLeftAnchor(addButton, 10.0); 

     pane.getChildren().addAll(tabPane, addButton); 

     scene = new Scene(pane, 1200, 800); 
     scene.getStylesheets().add("tabs.css"); 
     stage.setScene(scene); 
    } 

    private EventHandler<ActionEvent> addTab = new EventHandler<ActionEvent>() { 
     @Override 
     public void handle(ActionEvent event) { 
      tabPane.getTabs().add(JTab.getInstance().addTab()); 
     } 
    }; 
} 

public class JTab {//Tab Class might need this. 

    private javafx.scene.control.Tab tab; 
    private BorderPane root; 
    private Button reloadButton; 
    private TextField field; 
    private WebView view; 
    private WebEngine engine; 


    public static JTab getInstance() { 
     return new JTab(); 
    } 

    public javafx.scene.control.Tab addTab() { 
     tab = new Tab(); 
     tab.setText("New Tab"); 
     //setGraphic 

     HBox hBox = new HBox(5); 
     hBox.setAlignment(Pos.CENTER); 

     //Our buttons for navigation. 
     reloadButton = new Button("Reload"); 

     //Add listeners to the buttons. 
     reloadButton.setOnAction(reload); 

     //The TextField for entering web addresses. 
     field = new TextField("Enter URL"); 
     field.setPrefColumnCount(50); //make the field at least 50 columns wide. 
     field.focusedProperty().addListener((ov, t, t1) -> { //When click on field entire thing selected 
      Platform.runLater(() -> { 
       if (field.isFocused() && !field.getText().isEmpty()) { 
        field.selectAll(); 
       } 
      }); 
     }); 
     field.setOnKeyPressed(event -> { //When ENTER is pressed it will load page 
      if (event.getCode() == KeyCode.ENTER) { 
       if (!field.getText().isEmpty()) { 
        loadData(field.getText()); 
       } 
      } 
     }); 

     //Add all out navigation nodes to the vbox. 
     hBox.getChildren().addAll(reloadButton, field); 

     view = new WebView(); 

     engine = view.getEngine(); 
     engine.setJavaScriptEnabled(true); 
     loadData("google.com"); 

     root = new BorderPane(); 
     root.setPrefSize(1024, 768); 
     root.setTop(hBox); 
     root.setCenter(view); 


     tab.setContent(root); 
     return tab; 
    } 


    public void loadData(String URL) { 
     if(!URL.startsWith("http://")) { 
      URL = "http://" + URL; 
      field.setText(URL); 
     } 
     engine.load(URL); 
     // tab.setText(getTitle()); 
    } 

    private String getTitle() { 
     Document doc = engine.getDocument(); 
     NodeList heads = doc.getElementsByTagName("head"); 
     String titleText = engine.getLocation() ; // use location if page does not define a title 
     if (heads.getLength() > 0) { 
      Element head = (Element)heads.item(0); 
      NodeList titles = head.getElementsByTagName("title"); 
      if (titles.getLength() > 0) { 
       Node title = titles.item(0); 
       titleText = title.getTextContent(); 
      } 
     } 
     return titleText; 
    } 

    private EventHandler<ActionEvent> reload = new EventHandler<ActionEvent>() { 
     @Override 
     public void handle(ActionEvent event) { 
      engine.reload(); 

     } 
    }; 
} 

    public class Main extends Application { //main class 

    @Override 
    public void start(Stage stage) throws Exception { 
     stage.setTitle("JBrowser"); 
     new JBrowser(stage); 
     //new Window(stage); 
     stage.show(); 

    } 


    public static void main(String[] args) throws IOException { 
     Application.launch(args); 
    } 
} 

回答

0
public JBrowser() { 
    Stage stage = new Stage(); 
    //... 
} 

創建一個新的階段,它可以成爲一個窗口!