JFX学习笔记 五 重修语法——表达式

学习笔记四里面各位也都看到了,在语法上JFX与JAVA有许多地方相似。这里我主要提一下与JAVA不同之处。
关系操作符
[b]JFX[/b] [b]JAVA[/b]
== equality ==
<> inequality !=
< less than <
> greater than >
<= less than or equal <=
>= greater than or equal >=
布尔操作符
and logical and &&
or logical or ||
not logical negation !
算术操作符
+ addition +
- subtraction; unarynegation -
* multiplication *
/ division /
% remainder %
+= add and assign +=
-= subtract and assign -=
*= multiply and assign *=
/= divide and assign /=
%= remainder and assign %=
其它操作符

not logical negation !
算术操作符
+ addition +
-
subtraction; unary
negation
-
* multiplication *
/ division /
% remainder %
+= add and assign +=
-= subtract and assign -=
*= multiply and assign *=
/= divide and assign /=
%=
remainder and
assign
%=
其它操作符
sizeof array length n/a
indexof ordinal position n/a
if e1 then e2 else e3 conditional expression e1 ? e2 :e3
select list comprehension n/a
foreach list comprehension n/a
new allocation new
op() function/operation call n/a
x.op() member function/operation call x.o p()
instanceof type check instanceof
this self access this.

bind [lazy] incremental [lazy] evaluation n/a
: eager initialization n/a
[] array selection []
format as String formatting n/a
<<>> Identifier quotes n/a
{} String expression n/a
(expr) grouping
reverse reverses a list n/a
[number1,next..number2] numeric range n/a

例子:
import java.lang.System;
import java.lang.Math;
var x = 2;
var y = 4;
var a = true;
var b = false;
System.out.println(x == y); // prints false
System.out.println(x <> y); // prints true
System.out.println(x < y); // prints true
System.out.println(x > y); // prints true
System.out.println(x >= y); // prints false
System.out.println(x <= y); // prints true
System.out.println(x + y); // prints 6
System.out.println(x - y); // prints -2
System.out.println(x * y); // prints 8
System.out.println(x / y); // prints 0.5
System.out.println(x % y); // prints 2
System.out.println(a and b); // prints false
System.out.println(a or b); // prints true
System.out.println(not a); // prints false
System.out.println(sizeof [x,y]); // prints 2
System.out.println([x,y][indexof . == 0]); // prints 2
System.out.println(if a then x else y); // prints 2
System.out.println(select q from q in [x, y] where q > 3); prints 4
System.out.println(foreach(q in [x, y] where q < 3) q); prints 2
System.out.println(Math.max(x, y)); // prints 4
System.out.println("abc".toUpperCase()); // prints ABC
System.out.println(x instanceof Number); // prints true
x = 10;
System.out.println(x); // prints 10

需要注意的是,1 JFX中字符串可以用单引号和双引号两种方式表示,双号表示可以在内嵌入{}表达式.并且支持换行操作,JAVA是不允许的
var answer = true;
var s = "The answer is {if answer then "Yes" else "No"}"; // s = 'The answer is Yes';
var s = "This
contains
new lines";
2 <<>> 如果你的变量名使用了关键字,通常是不允许的。如果使用<<变量名>>则没有约束
var this = 100;//不允许的
var <<this>> = 100;//允许
当然<<>>不是拿给大家用着玩的,主要出于多种环境可能出现命名调用重突的考虑,例如方法名为insert,但在JFX中insert是关键字,那么如何区分,就得靠<<>>了
var textArea = new JTextArea();
textArea.<<insert>>("He llo", 0);
3 字符串(String)、数值(Number)和日期的格式化。JavaFX 有内建的字符串格式化操作符(format as),语法:表达式format as 指令
format as 操作符支持java.text.DecimalFormat、java.text.SimpleDateFormat 和java.util.Formatter
的格式化指令:如果格式化指令以%开头,那么将会使用java.util.Formatter;如果表达式是
Number 类型,则使用java.text.DecimalFormat;如果表达式是java.util.Date 类型,则使用java.text.SimpleDateFormat。指令操作数是一个在语法上的标识符,而不是一个表达式。这就允许了在编译时静态检查指令内容的正确性。

例如:
import java.util.Date;
100.896 format as <<%f>>; // yields '100.896000'
31.intValue() format as <<%02X>>; // yields '1F'
var d = new Date();
d format as <<yyyy-MM-dd'T'HH:mm:ss.SSSZ>>; // yields '2005-10-31T08:04:31.323-0800'
0.00123 format as <<00.###E0>>; // yields '12.3E-4'

4操作,在JavaFX 中使用operation 关键字声明过程(procedure)。例如:
import java.lang.StringIndexOutOfBoundsException;
operation substring(s:String, n:Number): String {
try {
return s.substring(n);
} catch (e:StringIndexOutOfBoundsException) {
throw "sorry, index out of bounds";
}
}
在上例中定义了一个称为“substring”的新过程,它接受两个参数:第一为字符串类型的参数“s”,第二为Number 类型的参数“n”,而返回值为字符串类型。操作的出现用途,在以后的笔记中详细解释,目前可以理解成方法或函数

5 Try 语句
JavaFX 的try 语句用法类似Java,但它具有JavaFX 变量声明语法。注意:在JavaFX 中,任意对象都能够被抛出和捕捉,而并非仅仅是java.lang.Throwable 的继承类。
例如:
try {
throw "He llo";
} catch (s:String) {
System.out.println("caught a String: {s}");
} catch (any) {
System.out.println("caught something not a String: {any}");
} finally {
System.out.println("finally...");
}
增加了异常处理的灵活性不是吗,而且处理程序的手段又多了一种看似更合法的处理,扔对象。而不是扔异常。

6 for语句,JFX把for是改的让我觉得很不顺眼。
JavaFX 的for 语句头与foreach 列表推导操作符(list comprehension operator)使用相同的语法。但是,在下面示例中for 语句体处理的是由列表推导生成的成员。
例如:
for (i in [0..10]) {
System.out.println("i = {i}");
}
// print only the even numbers using a filter
for (i in [0..10] where i % 2 == 0) {
System.out.println("i = {i}");
}
// print only the odd numbers using a range expression
for (i in [1,3..10]) {
System.out.println("i = {i}");
}
// print the cartesian product
for (i in [0..10], j in [0..10]) {
System.out.println(i);
System.out.println(j);
}
最后一个等价于
for(i in [0..10])
{
System.out.println(i);
for(j in [0..10])
{
System.out.println(j);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaFX中,我们可以通过添加窗口关闭事件的监听器来监视窗口关闭。通过监听窗口关闭事件,我们可以在窗口关闭之前执行一些必要的操作。 要监听窗口关闭事件,我们首先需要获取窗口的Scene对象,然后通过Scene对象获取窗口Stage对象。接下来,我们可以使用Stage的setOnCloseRequest()方法来设置一个窗口关闭事件的监听器。 例如,以下是一个简单的代码示例,演示了如何监听窗口关闭事件: ```java import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.StackPane; import javafx.stage.Stage; public class WindowCloseListenerExample extends Application { @Override public void start(Stage primaryStage) { primaryStage.setTitle("Window Close Listener Example"); // 创建一个按钮 Button closeButton = new Button("Close Window"); closeButton.setOnAction(event -> primaryStage.close()); // 创建一个布局,并将按钮添加到布局中 StackPane layout = new StackPane(); layout.getChildren().add(closeButton); // 创建一个场景,并将布局添加到场景中 Scene scene = new Scene(layout, 300, 200); primaryStage.setScene(scene); // 监听窗口关闭事件 primaryStage.setOnCloseRequest(event -> { // 在窗口关闭之前执行一些操作,比如保存数据或清理资源 System.out.println("Window is closing..."); }); primaryStage.show(); } public static void main(String[] args) { launch(args); } } ``` 在上面的例子中,我们创建了一个简单的JavaFX应用程序,其中包含一个窗口和一个按钮。当点击按钮时,程序会关闭窗口。同时,在窗口关闭之前,我们通过setOnCloseRequest()方法设置了一个窗口关闭事件的监听器,并在监听器中打印出一条消息。 当我们运行上述代码时,我们可以看到当点击窗口的关闭按钮时,程序会先输出"Window is closing...",然后关闭窗口。 这就是如何在JavaFX中监听窗口关闭事件的基本过程。通过监听窗口关闭事件,我们可以在窗口关闭之前执行一些必要的操作,以确保程序的正常退出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值