JavaFX笔记 發表於 2019-04-02 | 分類於 代码仓库 | 字數統計: 183 | 閱讀時長 ≈ 1 这是用来存放一些Java FX中代码写法的仓库,可能会比较乱,以后可能会腾出时间啊整理,但谁知道呢😀 JavaFX事件监听123456789101112131415161718192021222324252627282930313233343536373839404142//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.javapublic 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"); } }} 更多范例参考 Post author: Kronos Post link: https://sakronos.github.io/Note/2019/04/02/JavaFX笔记/ Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.