1.windows之間的交互
2.關閉程序
3.布局鑲嵌
1.windows之間的交互
我們要實現“確定”、“取消”之類的功能:就像我們平時使用Word的時候要關閉會提示要不要保存的信息。
步驟如下:1、創建一個新的窗口 ConfirmBox.java 通過ConfirmBox來實現,在Main中調用Display方法。
2、在Main.java的文件中設置點擊按鈕調用ConfirmBox類的方法。
ConfirmBox.java
package application; 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; public class ConfirmBox { static boolean answer; //Store the answer public static boolean display(String title, String message){ Stage window = new Stage(); window.initModality(Modality.APPLICATION_MODAL); window.setTitle(title); window.setMinWidth(250); window.setMinHeight(400); Label label1 = new Label(); label1.setText(message); //Create 2 buttons Button YesButton = new Button("YES"); //button yes Button NoButton = new Button("NO"); //button no
//Yes Button YesButton.setOnAction(e->{ answer = true; System.out.println("You Click YES"); window.close(); });
//Click No Button NoButton.setOnAction(e->{ answer = false; System.out.println("You Click NO"); window.close(); }); VBox layout = new VBox(10); layout.getChildren().addAll(label1,YesButton,NoButton); layout.setAlignment(Pos.CENTER); Scene scene = new Scene (layout); window.setScene(scene); window.showAndWait(); return answer; } }
Main.java
package application; import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.StackPane; public class Main extends Application{ Stage window; Button button; public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) throws Exception{ window = primaryStage; window.setTitle("This is a title"); button = new Button("Click me"); button.setOnAction(e ->{ ConfirmBox.display("ConfirmBox Title", "Are you sure want to send a nake pic?"); }); StackPane layout =new StackPane(); layout.getChildren().add(button); Scene scene =new Scene(layout, 400,400); window.setScene(scene); window.show(); } }
顯示效果如下:
2.關閉程序
添加一下代碼:將button調用的改成關閉程序方法
button.setOnAction(e -> CloseProgram());
此時並沒有CloseProgram的方法,需要手動創建:
private void CloseProgram(){ System.out.println("File is Saved"); window.close(); }
此時實現了關閉程序的功能。
將程序進行修改
private void CloseProgram(){ Boolean answer = ConfirmBox.display("Title", "Sure you want to exit?"); if(answer) window.close(); }
此時的CloseProgram方法調用前面使用的ConfirmBox中的方法,關閉的時候會彈出提示框,YES或者NO來決定是否關閉窗口。
在Start方法中添加以下代碼:點擊右上角的x,調用closeProgram方法。
window.setOnCloseRequest(e -> CloseProgram());
同樣的,原來寫的代碼中click me 按鈕也是調用這個方法。
3.布局鑲嵌
package application; import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.scene.layout.StackPane; import javafx.scene.layout.VBox; public class Main extends Application{ Stage window; Button button; public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) throws Exception{ window = primaryStage; window.setTitle("This is a title"); HBox topMenu = new HBox(); Button buttonA = new Button("File"); Button buttonB = new Button("Exit"); Button buttonC = new Button("View"); topMenu.getChildren().addAll(buttonA, buttonB,buttonC); VBox leftMenu = new VBox(); Button buttonD = new Button("D"); Button buttonE = new Button("E"); Button buttonF = new Button("F"); leftMenu.getChildren().addAll(buttonD, buttonE,buttonF); BorderPane borderPane = new BorderPane(); borderPane.setTop(topMenu); borderPane.setLeft(leftMenu); Scene scene =new Scene(borderPane, 400,400); window.setScene(scene); window.show(); } }