JavaFX--第2天-窗口基本的类
1 内部匿名类和Lambda表达式
2 Switching Scene
3 信息提示框 (Alert Boxes)
前情回顾:
前面的学习内容:关于JavaFX的基本概念,以及窗口所使用的类的一个介绍
学习了如何运用事件对一个按钮做出最简单的回应—click me 点击。
1 内部匿名类和Lambda表达式
在之前的例子上对
button.setOnAction(this);
进行更改
button.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event){
System.out.println("I am an annonymous inner class");
}
});
此时点击按钮调用的时间就是我们后来修改的,不用去检验每个按钮的名字,直接在生成对象之后对象的方法上调用内部类,使得事件发生。"Click me"。
但是后来会出现一个问题。按照上一次的想法我们有很多个按钮的时候会写出if条件结构,然后还要去对应代码中的对象,但是都使用内部匿名类也不方便。
甲骨文公司在Java 8中开始加入了Lambda表达式,此时将这个语句改成如下:
button.setOnAction(e-> System.out.println("heyyyyy, I am Lambda"));
此时控制台对我们点击了按钮进行回应:heyyyyy, I am Lambda,Java自动帮我们处理这个事件。同时也可以改成
button.setOnAction(e->{
System.out.println("heyyyyy, I am Lambda1");
System.out.println("heyyyyy, I am Lambda2");
System.out.println("heyyyyy, I am Lambda3");
});
2 Switching Scene
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox; public class Main extends Application{ Stage window;
Scene scene1,scene2; public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception{
window = primaryStage;
Label label1 = new Label("This is Scene1");
Button button1 = new Button("Go to Scene2");
button1.setOnAction(e -> window.setScene(scene2));
//Layout 1 - children are laid out in vertical column
VBox layout1 = new VBox(20);
layout1.getChildren().addAll(label1,button1);
scene1 = new Scene(layout1,200,200); //200x200 pixel //Button2
Button button2 = new Button("Go back to Scene1");
button2.setOnAction(e -> window.setScene(scene1));
//layout2
StackPane layout2 = new StackPane();
layout2.getChildren().addAll(button2);
scene2 = new Scene(layout2, 200, 200);
window.setScene(scene1);
window.setTitle("This is a title");
window.show();
}
}
研究Scene1和Scene2 的两种不同的情况,Scene的切换通过点击Button来实现。这个例子看起来有点像我们平时使用的软件,比如说我们要关闭一个word文档的时候会发现此时,系统弹出一个窗口,问是否保存。有时候系统出错,也会弹出一个窗口来提示错误。下面将介绍具体的例子。
3 信息提示框 (Alert Boxes)
点击按键之后弹出对话框
此时就很像我们实现AlertBox,如果不解决新弹出窗口,比如关闭,那么旧的窗口就不能操作。
public class AlertBox { public static void display(String title, String message){
Stage window = new Stage(); // make a new Stage for our Scene
window.initModality(Modality.APPLICATION_MODAL); //initiate the Mod by the using the Java Library
window.setTitle(title); //Set the title of the new window
window.setMinWidth(250);
Label label1 = new Label(); //make label to write some message
label1.setText(message);
Button closeButton = new Button("Close the window");
closeButton.setOnAction(e ->window.close()); VBox layout = new VBox(10); // make the Alert box layout
layout.getChildren().addAll(label1, closeButton); //Add the Button and label to the window
layout.setAlignment(Pos.CENTER); Scene scene = new Scene (layout);
window.setScene(scene);
window.show();
window.showAndWait();
} }
showAndWait 官方解释
public void showAndWait()
Shows this stage and waits for it to be hidden (closed) before returning to the caller. This method temporarily blocks processing of the current event, and starts a nested event loop to handle other events. This method must be called on the FX Application thread.A Stage is hidden (closed) by one of the following means:
- the application calls the
Window.hide()
orclose()
method on this stage - this stage has a non-null owner window, and its owner is closed
- the user closes the window via the window system (for example, by pressing the close button in the window decoration)
- the application calls the
JavaFX--第2天-窗口基本的类的更多相关文章
- firefox快捷键窗口和标签类
firefox快捷键窗口和标签类: 关闭标签: Ctrl+W 或 Ctrl+F4关闭窗口: Ctrl+Shift+W 或 Alt+F4向左移动标签: Ctrl+左方向键 或 Ctrl+上方向键向右移动 ...
- C#中一个窗口是一个类呢,还是一个窗口类的实例呢?(转)
C#中一个窗口是一个类呢,还是一个窗口类的实例呢? 答: 没有一个人说到重点上. 一个窗口,它不是仅仅用一个类可以描述的: 首先,这个窗口的数据类型类型,是从Form类派生下来的,也就是说它的定义是一 ...
- JavaFX(三)窗口拖动
1.问题场景 在上一篇中,我们将窗口的默认标题栏隐藏从而导致鼠标点击窗体无法进行拖动. 2.解决思路 给组件添加鼠标按下事件监听器和鼠标拖动事件监听器. 3.代码实现 代码片段: private do ...
- JavaFX(一)窗口跳转
笔者此处不讲JavaFX的基础API,只针对笔者工作时遇到的问题进行记录与总结. 零基础的网友可以访问http://www.javafxchina.net/blog/docs/tutorial1/进行 ...
- 窗口移动--基类(BaseForm)
#region 窗口移动 private bool _isLeftButtonDown = false; public const int HTCAPTION = 0x0002; protected ...
- javafx实现模态/模式窗口
import javafx.stage.*; import javafx.scene.*; import javafx.scene.paint.Color; import javafx.scene.c ...
- VC----Class Style类风格和窗口风格
CS_BYTEALIGNCLIENT:以字节边界来对齐窗口客户区,这个风格会影响 窗口 的宽度和水平位置.实际上没有看到效果. CS_BYTEALIGNWINDOW:以字节边界来对齐窗口,这个风格会影 ...
- 通用窗口类 Inventory Pro 2.1.2 Demo1(上)
插件功能 按照Demo1的实现,使用插件来实现一个装备窗口是很easy的,虽然效果还很原始但是也点到为止了,本篇涉及的功能用加粗标出,具体的功能如下: 1.实现了两个窗口,通过点击键盘I来,打开或者关 ...
- 探索Win32系统之窗口类(转载)
Window Classes in Win32 摘要 本文主要介绍win32系统里窗口类的运做和使用机制,探索一些细节问题,使win32窗口类的信息更加明朗化. 在本文中,"类", ...
随机推荐
- C#设计模式(7)——适配器模式
一.概述 将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以在一起工作. 二.模型 三.代码实现 using System; /// 这里以 ...
- shell入门-cut命令
命令:cut 选项:-d:-f 指定第几段由“:(分割符)”分割的段 -c 指定第几个字符 说明:选取命令,选取一段数据中我们想要的,一般是针对每行来分析选取的 [root@wangshaoj ...
- [cf687c]The Values You Can Make(01背包变形)
题意:给定n个硬币,每个硬币都有面值,求每个能构成和为k的硬币组合中,任意个数相互求和的总额种类,然后将所有硬币组合中最后得到的结果输出. 解题关键:在01背包的过程中进行dp.dp[i][j]表示组 ...
- 【总结整理】javascript进阶学习(慕课网)
数组 数组是一个值的集合,每个值都有一个索引号,从0开始,每个索引都有一个相应的值,根据需要添加更多数值. 二维数组 二维数组 一维数组,我们看成一组盒子,每个盒子只能放一个内容. 一维数组的表示: ...
- R 数据类型
c()功能函数,产数据用 向量:一维数组,要求存放的数据类型一致 矩阵:二维数组,要求存放的数据类型一致,用通过matrix函数创建 数组:维度超过二维时建议用数组,用可araay函数创建 数据框:相 ...
- 15、Linux 文件属性和测试( chgrp,chown,chmod和-e -f -d -s
一.更改文件属性 1.chgrp:更改文件属组 语法: chgrp [-R] 属组名文件名 参数选项 -R:递归更改文件属组,就是在更改某个目录文件的属组时,如果加上-R的参数,那么该目录下的所有文件 ...
- java File基本操作,以及递归遍历文件夹
java 的文件操作,相对来说是比较重要的,无论是编写CS还是BS程序,都避免不了要与文件打交道,例如读写配置文件等.虽然现在很多框架都直接帮你做好了这一步! java.io.File 底层是调用与c ...
- 一步步教你学会browserify
本文来自网易云社区 作者:孙圣翔 注意 文章需要边看边练习,不然你可能忘得速度比看的还快. 原文地址: http://my.oschina.net/goskyblue/blog/552284 Brow ...
- 2017乌鲁木齐区域赛A(动态规划,组合数学,期望)
#include<bits/stdc++.h>using namespace std;double c[110][110];double g[110];double dp[110][110 ...
- matplotlib.pyplot import报错: ValueError: _getfullpathname: embedded null character in path
Environment: Windows 10, Anaconda 3.6 matplotlib 2.0 import matplotlib.pyplot 报错: ValueError: _getfu ...