有時(shí)候,我們可能有這樣的需求——點(diǎn)擊按鈕時(shí)能夠彈出新的窗口,然后在這個(gè)窗口中提供一些服務(wù)。那么在JavaFX中如何實(shí)現(xiàn)呢?
其實(shí),在JavaFX彈出新的窗口原理很簡單,就是再新建一個(gè)Stage,如下代碼實(shí)現(xiàn)點(diǎn)擊按鈕打開新的窗口,即在按鈕的鼠標(biāo)點(diǎn)擊事件中新建了一個(gè)Stage
package pre.huangjs.blog;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class OpenNewWindow extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
// 創(chuàng)建一個(gè)包含按鈕的APP
Button newWindowBtn = new Button("打開新的窗口");
// 為按鈕添加事件——點(diǎn)擊時(shí)打開新的窗口
newWindowBtn.setOnMouseClicked(new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
// 創(chuàng)建新的stage
Stage secondStage = new Stage();
Label label = new Label("新窗口"); // 放一個(gè)標(biāo)簽
StackPane secondPane = new StackPane(label);
Scene secondScene = new Scene(secondPane, 300, 200);
secondStage.setScene(secondScene);
secondStage.show();
}
});
StackPane mainPane = new StackPane(newWindowBtn); // 創(chuàng)建一個(gè)新的布局
Scene scene = new Scene(mainPane, 500, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

01.gif
現(xiàn)在基本實(shí)現(xiàn)了我們的功能,但是還是存在一個(gè)不足,我們?cè)陉P(guān)閉主窗口primaryStage時(shí),打開的secondStage不會(huì)自動(dòng)關(guān)閉,這個(gè)不太合理,如果想達(dá)到主窗口關(guān)閉時(shí)子窗口關(guān)閉,那么需要設(shè)置主窗口的setOnCloseRequest(EventHandler<WindowEvent> value)方法,具體代碼:
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>(){
public void handle(WindowEvent event) {
Platform.exit();
}
});

02.gif
JavaFX初學(xué)者,如有術(shù)語使用不正確,請(qǐng)指正!
完整代碼:
package pre.huangjs.blog;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
public class OpenNewWindow extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
// 創(chuàng)建一個(gè)包含按鈕的APP
Button newWindowBtn = new Button("打開新的窗口");
// 為按鈕添加事件——點(diǎn)擊時(shí)打開新的窗口
newWindowBtn.setOnMouseClicked(new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
// 創(chuàng)建新的stage
Stage secondStage = new Stage();
Label label = new Label("新窗口"); // 放一個(gè)標(biāo)簽
StackPane secondPane = new StackPane(label);
Scene secondScene = new Scene(secondPane, 300, 200);
secondStage.setScene(secondScene);
secondStage.show();
}
});
StackPane mainPane = new StackPane(newWindowBtn); // 創(chuàng)建一個(gè)新的布局
Scene scene = new Scene(mainPane, 500, 400);
primaryStage.setScene(scene);
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>(){
public void handle(WindowEvent event) {
Platform.exit();
}
});
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}