FXML文件中的getHostServices()。showDocument() [英] getHostServices().showDocument() in a FXML File
本文介绍了FXML文件中的getHostServices()。showDocument()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有简单的方法将getHostServices()。showDocument()命令以某种方式放入toHomepage()方法,而不是执行代码行和代码行,所以代码应该看起来干净简单?
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) {
}
}
如果我按下按钮,它应该直接将我链接到Facebook网址
If I press the button, it should directly link me to the Facebook URL
推荐答案
您需要将 HostServices
传递给控制器
。
密钥代码:在
控制器中设置
。HostServices
HostServices hostServices ;
public void setGetHostController(HostServices hostServices)
{
this.hostServices = hostServices;
}
密码:传递
HostServices
到控制器
。
FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));
Parent root = loader.load();
FXMLDocumentController fXMLDocumentController = loader.getController();
fXMLDocumentController.setGetHostController(getHostServices());
Main
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
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屋!
查看全文