有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JavaFX事件回调回父单元

我有一个JavaFX程序,我希望为按钮分配一个事件处理程序,但我希望该事件触发父单元中的一个操作

我有一个GridPane作为我的主窗格。 当我启动程序时,我在这个网格窗格中的固定位置(0,0)嵌入了另一组形状(称为块)。 作为我的模块的一部分,我有一个名为DownArrow的三角形按钮。我的计划是,当按下此按钮时,Doc控制器将在按下三角形的块下方放置一个新块

我正在研究使用DownArrow.setOnMouseClicked事件处理程序,并将其映射回控制器中的例程,以添加新块。。。然而,我需要某种信息回调,让控制器知道按下了哪个块

我被难住了

我想创建一个定制的事件处理程序,我可以传递更多的参数,但这看起来很笨拙——还有什么我应该做的吗

How to pass paremeter with an event in javafx?

请参阅下面的完整代码:

package editorscratchpad;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.scene.text.Font;
import javafx.stage.Stage;

/**
 *
 * @author a_curley
 */
public class EditorScratchPad extends Application {

    HBox mainPanel;
    VBox flowChart;
    GridPane flowchartGrid;
    VBox controlPanel;
    Integer stepCount;
    TextField descData;

    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        descData = new TextField();
        stepCount = 0;

        btn.setText("Add Step");
        btn.setOnAction((ActionEvent event) -> {
            buttonClicked(event);
        });

        ScrollPane sp = new ScrollPane();
        mainPanel = new HBox(20);
        //mainPanel.setPrefWidth(400);
        controlPanel = new VBox(5);
        controlPanel.setAlignment(Pos.CENTER);

        flowChart = new VBox();
        flowChart.setPrefWidth(600);
        flowChart.setAlignment(Pos.CENTER);

        flowchartGrid = new GridPane();
        flowchartGrid.setPrefWidth(600);
        flowchartGrid.setHgap(10);
        flowchartGrid.setVgap(10);
        flowchartGrid.setPadding(new Insets(0, 10, 0, 10));

        //sp.setContent(flowChart);
        sp.setContent(flowchartGrid);

        controlPanel.getChildren().add(btn);
        controlPanel.getChildren().add(descData);
        mainPanel.getChildren().add(sp);
        mainPanel.getChildren().add(controlPanel);

        Scene scene = new Scene(mainPanel, 800, 500);
        primaryStage.sizeToScene();

        primaryStage.setTitle("Flow chart");
        primaryStage.setScene(scene);

        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    public void buttonClicked(ActionEvent event) {
        //Integer Step = stepCount*10;        
        //Integer Step = (flowChart.getChildren().size()+1)*10;
        Integer Step = (flowchartGrid.getChildren().size() + 1);
        String stateDesc = descData.getText();
        if (stateDesc.length() < 3) {
            stateDesc = "State " + Step.toString();
        }

        blockComponent newBlock = new blockComponent("S" + Integer.toString(Step * 10), stateDesc, Step); //<<<< Create the Block.
        //flowChart.getChildren().add(newBlock.getComponent());
        flowchartGrid.add(newBlock.getComponent(), 0, Step);
        System.out.println("Added S" + Integer.toString(Step * 10));
        stepCount++;
    }

    public void addNewBlock(ActionEvent event) {
        Integer Step = (flowchartGrid.getChildren().size() + 1);
        String stateDesc = "Step " + Integer.toString(Step * 10);
    }

    /**
     * Subclass representing a Block Graphics only.
     */
    public class blockComponent {

        String Name;
        String Desc;
        Integer StateNo;
        Integer XLoc;
        Integer YLoc;
        Integer blockHeight;
        Integer blockWidth;
        Integer stLabRad;

        public blockComponent(String newName, String newDesc, Integer newSt) {
            Name = newName;
            Desc = newDesc;
            StateNo = newSt;
            XLoc = 0;
            YLoc = 0;
            blockHeight = 60;
            blockWidth = 120;
            stLabRad = 10;
        }

        public blockComponent(String newName, String newDesc, Integer newSt, Integer xCoOrd, Integer yCoOrd) {
            this(newName, newDesc, newSt);
            XLoc = xCoOrd;
            YLoc = yCoOrd;
        }

        public Group getComponent() {
            Group thisGroup = new Group();
            // Define Rectangle
            Rectangle Block = new Rectangle();
            Block.setY(stLabRad);
            Block.setX(stLabRad);
            Block.setHeight(blockHeight);
            Block.setWidth(blockWidth);
            Block.setStroke(Color.BLACK);
            Block.setStrokeWidth(2);
            Block.setFill(Color.WHITESMOKE);

            // Define state label
            Circle stLab = new Circle();
            stLab.setCenterX(stLabRad);
            stLab.setCenterY(stLabRad);
            stLab.setRadius(stLabRad);
            stLab.setStroke(Color.PALEGREEN);
            stLab.setFill(Color.PALEGREEN);

            // Define State No.
            Label stNo = new Label();
            stNo.setFont(Font.font("Impact"));
            stNo.setTextFill(Color.WHITE);
            stNo.setLayoutX(0);
            stNo.setLayoutY(0);
            stNo.setText(StateNo.toString());

            // Define the description
            Label stD = new Label();
            stD.setFont(Font.font("Impact"));
            stD.setTextFill(Color.BLACK);
            stD.setLayoutX(15);
            stD.setLayoutY(15);
            stD.setText(Desc.toString());

            //---- Three Triangles for drawing. ----
            // Down Arrow
            Polygon downArrow = new Polygon();
            downArrow.getPoints().addAll(new Double[]{
                //X                                         //Y
                (blockWidth.doubleValue() / 2) + stLabRad, (blockHeight.doubleValue()),
                ((blockWidth.doubleValue() / 2) + 10) + stLabRad, (blockHeight.doubleValue() - 10),
                ((blockWidth.doubleValue() / 2) - 10) + stLabRad, (blockHeight.doubleValue() - 10)
//                70.0,60.0,
//                60.0,50.0,
//                80.0,50.0
            });
            downArrow.setStroke(Color.BLACK);
            downArrow.setStrokeWidth(1);
            downArrow.setFill(Color.LIGHTCYAN);
            downArrow.setOnMouseEntered(new EventHandler<MouseEvent>() {
                public void handle(MouseEvent me) {
                    downArrow.setStroke(Color.BLACK);
                    downArrow.setFill(Color.LIGHTCYAN);
                }
            });
            downArrow.setOnMouseExited(new EventHandler<MouseEvent>() {
                public void handle(MouseEvent me) {
                    downArrow.setStroke(Color.WHITESMOKE);
                    downArrow.setFill(Color.WHITESMOKE);
                }
            });

            // try: https://stackoverflow.com/questions/35372236/how-to-pass-paremeter-with-an-event-in-javafx
            //add all components to the group to display
            thisGroup.getChildren().add(Block);
            thisGroup.getChildren().add(stD);
            thisGroup.getChildren().add(stLab);
            thisGroup.getChildren().add(stNo);
            thisGroup.getChildren().add(downArrow);
            return thisGroup;
        }
    }

}

共 (1) 个答案

  1. # 1 楼答案

    我不确定这是否是我想要的正确方法,但上面针对getSource()的@0009laH响应是最适合我的方法