JavaFX--第3天窗口布局
2024-09-05 19:35:45
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();
} }
最新文章
- 412. Fizz Buzz
- Android NDK开发初识
- android防止内存溢出浅析
- 一个比较有意思的C语言问题
- C++ 第一次课堂作业
- Tomcat 6.0 简介
- Android RecyclerView单击、长按事件标准实现:基于OnItemTouchListener + GestureDetector
- paip.消除 Java 的冗长try/catch/finally
- java.lang.NullPointerException 	org.apache.jsp.WEB_002dINF.pages.imagecheck.test_jsp._jspInit(test_jsp.java:22)的原因
- 《javascript高级程序设计》 第25章 新兴的API
- Visual Studio 2013 Update 2 and with Update 2
- 利用ArcGIS Engine、VS .NET和Windows控件开发GIS应用
- 显示 EXCEL 的页签列表
- LightOj_1284 Lights inside 3D Grid
- centos上如何安装mysql
- [翻译] C++ STL容器参考手册 (总册)
- Math.random引发的骗术,绝对是用随机数骗前端妹纸的最佳方法
- [易飞]设置导入导出规则-小BUG
- 【爆料】-《维多利亚大学毕业证书》Victoria一模一样原件
- 类别不平衡问题和Softmax回归
热门文章
- wffmpeg64.dll调用 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
- timestamp 在curl中变成了Xtamp
- 微信小程序小结(4) -- 分包加载及小程序间跳转
- 插头DP学习笔记
- __weak 和 __strong 还有Autorelease的用法
- Eclipse报错:Attribute ";xmlns"; was already specified for element ";web-app";.
- 网络工程18级《C++程序设计II》实践作业1
- Kibana6.x.x——启动后警告信息:Session cookies will be transmitted over insecure connections. This is not recommended.
- Codeforces Round #347 (Div. 2) A
- MetricStatTimer