JavaFX--第3天窗口布局
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();
} }
JavaFX--第3天窗口布局的更多相关文章
- VS2013中如何更改主题颜色(深色)和恢复默认的窗口布局
1.通常情况下,我们会根据个人爱好更改VS2013的主题颜色,一开始我喜欢白色,后来我偏爱深色. 依次选择:工具->选项->常规->主题->深色->确定,ok 2.我们在 ...
- QLayout窗口布局
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QLayout窗口布局 本文地址:http://techieliang.com/201 ...
- 保持plsql窗口布局
在window菜单中有个 save layout 项,设置好窗口布局后,选一下此项就保存你当前的窗口布局了,下次启动就不用再设置了.
- 06--Qt窗口布局
Qt窗口布局 标签: qtlayout 2012-05-05 07:56 3076人阅读 评论(0) 收藏 举报 分类: Qt开发(33) 版权声明:本文为博主原创文章,未经博主允许不得转载. 布 ...
- IDEA如何重置窗口布局
如何重置窗口布局 我不知道怎么搞的,左边的,上边的,下边的,视图都没有了 , 重启了一下,然后重置为默认视图,就好了
- IDEA的窗口布局设置
修改idea的窗口布局 idea默认的窗口模式是如: 可以通过File->Appearance->Window Options->勾选 Widescreen tool window ...
- Visual Studio 重置窗口布局
Visual Studio 重置窗口布局
- JavaFX(一)窗口跳转
笔者此处不讲JavaFX的基础API,只针对笔者工作时遇到的问题进行记录与总结. 零基础的网友可以访问http://www.javafxchina.net/blog/docs/tutorial1/进行 ...
- Window窗口布局 --- DecorView浅析
开发中,通常都是在onCreate()中调用setContentView(R.layout.custom_layout)来实现想要的页面布局,我们知道,页面都是依附在窗口之上的,而DecorView即 ...
随机推荐
- nodejs nodejs的操作
nodejs的操作 由于版本造成的命令不能正常安装,请参考五问题 一.概念: 参考百度百科: http://baike.baidu.com/link?url=aUrGlI8Sf20M_YGk8mh-- ...
- hadoop下HDFS基本命令使用
前提:启动hadoop 1. 查看hdfs下 " / " 的目录 hdfs dfs -ls / 2. 创建文件夹(在 " / " 创建hadoop文件夹) hd ...
- NSNumber数字
前言 将基本数据类型包装成 OC 对象 1.NSNumber 与 基本数据类型 的相互转换 // 基本数据类型 转 NSNumber // 对象方法,将整形数据转换为 OC 对象 NSNumber * ...
- 关于cuda拷贝的速度测试
由于没有使用profiler,仅仅通过简单的传输函数测试,如下测试了10000个点,1000000个点,100000000个点的速度: 均按时钟周期来计时,通过MAX调整数据 int main(){ ...
- python 字符串,bytes和hex字符串之间的相互转换
import binascii datastr='13'#string 类型转换为bytedataByte=str.encode(datastr)#byte串 转换为16进制 byte串 ,比如 b' ...
- P5056 【模板】插头dp
\(\color{#0066ff}{ 题目描述 }\) 给出n*m的方格,有些格子不能铺线,其它格子必须铺,形成一个闭合回路.问有多少种铺法? \(\color{#0066ff}{输入格式}\) 第1 ...
- P2948 [USACO09OPEN]滑雪课Ski Lessons
题意:Bessie去滑雪,限时T,滑雪场有S节课 每节课开始于$m_i$,长度为$l_i$,可以将Bessie的能力值变成$a_i$(注意是变成不是增加) 有n个滑雪坡,去滑雪需要$c_i$的能力,并 ...
- 选课 ( dp 树形dp 动态规划 树规)
和某篇随笔重了?!!?!?!?!?!?不管了留着吧 题目: 在大学里每个学生,为了达到一定的学分,必须从很多课程里选择一些课程来学习,在课程里有些课程必须在某些课程之前学习,如高等数学总是在其它课程之 ...
- socket 释放全过程
1.close()函数:立即返回到进程 int close(int sockfd); //返回成功为0,出错为-1. close 一个套接字的默认行为是把套接字标记为已关闭,然后立即返回到调用 ...
- linux物理内存管理
1.为什么需要连续的物理内存: Linux内核管理物理内存是通过分页机制实现的,它将整个内存划分成无数个4k(在i386体系结构中)大小的页,从而分配和回收内存的基本单位便是内存页了.利用分页管理有助 ...