JavaFX |超链接类

Hyperlink类是JavaFX的一部分。超链接是一种HTML类型的标签,可以包含文本和图形,也可以同时包含文本和图形。它对滚动和点击做出响应。单击/按下超链接时 isVisited() 变成现实。超链接的行为类似于按钮。当按下并释放超链接时 按钮事件 他被派去了。因此,应用程序可以基于此事件执行某些操作。

null

类的构造函数:

  1. 超级链接() :创建没有文本或图形的超链接。
  2. 超链接(字符串t) :创建以指定文本作为标签的超链接。
  3. 超链接(字符串t,节点g) :创建以指定文本和图形作为标签的超链接。

常用方法:

方法 解释
isVisited() 如果尚未访问超链接,则返回true,否则返回false。
SETV(布尔v) 设置所访问属性的值。
setOnAction(EventHandler v) 设置属性onAction的值。
火() 实现来调用ActionEvent(如果定义了ActionEvent)。

下面的程序演示了超级链接类的使用:

  1. Java程序创建超链接并将其添加到后台,还可以添加事件处理程序来处理事件: 此程序创建一个由名称Hyperlink指示的超链接。超链接将在场景中创建,而场景又将在舞台中托管。我们会创建一个标签来显示超链接是否被访问。功能 setTitle() 用于为舞台提供标题。然后 HBox 是创建的,在其上 addChildren() 方法以在场景中附加超链接和标签。最后是 show() 方法以显示最终结果。我们将创建一个事件处理程序来处理按钮事件。事件处理程序将使用 setOnAction() 作用

    // Java Program to create a hyperlink and add
    // it to the stage also add an event handler
    // to handle the events
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    public class hyperlink extends Application {
    // launch the application
    public void start(Stage stage)
    {
    // set title for the stage
    stage.setTitle( "creating hyperlinks" );
    // create a hyperlink
    Hyperlink hyperlink = new Hyperlink( "hyperlink" );
    // create a HBox
    HBox hbox = new HBox();
    // create a label
    Label label = new Label( "hyperlink not visited" );
    // action event
    EventHandler<ActionEvent> event =
    new EventHandler<ActionEvent>() {
    public void handle(ActionEvent e)
    {
    label.setText( "hyperlink visited " );
    }
    };
    // when hyperlink is pressed
    hyperlink.setOnAction(event);
    // add hyperlink
    hbox.getChildren().add(hyperlink);
    hbox.getChildren().add(label);
    // create a scene
    Scene scene = new Scene(hbox, 200 , 200 );
    // set the scene
    stage.setScene(scene);
    stage.show();
    }
    // Main Method
    public static void main(String args[])
    {
    // launch the application
    launch(args);
    }
    }

    
    

    输出:

    图片[1]-JavaFX |超链接类-yiteyi-C++库

  2. Java程序,用于创建包含文本和图像的超链接,并向其中添加事件处理程序: 此程序创建一个由名称指示的超链接 超链接 上面有图片和文字。将使用导入图像的文件输入流包含图像。然后,我们将使用文件输入流的对象创建一个图像,然后使用该图像文件创建一个图像视图。超链接将在场景中创建,而场景又将在舞台中托管。我们会创建一个标签来显示超链接是否被访问。功能 setTitle() 用于为舞台提供标题。然后 HBox 是创建的,在其上 addChildren() 方法以在场景中附加超链接和标签。最后是 show() 方法以显示最终结果。我们将创建一个事件处理程序来处理按钮事件。事件处理程序将使用 setOnAction() 作用

    // Java Program to create a hyperlink with
    // both text and image on it and also add
    // an event handler to it
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import java.io.*;
    import javafx.scene.image.*;
    public class hyperlink_1 extends Application {
    // launch the application
    public void start(Stage stage)
    {
    try {
    // set title for the stage
    stage.setTitle( "creating hyperlinks" );
    // create a input stream
    FileInputStream input = new FileInputStream( "f:\gfg.png" );
    // create a image
    Image image = new Image(input);
    // create a image View
    ImageView imageview = new ImageView(image);
    // create a hyperlink
    Hyperlink hyperlink = new Hyperlink( "GeeksforGeeks" , imageview);
    // create a HBox
    HBox hbox = new HBox();
    // create a label
    Label label = new Label( "hyperlink not visited" );
    // action event
    EventHandler<ActionEvent> event =
    new EventHandler<ActionEvent>() {
    public void handle(ActionEvent e)
    {
    label.setText( "hyperlink visited " );
    }
    };
    // when hyperlink is pressed
    hyperlink.setOnAction(event);
    // add hyperlink
    hbox.getChildren().add(hyperlink);
    hbox.getChildren().add(label);
    // create a scene
    Scene scene = new Scene(hbox, 200 , 200 );
    // set the scene
    stage.setScene(scene);
    stage.show();
    }
    catch (Exception e) {
    System.err.println(e.getMessage());
    }
    }
    // Main Method
    public static void main(String args[])
    {
    // launch the application
    launch(args);
    }
    }

    
    

    输出:

    图片[2]-JavaFX |超链接类-yiteyi-C++库

    注: 上述程序可能无法在联机IDE中运行。请使用脱机编译器。

    参考: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Hyperlink.html

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享