如何在JavaFX中从Swing实现MouseListener

时间:2022-09-02 15:09:26

I have a simple login panel that I wrote using Swing and now I have to redo everything with JavaFX, but I don't know how to convert the ActionListeners/MouseListeners.

我有一个使用Swing编写的简单登录面板,现在我必须使用JavaFX重做所有内容,但我不知道如何转换ActionListeners / MouseListeners。

如何在JavaFX中从Swing实现MouseListener

Whenever the "Neu-Anmeldung" checkbox is checked/unchecked, the values true/false should be seen in the Eclipse console.

每当选中/取消选中“Neu-Anmeldung”复选框时,应在Eclipse控制台中看到值true / false。

My Swing code for this purpose:

我的Swing代码用于此目的:

// ...

this.jBoxNeuAnmeldung.addMouseListener(new MouseAdapter() {
      @Override
      public void mouseClicked(MouseEvent e) {
        if (LoginFrame.this.jBoxNeuAnmeldung.isSelected())
          neuAnmeldung = true;
        else
          neuAnmeldung = false;
        System.out
            .println("Neu-Anmeldung Checkbox angekreutzt? " + neuAnmeldung);
      }
    });

// ...

How do I do the same thing with JavaFX?

如何使用JavaFX执行相同的操作?

3 个解决方案

#1


1  

    ...
    checkBox.setOnAction((event) -> {
        neuAnmeldung = checkBox.isSelected();
        System.out.println("Neu-Anmeldung Checkbox angekreutzt? "
                + neuAnmeldung);
    });

replace checkBox by check box name, e.g.

用复选框名称替换checkBox,例如

    final CheckBox jBoxNeuAnmeldung = new CheckBox();
    ...
    jBoxNeuAnmeldung.setOnAction((event) -> {
        neuAnmeldung = jBoxNeuAnmeldung.isSelected();
        System.out.println("Neu-Anmeldung Checkbox angekreutzt? "
                + neuAnmeldung);
    });     

(in standard Java 8 you can also omit final in CheckBox when used in labmda expression).

(在标准Java 8中,当在labmda表达式中使用时,您也可以省略CheckBox中的final)。

#2


0  

By adding a ChangeListener to the selected property

通过向所选属性添加ChangeListener

checkBox.selectedProperty().addListener((observable, oldValue, newValue) -> System.out.println("Neu-Anmeldung Checkbox angekreutzt? " + newValue));

#3


0  

@FXML
  public CheckBox checkBoxNeuAnmeldung;

  @FXML
  public void handleCheckBoxNeuAnmeldungAction(ActionEvent event) {
    if (checkBoxNeuAnmeldung.isSelected()) {
      neuAnmeldung = true;
    } else
      neuAnmeldung = false;
    System.out.println("Neu-Anmeldung Checkbox angekreutzt? " + neuAnmeldung);
  }

I managed to implement a simpler method using ActionEvent and connected it tp my FXML file.

我设法使用ActionEvent实现一个更简单的方法,并将其连接到我的FXML文件。

#1


1  

    ...
    checkBox.setOnAction((event) -> {
        neuAnmeldung = checkBox.isSelected();
        System.out.println("Neu-Anmeldung Checkbox angekreutzt? "
                + neuAnmeldung);
    });

replace checkBox by check box name, e.g.

用复选框名称替换checkBox,例如

    final CheckBox jBoxNeuAnmeldung = new CheckBox();
    ...
    jBoxNeuAnmeldung.setOnAction((event) -> {
        neuAnmeldung = jBoxNeuAnmeldung.isSelected();
        System.out.println("Neu-Anmeldung Checkbox angekreutzt? "
                + neuAnmeldung);
    });     

(in standard Java 8 you can also omit final in CheckBox when used in labmda expression).

(在标准Java 8中,当在labmda表达式中使用时,您也可以省略CheckBox中的final)。

#2


0  

By adding a ChangeListener to the selected property

通过向所选属性添加ChangeListener

checkBox.selectedProperty().addListener((observable, oldValue, newValue) -> System.out.println("Neu-Anmeldung Checkbox angekreutzt? " + newValue));

#3


0  

@FXML
  public CheckBox checkBoxNeuAnmeldung;

  @FXML
  public void handleCheckBoxNeuAnmeldungAction(ActionEvent event) {
    if (checkBoxNeuAnmeldung.isSelected()) {
      neuAnmeldung = true;
    } else
      neuAnmeldung = false;
    System.out.println("Neu-Anmeldung Checkbox angekreutzt? " + neuAnmeldung);
  }

I managed to implement a simpler method using ActionEvent and connected it tp my FXML file.

我设法使用ActionEvent实现一个更简单的方法,并将其连接到我的FXML文件。