日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

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

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

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

發(fā)布時(shí)間:2025/1/21 java 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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ò),歡迎將生活随笔推薦給好友。