change value在java,ChangeListener ObservableMap“oldValue”是newValue的副本

问题:为什么ChangeListener ObservableMap使用“oldValue”调用newValue的副本?

import javafx.beans.property.SimpleMapProperty;

import javafx.beans.value.ChangeListener;

import javafx.beans.value.ObservableValue;

import javafx.collections.FXCollections;

import javafx.collections.ObservableMap;

public class ObservableMapExample {

SimpleMapProperty map;

class MyChangeListener implements ChangeListener> {

@Override

public void changed(ObservableValue> changed,

ObservableMap oldValue, ObservableMap newValue) {

System.out.println("changed "+changed+" old:"+oldValue+" new:"+newValue);

}

}

ObservableMapExample() {

map = new SimpleMapProperty();

map.setValue(new SimpleMapProperty(FXCollections.observableHashMap()));

map.addListener(new MyChangeListener());

map.put("a", "1");

map.put("b", "2");

map.put("b", "3");

}

public static void main(String[] args) {

System.out.println("ObservableMapExample");

new ObservableMapExample();

}

}

输出:

ObservableMapExample

changed MapProperty [value: MapProperty [value: {a=1}]] old:MapProperty [value: {a=1}] new:MapProperty [value: {a=1}]

changed MapProperty [value: MapProperty [value: {a=1, b=2}]] old:MapProperty [value: {a=1, b=2}] new:MapProperty [value: {a=1, b=2}]

changed MapProperty [value: MapProperty [value: {a=1, b=3}]] old:MapProperty [value: {a=1, b=3}] new:MapProperty [value: {a=1, b=3}]

import javafx.application.Application; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.scene.*; import javafx.scene.layout.AnchorPane; import javafx.scene.text.Font; import javafx.stage.*; import javafx.scene.control.*; public class Main extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) throws Exception { Button b1=new Button("账号登录"); b1.setPrefWidth(100); b1.setPrefHeight(20); b1.setStyle( "-fx-background-color: #B22222;"+ "-fx-text-fill: #FFFAFA;" ); Label label=new Label("账号 :"); Label label2=new Label("密码 :"); label.setFont(Font.font(13)); label2.setFont(Font.font(13)); TextField textField1=new TextField(); textField1.setPrefHeight(25); textField1.setPrefWidth(200); textField1.setFont(Font.font(12)); Tooltip tip=new Tooltip("请输入12位学号"); textField1.setPromptText("请输入12位学号"); tip.setFont(Font.font(10)); textField1.setTooltip(tip); PasswordField passwordField=new PasswordField(); passwordField.setPrefHeight(25); passwordField.setPrefWidth(200); Tooltip tooltip2=new Tooltip("请输入八位密码"); passwordField.setPromptText("请输入八位密码"); textField1.textProperty().addListener((new ChangeListener<String>() { @Override public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) { if (newValue.length()>12){ textField1.setText(oldValue); } } })); passwordField.textProperty().addListener((new ChangeListener<String>() { @Override public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) { if (newValue.length()>8){ passwordField.setText(oldValue); } } })); /*ImageView img = new ImageView(); Image image=new Image(getClass().getResourceAsStream("/image/iocn.jpg")); img.setImage(image);*/ passwordField.setTooltip(tooltip2); AnchorPane anchorPane=new AnchorPane(textField1); anchorPane.getChildren().add(passwordField); anchorPane.getChildren().add(b1); anchorPane.getChildren().addAll(label2,label); anchorPane.setTopAnchor(label,150.0); anchorPane.setLeftAnchor(label,140.0); anchorPane.setTopAnchor(label2,180.0); anchorPane.setLeftAnchor(label2,140.0); anchorPane.setTopAnchor(textField1,150.0); anchorPane.setLeftAnchor(textField1,180.0); anchorPane.setTopAnchor(passwordField,180.0); anchorPane.setLeftAnchor(passwordField,180.0); anchorPane.setTopAnchor(b1,220.0); anchorPane.setLeftAnchor(b1,220.0); Scene scene=new Scene(anchorPane); primaryStage.setHeight(500); primaryStage.setWidth(550); primaryStage.setScene(scene); primaryStage.show(); } }
03-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值