简体   繁体   English

从FXML控制器中的其他类调用方法

[英]Calling methods from other classes in FXML controller

I'm currently doing a homework assignment, creating a web browser with multiple tabs in javaFX using the scene builder and coding in the FXML controller. 我目前正在做作业,使用场景构建器在javaFX中创建带有多个选项卡的Web浏览器,并在FXML控制器中进行编码。 I'm fairly new, but I can't seem to figure out how to access other methods from other classes within the controller class. 我还很新,但是我似乎无法弄清楚如何从控制器类中的其他类访问其他方法。

I have one file name FXMLDocumentController.java where all my coding is in, and another file just called Name.java which just loads the .fxml document that was created by the scene builder. 我有一个文件名FXMLDocumentController.java,其中所有代码都在其中,另一个文件名为Name.java,该文件仅加载由场景构建器创建的.fxml文档。

package name;



public class FXMLDocumentController implements Initializable {

@FXML
private Tab TabPanel1;

@FXML
private Button backButton;

@FXML
private Button forwardButton;

@FXML
private Button goButton;

@FXML
private Label label;

@FXML
private MenuBar menubar1;

@FXML
private Button stopButton;

@FXML
private AnchorPane textArea1;

@FXML
private ComboBox urlAddress;

@FXML
private WebView webview1;
private WebEngine webEngine;

@FXML
void backButtonClickAction(ActionEvent event) {
    WebHistory history = webEngine.getHistory();
    int back = history.getCurrentIndex();
    if (back <= 0) {
    } else {
        history.go(-1);
    }

}

@FXML
void forwardButtonClickAction(ActionEvent event) {
    WebHistory history = webEngine.getHistory();
    int fwd = history.getCurrentIndex();
    fwd++;
    ObservableList<WebHistory.Entry> entryList = history.getEntries();
    int stop = entryList.size();
    if (fwd == stop) {
    } else {
        history.go(1);
    }
}

@FXML
void stopButtonAction(ActionEvent event) {
    webEngine.getLoadWorker().cancel();
}

@FXML
void goButtonAction(ActionEvent event) {
    String url = urlAddress.getValue().toString();
    if (url.startsWith("http://www.")) {
        webEngine.load(url);
    } else if (url.startsWith("www.")) {
        webEngine.load("http://" + url);
    } else {
        webEngine.load("http://www." + url);
    }

    //TabPanel1.setText(webEngine.getLocation());
    //System.out.println(webEngine.getTitle());
}

@FXML
void urlAddressEnter(ActionEvent event) {
    String url = urlAddress.getValue().toString();
    if (url.startsWith("http://www.")) {
        webEngine.load(url);
    } else if (url.startsWith("www.")) {
        webEngine.load("http://" + url);
    } else {
        webEngine.load("http://www." + url);
    }
}

@Override
public void initialize(URL url, ResourceBundle rs) {
    assert TabPanel1 != null : "fx:id=\"TabPanel1\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
    assert backButton != null : "fx:id=\"backButton\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
    assert forwardButton != null : "fx:id=\"forwardButton\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
    assert goButton != null : "fx:id=\"goButton\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
    assert label != null : "fx:id=\"label\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
    assert menubar1 != null : "fx:id=\"menubar1\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
    assert stopButton != null : "fx:id=\"stopButton\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
    assert textArea1 != null : "fx:id=\"textArea1\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
    assert urlAddress != null : "fx:id=\"urlAddress\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
    assert webview1 != null : "fx:id=\"webview1\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
    webEngine = webview1.getEngine();
    getHistory();

}

private void getHistory() {
    final WebHistory history = webEngine.getHistory();
    history.getEntries().addListener(new ListChangeListener<WebHistory.Entry>() {
        @Override
        public void onChanged(ListChangeListener.Change<? extends WebHistory.Entry> c) {
            c.next();
            for (WebHistory.Entry e : c.getRemoved()) {
                urlAddress.getItems().remove(e.getUrl());
            }
            for (WebHistory.Entry e : c.getAddedSubList()) {
                urlAddress.getItems().add(e.getUrl());
            }
        }
    });
}

}

This creates a browser with back, forward, stop and a history drop down list. 这将创建一个带有后退,前进,停止和历史记录下拉列表的浏览器。 I now need to create multiple tabs so I need to create a new .java file that will create new tabs and have a method to return the current webengine, so I can implement all of these features. 现在,我需要创建多个选项卡,因此需要创建一个新的.java文件,该文件将创建新的选项卡,并具有返回当前Webengine的方法,因此我可以实现所有这些功能。

The problem is, when I create a new class I can't access it from the controller, I keep getting errors. 问题是,当我创建一个新类时,无法从控制器访问它,所以我不断收到错误消息。 For instance, if I had a new file named BrowserTab with a method called newBrowserTab, how could I call on that method from the controller file with all my other code. 例如,如果我有一个名为BrowserTab的新文件,并带有一个名为newBrowserTab的方法,那么如何使用我的所有其他代码从控制器文件中调用该方法。 I figured it would be something like this: 我想这将是这样的:

BrowserTab.newBrowserTab(tabPane, "New Tab")

See static methods for a quick solution. 有关快速解决方案,请参见静态方法

Static methods are the most straight-forward, but are not very object-oriented. 静态方法是最简单的方法,但不是非常面向对象的。 There are other methods of passing object instances around to FXML controllers which don't involve static methods and provide greater encapsulation, instancing, polymorphism support, etc. with less global state. 还有其他将对象实例传递给FXML控制器的方法,这些方法不涉及静态方法,而是以较少的全局状态提供了更多的封装,实例化,多态性支持等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM