同一窗格的对象的事件处理程序

时间:2018-07-05 14:45:07

标签: events javafx

我正在研究javaFX上的事件处理程序的机制,但是我不确定我是否了解它,实际上我有点怀疑: 如果我有两个对象,则它们具有处理事件的所有必需代码(EventHandler接口等),它们在SAME stackPane处为BELONG,问题是:第一个对象是否有办法启动事件(ActionEvent for示例),尽管它们属于同一窗格,但仍将由2个对象处理? 因为对于我所了解的“事件路线”,这是不可能的,至少是直接不可能的。 本质上,我的小程序有一个将屏幕分成两个堆栈的拆分窗格,在左侧窗格中,我使用按钮放置了一个网格窗格,每个窗格都有允许绘制不同形状的功能,在右侧窗格中,使用了画布

我的想法是在每个按钮的setonaction中启动一个ActionEvent,在画布上实现EventHandlers来捕获事件 使用相对手柄方法,并在其手柄模式中区分单击了哪个按钮以绘制正确的形状。 有人能帮我吗 ?还是非常感谢

1 个答案:

答案 0 :(得分:0)

.form{
  flex:1;

}
form {
  background-color: lightgreen;
  margin: -10px auto;
  padding: 20px;
  width: 50%;
 position: relative;
}

form::before {
  content: '';
  width: 0; 
  height: 0; 
  border-left: 20px solid transparent; 
  border-right: 20px solid transparent; 
  border-bottom: 20px solid red;
  position: absolute;
  top: -20px;
  left: 50%;
  margin-left: -20px;
}

form::after {
  content: '';
  width: 0; 
  height: 0;
  border-left: 120px solid transparent; 
  border-right: 120px solid transparent; 
  border-top: 70px solid blue;
  position: absolute;
  bottom: -70px;
  left: 0;
}

            package es1;

            import javafx.application.Application;
            import javafx.event.ActionEvent;
            import javafx.event.Event;
            import javafx.event.EventHandler;
            import javafx.scene.Scene;
            import javafx.scene.control.Button;
            import javafx.scene.control.SplitPane;
            import javafx.scene.control.TextField;
            import javafx.scene.layout.StackPane;
            import javafx.stage.Stage;

    /**
     *
     * @author DAVIDE
     */
         public class Es1 extends Application {

                @Override
                public void start(Stage primaryStage) {
                Button btn = new Button();
                btn.setText("Say 'Hello World'");
                btn.setOnAction(new EventHandler<ActionEvent>() {

                @Override
                public void handle(ActionEvent event) {
                    System.out.println("Hello World!");
                    lanciaevento(this.getClass().toString());

                }
            });

            //add a button on left panel and a textfiled on the right for test if the event launched
            //on the click of the button is reached by the textfield 
            Textfield text = new Textfield();
            StackPane panel1 = new StackPane();
            panel1.getChildren().addAll(btn);
            StackPane panel2 = new StackPane();
            panel2.getChildren().addAll(text);

            splitpane divisore = new splitpane();
            divisore.addEventHandler(ActionEvent.ACTION, divisore);
            divisore.getItems().addAll(panel1,panel2);


            Scene scene = new Scene(divisore, 600, 450);

            primaryStage.setTitle("Hello World!");
            primaryStage.setScene(scene);
            primaryStage.show();

        }


        public void lanciaevento(String oggetto)
        {
            ActionEvent evento    = new ActionEvent();
        }

        /**
         * @param args the command line arguments
         */


        public static void main(String[] args) {
            launch(args);
        }


       }

            package es1;

            import javafx.event.ActionEvent;
            import javafx.event.EventHandler;
            import javafx.scene.control.SplitPane;

            /**
             *
             * @author DAVIDE
             */
            public class splitpane extends SplitPane implements EventHandler<ActionEvent>{

                private String message_event;



                public String get_message()
                {
                 return(message_event);
                }

                public void set_message(String messaggio)
                {
                 message_event = messaggio;   

                }

                @Override
                public void handle(ActionEvent event) {
                            System.out.println("mi ha mandato un messaggio "+event.getSource().toString());

                }



            }