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

最新文章

  1. 412. Fizz Buzz
  2. Android NDK开发初识
  3. android防止内存溢出浅析
  4. 一个比较有意思的C语言问题
  5. C++ 第一次课堂作业
  6. Tomcat 6.0 简介
  7. Android RecyclerView单击、长按事件标准实现:基于OnItemTouchListener + GestureDetector
  8. paip.消除 Java 的冗长try/catch/finally
  9. java.lang.NullPointerException org.apache.jsp.WEB_002dINF.pages.imagecheck.test_jsp._jspInit(test_jsp.java:22)的原因
  10. 《javascript高级程序设计》 第25章 新兴的API
  11. Visual Studio 2013 Update 2 and with Update 2
  12. 利用ArcGIS Engine、VS .NET和Windows控件开发GIS应用
  13. 显示 EXCEL 的页签列表
  14. LightOj_1284 Lights inside 3D Grid
  15. centos上如何安装mysql
  16. [翻译] C++ STL容器参考手册 (总册)
  17. Math.random引发的骗术,绝对是用随机数骗前端妹纸的最佳方法
  18. [易飞]设置导入导出规则-小BUG
  19. 【爆料】-《维多利亚大学毕业证书》Victoria一模一样原件
  20. 类别不平衡问题和Softmax回归

热门文章

  1. wffmpeg64.dll调用 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
  2. timestamp 在curl中变成了Xtamp
  3. 微信小程序小结(4) -- 分包加载及小程序间跳转
  4. 插头DP学习笔记
  5. __weak 和 __strong 还有Autorelease的用法
  6. Eclipse报错:Attribute "xmlns" was already specified for element "web-app".
  7. 网络工程18级《C++程序设计II》实践作业1
  8. Kibana6.x.x——启动后警告信息:Session cookies will be transmitted over insecure connections. This is not recommended.
  9. Codeforces Round #347 (Div. 2) A
  10. MetricStatTimer