javafx弹出窗口

新建两个类,Main和AlertBox

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application{

    @Override
    public void start(Stage primaryStage) {
        Button button = new Button();
        button.setText("Open a window");
        button.setOnAction(e -> new AlertBox().display("title", "message"));

        AnchorPane layout = new AnchorPane();
        layout.getChildren().add(button);

        Scene scene=new Scene(layout,300,300);

        primaryStage.setScene(scene);
        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }
}
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class AlertBox {

    public void display(String title , String message){
    Stage window = new Stage();
    window.setTitle("title");
    //modality要使用Modality.APPLICATION_MODEL
    window.initModality(Modality.APPLICATION_MODAL);
    window.setMinWidth(300);
    window.setMinHeight(150);

    Button button = new Button("Close the window");
    button.setOnAction(e -> window.close());

    Label label = new Label(message);

    VBox layout = new VBox(10);
    layout.getChildren().addAll(label , button);
    layout.setAlignment(Pos.CENTER);

    Scene scene = new Scene(layout);
    window.setScene(scene);
    //使用showAndWait()先处理这个窗口,而如果不处理,main中的那个窗口不能响应
    window.showAndWait();
    }
}

运行一下就是这样哒

  • 4
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,您可以按照以下步骤进行实现: 1. 在 JavaFX窗口中,注册一个按键事件处理器,例如: ```java scene.setOnKeyPressed(event -> { if (event.getCode() == KeyCode.ENTER) { // 弹出窗口 } }); ``` 2. 创建子窗口的 Stage 对象,例如: ```java Stage childStage = new Stage(); ``` 3. 在子窗口中添加需要的控件和布局,例如: ```java VBox vbox = new VBox(); vbox.getChildren().add(new Label("这是子窗口")); Scene childScene = new Scene(vbox, 200, 100); childStage.setScene(childScene); ``` 4. 在主窗口中按下按键时,显示子窗口: ```java childStage.show(); ``` 完整代码示例: ```java import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.input.KeyCode; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception { VBox root = new VBox(); Scene scene = new Scene(root, 300, 250); primaryStage.setScene(scene); scene.setOnKeyPressed(event -> { if (event.getCode() == KeyCode.ENTER) { Stage childStage = new Stage(); VBox vbox = new VBox(); vbox.getChildren().add(new Label("这是子窗口")); Scene childScene = new Scene(vbox, 200, 100); childStage.setScene(childScene); childStage.show(); } }); primaryStage.show(); } public static void main(String[] args) { launch(args); } } ``` 注意:这只是一个简单的示例,实际应用中可能需要更复杂的逻辑和布局。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值