Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaFX:在一个FXML中单击按钮会影响另一个FXML中的行为_Javafx - Fatal编程技术网

JavaFX:在一个FXML中单击按钮会影响另一个FXML中的行为

JavaFX:在一个FXML中单击按钮会影响另一个FXML中的行为,javafx,Javafx,我是JavaFX的新手,所以我决定制作一个简单的web浏览器,它可以很好地工作,但只能使用预定义的URL(在本例中)。我做了以下工作:创建了BrowserTop.fxml(其中包括一个用于URL的文本字段和一个用于获取此URL的按钮)和Browser.fxml(其中包括普通WebView)。所以,我的问题是,我不明白如何通过单击BrowserTop.fxml中的按钮将字符串URL传递给WebView(它使用Browser.fxml) Browser.java package browser;

我是JavaFX的新手,所以我决定制作一个简单的web浏览器,它可以很好地工作,但只能使用预定义的URL(在本例中)。我做了以下工作:创建了BrowserTop.fxml(其中包括一个用于URL的文本字段和一个用于获取此URL的按钮)和Browser.fxml(其中包括普通WebView)。所以,我的问题是,我不明白如何通过单击BrowserTop.fxml中的按钮将字符串URL传递给WebView(它使用Browser.fxml)

Browser.java

package browser;

import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;


public class Browser extends Application {
    private Stage stage;
    private BorderPane rootLayout;

    @Override
    public void start(Stage stage) throws Exception {
        this.stage = stage;

        initBrowserTop();
        initBrowserMain();
    }


    public void initBrowserTop() throws IOException{
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Browser.class.getResource("BrowserTop.fxml"));
        rootLayout = (BorderPane) loader.load();

        Scene scene = new Scene(rootLayout);
        stage.setScene(scene);
        stage.show();
    }

    public void initBrowserMain() throws IOException{
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Browser.class.getResource("Browser.fxml"));

        WebView browserWebView = (WebView) loader.load();
        WebEngine myWebEngine = browserWebView.getEngine();
        myWebEngine.load("http://google.com");

        rootLayout.setCenter(browserWebView);
    }


    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
}
package browser;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextField;

public class BrowserTopController implements Initializable {

    @FXML TextField txtURL;

    @FXML
    public void handleButtonAction(ActionEvent event){
        //something should be here

    }


    /**
     * Initializes the controller class.
     * @param url
     * @param rb
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

}
BrowserTop.fxml

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

<?import javafx.geometry.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHe

    ight="728.0" prefWidth="1024.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="browser.BrowserTopController">
       <top>
          <AnchorPane>
             <children>
                <TextField fx:id="txtURL" layoutX="52.0" prefHeight="25.0" prefWidth="584.0" AnchorPane.leftAnchor="52.0" AnchorPane.rightAnchor="0.0" BorderPane.alignment="CENTER" />
                <Button fx:id="btnGo" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="25.0" prefWidth="53.0" text="Go" AnchorPane.leftAnchor="0.0" />
             </children>
          </AnchorPane>
       </top>
    </BorderPane>

我以前确实使用过@James_D提到的技巧。我通过以下操作使您的代码正常工作:

package browser;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;


public class Browser extends Application {
    private Stage stage;
    private BorderPane rootLayout;

    @Override
    public void start(Stage stage) throws Exception {
        this.stage = stage;

        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Browser.class.getResource("BrowserTop.fxml"));
        rootLayout = (BorderPane) loader.load();
        BrowserTopController browserTopController = (BrowserTopController) loader.getController(); 

        WebView browserWebView = new WebView();
        WebEngine myWebEngine = browserWebView.getEngine();


        browserTopController.btnGo.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
                    myWebEngine.load(browserTopController.txtURL.getText());
                }
            });

        rootLayout.setCenter(browserWebView);

        Scene scene = new Scene(rootLayout);
        stage.setScene(scene);
        stage.show();
    }



    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
}

到您的fxml文档。

查看helpit中的技术是否像一个符咒一样有效!!谢谢您!!由于缺乏经验,我无法从James_D的链接中获得一些信息,但现在我将用手中的一个工作示例再次研究它
    @FXML Button btnGo;