JavaFX笔记

这是用来存放一些Java FX中代码写法的仓库,可能会比较乱,以后可能会腾出时间啊整理,但谁知道呢😀

JavaFX事件监听
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//Controller.java
@FXML public void handleChoice(MouseEvent event) {
//box.getSelectionModel().selectedIndexProperty().addListener(new ChoiceEvnt(check,box));
//写法Ⅰ

box.getSelectionModel().selectedIndexProperty().addListener(
(ObservableValue<? extends Number> ov,
Number old_val,Number new_val)->{
if (box.getSelectionModel().getSelectedIndex()==0)
check.setSelected(false);
else
check.setSelected(true);
});
}
//写法Ⅱ

//ChoiceEvnt.java
public class ChoiceEvnt implements ChangeListener<Number> {
CheckBox checkBox;
ChoiceBox choiceBox;

public ChoiceEvnt(CheckBox checkBox, ChoiceBox choiceBox) {
//System.out.println("Created.");
this.checkBox = checkBox;
this.choiceBox = choiceBox;
}

@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
System.out.println("Change.");
if ((int)choiceBox.getSelectionModel().getSelectedIndex()==0)
{
checkBox.setSelected(false);
System.out.println("select 1");
}
else
{
checkBox.setSelected(true);
System.out.println("select 2");
}
}
}

更多范例参考