日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

JavaFX弹出窗口和消息对话框代码示例

發(fā)布時(shí)間:2025/1/21 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JavaFX弹出窗口和消息对话框代码示例 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

彈出窗口

彈窗類

package cn.zxl.AlertWindow;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;/*** @Description: //TODO 彈出窗口、消息對(duì)話框* @Author: zhangxueliang* @Create: 2021-05-27 09:50* @Version: 1.0**/ public class AlertWindow {private static boolean res;public static boolean display(String title,String msg){Stage stage = new Stage();stage.initModality(Modality.APPLICATION_MODAL);Label label = new Label();label.setText(msg);Button btn1 = new Button("是");Button btn2 = new Button("否");btn1.setOnMouseClicked(event -> {res=true;System.out.println("你點(diǎn)擊了是");stage.close();});btn2.setOnMouseClicked(event -> {res=false;System.out.println("你點(diǎn)擊了否");stage.close();});VBox vBox = new VBox();vBox.getChildren().addAll(label,btn1,btn2);//設(shè)置居中vBox.setAlignment(Pos.CENTER);Scene scene = new Scene(vBox,200,200);stage.setScene(scene);stage.setTitle(title);stage.showAndWait();return res;} }

彈窗啟動(dòng)類

package cn.zxl.AlertWindow;import javafx.application.Application; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.VBox; import javafx.stage.Stage;/*** @Description: //TODO 主類* @Author: zhangxueliang* @Create: 2021-05-27 09:50* @Version: 1.0**/ public class Main extends Application {@Overridepublic void start(Stage primaryStage) throws Exception {Button btn = new Button("彈出窗口");btn.setOnMouseClicked(event -> {System.out.println(AlertWindow.display("新窗口", "是否關(guān)閉窗口"));});VBox vBox = new VBox();vBox.getChildren().add(btn);//設(shè)置居中顯示vBox.setAlignment(Pos.CENTER);Scene scene = new Scene(vBox, 400, 400);primaryStage.setTitle("彈出窗口示例");primaryStage.setScene(scene);primaryStage.show();}public static void main(String[] args) {launch(args);} }

最終效果

單擊彈出窗口按鈕會(huì)彈出新窗口。

消息對(duì)話框

彈窗類

同上。

消息對(duì)話框啟動(dòng)類

package cn.zxl.AlertWindow;import javafx.application.Application; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.VBox; import javafx.stage.Stage;/*** @Description: //TODO 消息提示框* @Author: zhangxueliang* @Create: 2021-05-27 09:50* @Version: 1.0**/ public class Main2 extends Application {Stage stage;@Overridepublic void start(Stage primaryStage) throws Exception {stage = primaryStage;//單擊系統(tǒng)自帶的關(guān)閉按鈕時(shí)也要彈出詢問(wèn)窗口stage.setOnCloseRequest(event -> {//點(diǎn)擊了否,也會(huì)關(guān)閉窗口,所以要取消默認(rèn)事件,單擊否按鈕不會(huì)關(guān)閉窗口event.consume();closeWindow();});Button btn = new Button("關(guān)閉窗口");btn.setOnMouseClicked(event -> closeWindow());VBox vBox = new VBox();vBox.getChildren().add(btn);//設(shè)置居中顯示vBox.setAlignment(Pos.CENTER);Scene scene = new Scene(vBox, 400, 400);stage.setTitle("彈出窗口示例");stage.setScene(scene);stage.show();}/*** //TODO 如果點(diǎn)擊了是按鈕,就關(guān)閉所有窗口* @Description: * @Create: 2021/5/27 10:59* @Author: zhangxueliang* @Param:* @Return: */private void closeWindow() {boolean b = AlertWindow.display("新窗口", "是否關(guān)閉窗口");if (b) {stage.close();}}public static void main(String[] args) {launch(args);} }

最終效果

單擊是關(guān)閉窗口,單擊否不關(guān)閉。
并且單擊X按鈕效果一樣。

總結(jié)

以上是生活随笔為你收集整理的JavaFX弹出窗口和消息对话框代码示例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。