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();
} }