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.csdn.net/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>