On Mouse Entered 交换按钮位置的方法

On Mouse Entered Method to swap Button position

我想开发一个鼠标输入的方法,使用 FXML 和 JavaFX 实时交换两个按钮的位置,不幸的是我对它很陌生。 Relocate(x,y)、get/setLayoutX/Y 和低于 get/setTranslateX/Y 都会抛出 IllegalArgumentEceptions,在堆栈跟踪中没有更多可理解的信息。为了获取和设置实时位置交换,首选按钮 属性 是什么?

@FXML protected void neinHover (ActionEvent evt){

        double jTmpX, jTmpY, nTmpX, nTmpY;
        nTmpX = neinButton.getTranslateX();
        nTmpY = neinButton.getTranslateY();
        jTmpX = jaButton.getTranslateX();
        jTmpY = jaButton.getTranslateY();
        jaButton.setTranslateX(nTmpX);
        jaButton.setTranslateY(nTmpY);
        neinButton.setTranslateX(jTmpX);
        neinButton.setTranslateY(jTmpY);    
    }

我想你想要这样的东西:

FXML:

<fx:root onMouseClicked="#swap" type="Pane" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8">
    <children>
        <Button fx:id="b1" mnemonicParsing="false" text="1" />
        <Button fx:id="b2" layoutX="90.0" mnemonicParsing="false" text="2" />
    </children>
</fx:root>

控制器:

public class MockPane extends Pane {
    @FXML
    private Button b1;
    @FXML
    private Button b2;


    public MockPane() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(
                "MockPane.fxml"));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);
        try {
            fxmlLoader.load();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    @FXML
    private void swap() {
        double b1x = b1.getLayoutX();
        double b1y = b1.getLayoutY();
        double b2x = b2.getLayoutX();
        double b2y = b2.getLayoutY();
        b1.setLayoutX(b2x);
        b1.setLayoutY(b2y);
        b2.setLayoutX(b1x);
        b2.setLayoutY(b1y);
    }
}

应用程序:

public class MockApp extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }


    @Override
    public void start(Stage stage) throws Exception {
        MockPane root = new MockPane();
        Scene scene = new Scene(root, 200, 100);
        stage.setScene(scene);
        stage.show();
    }
}