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



我在研究javaFX上事件处理程序的机制,但我不确定我是否理解了它,其实我有点怀疑: 如果我有两个对象,它们具有处理事件(事件处理程序接口 ecc..(所需的所有代码,它们属于相同的堆栈窗格,问题是:有没有办法让第一个对象启动一个事件(例如 ActionEvent(,尽管它们属于同一个窗格,但将由 2 个对象处理? 因为就我对"事件路线"的了解而言,这是不可能的,至少是直接的。 从本质上讲,我的小程序有一个拆分窗格,将屏幕分成两个堆栈窗格,在左侧面板中,我放了一个带有按钮的网格窗格,每个网格窗格都具有允许绘制不同形状的功能,在右侧面板中带有画布。

我的想法是在每个按钮的setonaction中启动一个ActionEvent,在画布上实现事件处理程序以捕获事件 使用相对手柄方法,并在手柄模式下区分单击哪个按钮以绘制正确的形状。 有人可以帮助我吗?无论如何非常感谢

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());
}

}

/*
* To change this license header, choose License Headers in Project Properties.
package es1;
import javafx.event.ActionEvent;
javafx.event.EventHandler;
import javafx.scene.control.TextField;
/**
*
* @author DAVIDE
*/
public class Textfield extends TextField implements EventHandler<ActionEvent> {

@Override
public void handle(ActionEvent event) {
this.appendText(event.getSource().toString());
}
}

最新更新