JavaFX通过Controller类实现第二窗口销毁和程序退出

本文介绍如何使用JavaFX实现窗口的创建与销毁,包括通过不同按钮触发关闭当前窗口或整个应用程序的功能,并提供具体的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Preface

Q:为什么有此文?
A:

  • 不能高度自定义化
  • 网上大部分文章是通过简易的warning窗口 或者 information窗口实现的,且过于繁琐
  • 大部分放在了Main.java,不好弄

原理

Controller类中关键性代码:(具体代码见

//销毁当前窗口的代码
//exitButton是第二窗口的退出按钮
public void exitButtonOnMouseClicked() {
    //通过stage方式操作窗口,因为一个新的窗口就是一个新的stage
    Stage stage = (Stage)exitButton.getScene().getWindow();
    stage.close();
}
//平台退出的代码
//platformExitButton是第二窗口的退出按钮
public void platformExitButtonOnMouseClicked() {
    //其实,此处也可像上面操作stage一样
    Platform.exit();
}

实现效果

窗口销毁效果

窗口销毁

应用程序退出效果

应用程序退出效果

Main.java

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 498, 320));
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

Controller.java

package sample;

import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class Controller {
    @FXML
    private Button okButton;//未用到

    @FXML
    private  Button platformExitButton;

    @FXML
    private Button exitButton;

    public void okButtonOnMouseClicked() {
        //此处是为了加载新的窗口,具体请见:http://blog.youkuaiyun.com/qq_20336817/article/details/79079296
        System.out.println("fuck me!");

        try {
            AnchorPane page = FXMLLoader.load(getClass().getResource("dialog.fxml"));
            Scene newScene = new Scene(page);
            Stage stage = new Stage();
            stage.setTitle("dialog window");
            stage.setScene(newScene);
            stage.show();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    //销毁当前窗口的代码
    //exitButton是第二窗口的退出按钮
    public void exitButtonOnMouseClicked() {
        Stage stage = (Stage)exitButton.getScene().getWindow();
        stage.close();
    }
    //平台退出的代码
    //platformExitButton是第二窗口的退出按钮
    public void platformExitButtonOnMouseClicked() {
        Platform.exit();
    }
}

sample.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>

<GridPane alignment="center" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/9" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <columnConstraints>
      <ColumnConstraints />
   </columnConstraints>
   <rowConstraints>
      <RowConstraints />
   </rowConstraints>
   <children>
      <AnchorPane prefHeight="320.0" prefWidth="498.0">
         <children>
            <Button fx:id="okButton" layoutX="232.0" layoutY="203.0" mnemonicParsing="false" onMouseClicked="#okButtonOnMouseClicked" text="Emmm">
               <font>
                  <Font size="17.0" />
               </font>
            </Button>
            <Label layoutX="150.0" layoutY="100.0" text="Fuck Me">
               <font>
                  <Font size="21.0" />
               </font>
            </Label>
            <Button fx:id="platformExitButton" layoutX="210.0" layoutY="259.0" mnemonicParsing="false" onMouseClicked="#platformExitButtonOnMouseClicked" text="Platform Exit">
               <font>
                  <Font size="17.0" />
               </font>
            </Button>
         </children>
      </AnchorPane>
   </children>
</GridPane>

dialog.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>

<AnchorPane prefHeight="245.0" prefWidth="314.0" xmlns="http://javafx.com/javafx/9" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <children>
      <Label layoutX="57.0" layoutY="51.0" text="I'm a dialog">
         <font>
            <Font size="29.0" />
         </font>
      </Label>
      <Button fx:id="exitButton" layoutX="174.0" layoutY="181.0" mnemonicParsing="false" onMouseClicked="#exitButtonOnMouseClicked" text="Exit">
         <font>
            <Font size="14.0" />
         </font>
      </Button>
   </children>
</AnchorPane>
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值