FXML 文件中的 getHostServices().showDocument() [英] getHostServices().showDocument() in a FXML File
问题描述
有没有什么简单的方法可以以某种方式将 getHostServices().showDocument() 命令放入 toHomepage() 方法中,而不是执行几行和几行代码,因此代码应该看起来干净简单?
包示例;导入 javafx.application.HostServices;导入 javafx.event.ActionEvent;导入 javafx.fxml.FXML;导入 javafx.scene.control.Button;公共类控制器{@FXML私人按钮 facebookButton;@FXMLvoid toHomepage(ActionEvent 事件) {}}
如果我按下按钮,它应该直接将我链接到 Facebook 网址
您需要将 HostServices
传递给 Controller
.
Key Code:在Controller
中设置HostServices
.
HostServices 主机服务;public void setGetHostController(HostServices hostServices){this.hostServices = 主机服务;}
<块引用>
关键代码:将HostServices
传递给Controller
.
FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));父根 = loader.load();FXMLDocumentController fXMLDocumentController = loader.getController();fXMLDocumentController.setGetHostController(getHostServices());
<块引用>
主要
import javafx.application.Application;导入 javafx.fxml.FXMLLoader;导入 javafx.scene.Parent;导入 javafx.scene.Scene;导入 javafx.stage.Stage;/**** @author sedrick*/公共类 JavaFXApplication7 扩展应用程序 {@覆盖公共无效开始(阶段阶段)抛出异常{FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));父根 = loader.load();FXMLDocumentController fXMLDocumentController = loader.getController();fXMLDocumentController.setGetHostController(getHostServices());场景场景 = 新场景(根);stage.setScene(场景);舞台表演();}/*** @param args 命令行参数*/公共静态无效主(字符串 [] args){发射(参数);}}
<块引用>
控制器
import java.net.URL;导入 java.util.ResourceBundle;导入 javafx.application.HostServices;导入 javafx.event.ActionEvent;导入 javafx.fxml.FXML;导入 javafx.fxml.Initializable;导入 javafx.scene.control.Label;/**** @作者塞德里*/公共类 FXMLDocumentController 实现 Initializable {HostServices 主机服务;@FXML自有品牌标签;@FXML私有无效 handleButtonAction(ActionEvent 事件){hostServices.showDocument("www.google.com");}@覆盖公共无效初始化(URL url,ResourceBundle rb){//去做}public void setGetHostController(HostServices hostServices){this.hostServices = 主机服务;}}
<块引用>
FXML
<儿童><Button layoutX="126" layoutY="90" text="点击我!"onAction="#handleButtonAction" fx:id="button"/><Label layoutX="126" layoutY="120" minHeight="16" minWidth="69" fx:id="label"/></儿童></AnchorPane>
Is there any easy way to put into the toHomepage() method the getHostServices().showDocument() command somehow, instead of doing lines and lines of code, so the code should look clean and simple?
package sample;
import javafx.application.HostServices;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
public class Controller {
@FXML
private Button facebookButton;
@FXML
void toHomepage(ActionEvent event) {
}
}
If I press the button, it should directly link me to the Facebook URL
You need to pass the HostServices
to the Controller
.
Key Code: Set the
HostServices
in theController
.
HostServices hostServices ;
public void setGetHostController(HostServices hostServices)
{
this.hostServices = hostServices;
}
Key Code: Passing
HostServices
to theController
.
FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));
Parent root = loader.load();
FXMLDocumentController fXMLDocumentController = loader.getController();
fXMLDocumentController.setGetHostController(getHostServices());
Main
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author sedrick
*/
public class JavaFXApplication7 extends Application {
@Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));
Parent root = loader.load();
FXMLDocumentController fXMLDocumentController = loader.getController();
fXMLDocumentController.setGetHostController(getHostServices());
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Controller
import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.HostServices;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
/**
*
* @author sedri
*/
public class FXMLDocumentController implements Initializable {
HostServices hostServices;
@FXML
private Label label;
@FXML
private void handleButtonAction(ActionEvent event) {
hostServices.showDocument("www.google.com");
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
public void setGetHostController(HostServices hostServices)
{
this.hostServices = hostServices;
}
}
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication7.FXMLDocumentController">
<children>
<Button layoutX="126" layoutY="90" text="Click Me!" onAction="#handleButtonAction" fx:id="button" />
<Label layoutX="126" layoutY="120" minHeight="16" minWidth="69" fx:id="label" />
</children>
</AnchorPane>
这篇关于FXML 文件中的 getHostServices().showDocument()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!