JavaFX |弹出类

Popup类是JavaFX的一部分。Popup类创建一个没有内容、填充为空且透明的弹出窗口。Popup类用于显示通知、按钮或下拉菜单等。弹出窗口没有装饰。它本质上是一个专门的场景/窗口,没有任何装饰。

null

类的构造函数:

  • 弹出窗口() :创建Popup类的对象。

常用方法:

方法 解释
getContent() 要在此弹出窗口上呈现的节点的可观察列表。
设置自动隐藏(布尔值v) 将“自动隐藏”的值设置为指定的布尔值。
isShowing() 返回弹出窗口是否可见。

以下程序说明了Popup类的使用:

  1. 创建弹出窗口并将其添加到舞台的Java程序: 在这个程序中,我们创建一个名为 弹出窗口 .弹出窗口包含一个名为 标签 .我们还创建了一个名为 按钮 并向其添加事件处理程序,然后显示隐藏的弹出窗口,如果已经可见则隐藏。按钮被添加到TilePane,TilePane被添加到场景,场景被添加到舞台。调用show函数来显示结果。标签的背景色是使用 setStyle() 函数,并使用 setMinHeight() , setMinWidth() 作用这个 隐藏 show() 函数用于隐藏或显示弹出窗口。

    // Java program to create a popup and
    // add it to the stage
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.*;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.control.Label;
    import javafx.stage.Stage;
    import javafx.stage.Popup;
    public class Popup_1 extends Application {
    // launch the application
    public void start(Stage stage)
    {
    // set title for the stage
    stage.setTitle( "Creating popup" );
    // create a button
    Button button = new Button( "button" );
    // create a tile pane
    TilePane tilepane = new TilePane();
    // create a label
    Label label = new Label( "This is a Popup" );
    // create a popup
    Popup popup = new Popup();
    // set background
    label.setStyle( " -fx-background-color: white;" );
    // add the label
    popup.getContent().add(label);
    // set size of label
    label.setMinWidth( 80 );
    label.setMinHeight( 50 );
    // action event
    EventHandler<ActionEvent> event =
    new EventHandler<ActionEvent>() {
    public void handle(ActionEvent e)
    {
    if (!popup.isShowing())
    popup.show(stage);
    else
    popup.hide();
    }
    };
    // when button is pressed
    button.setOnAction(event);
    // add button
    tilepane.getChildren().add(button);
    // create a scene
    Scene scene = new Scene(tilepane, 200 , 200 );
    // set the scene
    stage.setScene(scene);
    stage.show();
    }
    // Main Method
    public static void main(String args[])
    {
    // launch the application
    launch(args);
    }
    }

    
    

    输出:

  2. Java程序创建一个弹出窗口,并将其添加到舞台上,并使用 setAutoHide() 功能: 在这个程序中,我们创建一个名为 弹出窗口 .弹出窗口包含一个名为 标签 .我们还创建了一个名为 按钮 并向其添加事件处理程序,以显示隐藏的弹出窗口。按钮被添加到TilePane,TilePane被添加到场景,场景被添加到舞台。调用show函数来显示结果。当它失去焦点时,弹出窗口将自动隐藏,我们将使用 setAutoHide() 作用标签的背景色是使用 setStyle() 函数,并使用 setMinHeight() , setMinWidth() 作用隐藏()和 show() 函数用于隐藏或显示弹出窗口。

    // Java Program to create a popup and add
    // it to the stage and make the popup hide
    // automatically when it loses focus using
    // the setAutoHide() function
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.*;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.control.Label;
    import javafx.stage.Stage;
    import javafx.stage.Popup;
    public class popup_2 extends Application {
    // launch the application
    public void start(Stage stage)
    {
    // set title for the stage
    stage.setTitle( "Creating Popup" );
    // create a button
    Button button = new Button( "popup" );
    // create a tile pane
    TilePane tilepane = new TilePane();
    // create a label
    Label label = new Label( "This is a Popup" );
    // create a popup
    Popup popup = new Popup();
    // set background
    label.setStyle( " -fx-background-color: white;" );
    // add the label
    popup.getContent().add(label);
    // set size of label
    label.setMinWidth( 80 );
    label.setMinHeight( 50 );
    // set auto hide
    popup.setAutoHide( true );
    // action event
    EventHandler<ActionEvent> event =
    new EventHandler<ActionEvent>() {
    public void handle(ActionEvent e)
    {
    if (!popup.isShowing())
    popup.show(stage);
    }
    };
    // when button is pressed
    button.setOnAction(event);
    // add button
    tilepane.getChildren().add(button);
    // create a scene
    Scene scene = new Scene(tilepane, 200 , 200 );
    // set the scene
    stage.setScene(scene);
    stage.show();
    }
    // Main Method
    public static void main(String args[])
    {
    // launch the application
    launch(args);
    }
    }

    
    

    输出:

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

参考: https://docs.oracle.com/javase/8/javafx/api/javafx/stage/Popup.html

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