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>