FXML文件中的getHostServices()。showDocument()

时间:2018-06-09 13:43:55

标签: java javafx fxml scenebuilder

有没有简单的方法将getHostServices()。showDocument()命令以某种方式放入toHomepage()方法,而不是执行代码行和代码行,所以代码应该看起来干净简单?

g  <- erdos.renyi.game(100, .015, directed = TRUE)
# if you have directed graph, decide if you want
# strongly or weakly connected components
co <- components(g, mode = 'STRONG')
gi <- induced.subgraph(g, which(co$membership == which.max(co$csize)))
# if you want here you can decide if you want values only
# in the upper or lower triangle or both
ad <- get.adjacency(gi)

如果我按下按钮,则应直接将我链接到Facebook网址

1 个答案:

答案 0 :(得分:0)

您需要将HostServices传递给Controller

  

密钥代码:在HostServices中设置Controller

HostServices hostServices ;

public void setGetHostController(HostServices hostServices)
{
    this.hostServices = hostServices;
}        
  

关键代码:将HostServices传递给Controller

FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));
Parent root = loader.load();
FXMLDocumentController fXMLDocumentController = loader.getController();
fXMLDocumentController.setGetHostController(getHostServices());
  

主要

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);
    }

}
  

控制器

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>