JavaFXEventFilter实现
我尝试过实现JavaFXJavaFXEventFilter实现,java,javafx,eventfilter,Java,Javafx,Eventfilter,我尝试过实现JavaFXEventFilter,类似于: @FXML public void leftButton() { leftButton.addEventFilter(MouseEvent.MOUSE_PRESSED, new EventHandler < MouseEvent > () { @Override public void handle(MouseEvent event) {
EventFilter
,类似于:
@FXML
public void leftButton() {
leftButton.addEventFilter(MouseEvent.MOUSE_PRESSED,
new EventHandler < MouseEvent > () {
@Override
public void handle(MouseEvent event) {
try {
System.out.println("Left Button pressed, String: " + leftString);
byte[] command = {
(byte) startTx, address, byteOne, panLeft, speedNormal, 0x00, endTx, 0x2B
};
//byte[] bytes = DatatypeConverter.parseHexBinary(upString);
TwoWaySerialComm.SerialWriter sw = new TwoWaySerialComm.SerialWriter(
twoWaySerCom.serialPort.getOutputStream());
sw.out.write(command);
} catch (IOException e) {
e.printStackTrace();
}
}
});
leftButton.addEventFilter(MouseEvent.MOUSE_RELEASED,
new EventHandler < MouseEvent > () {
public void handle(MouseEvent event) {
try {
System.out.println("Left Button released, String: " + stopString);
byte[] command = {
(byte) startTx, address, byteOne, 0x00, 0x00, 0x00, (byte) endTx, 0x0F
};
TwoWaySerialComm.SerialWriter sw = new TwoWaySerialComm.SerialWriter(
twoWaySerCom.serialPort.getOutputStream());
sw.out.write(command);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
@FXML
公共无效左按钮(){
leftButton.addEventFilter(按下MouseEvent.MOUSE_,
新事件处理程序(){
@凌驾
公共无效句柄(MouseeEvent事件){
试一试{
System.out.println(“按下左键,字符串:“+leftString”);
字节[]命令={
(字节)startTx、地址、字节、PANLEVT、speedNormal、0x00、endTx、0x2B
};
//字节[]字节=DatatypeConverter.parseHexBinary(upString);
TwoWaySerialComm.SerialWriter sw=新TwoWaySerialComm.SerialWriter(
twoWaySerCom.serialPort.getOutputStream());
sw.out.write(命令);
}捕获(IOE异常){
e、 printStackTrace();
}
}
});
leftButton.addEventFilter(MouseEvent.MOUSE_已释放,
新事件处理程序(){
公共无效句柄(MouseeEvent事件){
试一试{
System.out.println(“释放左按钮,字符串:“+stopString”);
字节[]命令={
(字节)startTx,地址,字节,0x00,0x00,0x00,(字节)endTx,0x0F
};
TwoWaySerialComm.SerialWriter sw=新TwoWaySerialComm.SerialWriter(
twoWaySerCom.serialPort.getOutputStream());
sw.out.write(命令);
}捕获(IOE异常){
e、 printStackTrace();
}
}
});
}
因此,当单击按钮时,我通过串行端口写入一个字符串,当我松开按钮时,会发送另一个字符串
我有两个问题
1.当我第一次打开应用程序时,我必须按下按钮两次才能发生任何事情
2.每单击一次,该操作就会再次发生。例如,第一次点击“你好”,第二次点击“你好”
我怀疑我的问题是,我的第一次单击注册了EventFilter
,然后每个后续事件都会创建一个新的EventFilter
如何防止这种情况发生
当我第一次打开应用程序时,我必须在任何事情发生之前按下按钮两次
在方法leftButton()
中添加事件过滤器,该方法在按钮的操作中调用。因此,当你第一次点击你的按钮时,它没有附加任何事件过滤器,因此什么也没有发生
要克服这个问题,您可以将代码移动到initialize()
,或者使用单独的方法放置这些代码,然后使用FXML调用它们
按下按钮
public void onPress() {
try {
System.out.println("Left Button pressed, String: " + leftString);
byte[] command = { (byte) startTx, address, byteOne, panLeft, speedNormal, 0x00, endTx, 0x2B };
TwoWaySerialComm.SerialWriter sw = new TwoWaySerialComm.SerialWriter(
twoWaySerCom.serialPort.getOutputStream());
sw.out.write(command);
} catch (IOException e) {
e.printStackTrace();
}
}
发布时
public void onRelease() {
try {
System.out.println("Left Button released, String: " + stopString);
byte[] command = {(byte) startTx, address, byteOne, 0x00, 0x00, 0x00, (byte) endTx, 0x0F};
TwoWaySerialComm.SerialWriter sw = new TwoWaySerialComm.SerialWriter(twoWaySerCom.serialPort.getOutputStream());
sw.out.write(command);
} catch (IOException e) {
e.printStackTrace();
}
}
FXML
<Button id="leftButton" fx:id="leftButton" onMousePressed="#onPress" onMouseReleased="#onRelease"/>
每单击一次,该操作就会再次发生。例如,第一次点击“你好”,第二次点击“你好”
虽然,我不确定,但您似乎正在写入同一个流,因此添加了数据
当我第一次打开应用程序时,我必须在任何事情发生之前按下按钮两次
在方法leftButton()
中添加事件过滤器,该方法在按钮的操作中调用。因此,当你第一次点击你的按钮时,它没有附加任何事件过滤器,因此什么也没有发生
要克服这个问题,您可以将代码移动到initialize()
,或者使用单独的方法放置这些代码,然后使用FXML调用它们
按下按钮
public void onPress() {
try {
System.out.println("Left Button pressed, String: " + leftString);
byte[] command = { (byte) startTx, address, byteOne, panLeft, speedNormal, 0x00, endTx, 0x2B };
TwoWaySerialComm.SerialWriter sw = new TwoWaySerialComm.SerialWriter(
twoWaySerCom.serialPort.getOutputStream());
sw.out.write(command);
} catch (IOException e) {
e.printStackTrace();
}
}
发布时
public void onRelease() {
try {
System.out.println("Left Button released, String: " + stopString);
byte[] command = {(byte) startTx, address, byteOne, 0x00, 0x00, 0x00, (byte) endTx, 0x0F};
TwoWaySerialComm.SerialWriter sw = new TwoWaySerialComm.SerialWriter(twoWaySerCom.serialPort.getOutputStream());
sw.out.write(command);
} catch (IOException e) {
e.printStackTrace();
}
}
FXML
<Button id="leftButton" fx:id="leftButton" onMousePressed="#onPress" onMouseReleased="#onRelease"/>
每单击一次,该操作就会再次发生。例如,第一次点击“你好”,第二次点击“你好”
虽然我不确定,但您似乎正在写入同一个流,因此添加了数据。如果您确实需要事件过滤器,则需要将leftButton()
方法中的代码移动到初始化
方法。否则,该方法将在每次调用时添加额外的侦听器
@FXML
private initialize() {
leftButton.addEventFilter(MouseEvent.MOUSE_PRESSED,
...
);
leftButton.addEventFilter(MouseEvent.MOUSE_RELEASED,
...
);
}
如果事件处理程序足够,请创建2个处理程序并使用fxml注册它们:
@FXML
private void leftButtonPressed() {
...
}
@FXML
private void leftButtonReleased() {
...
}
如果确实需要事件过滤器,则需要将leftButton()
方法中的代码移动到初始化
方法。否则,该方法将在每次调用时添加额外的侦听器
@FXML
private initialize() {
leftButton.addEventFilter(MouseEvent.MOUSE_PRESSED,
...
);
leftButton.addEventFilter(MouseEvent.MOUSE_RELEASED,
...
);
}
如果事件处理程序足够,请创建2个处理程序并使用fxml注册它们:
@FXML
private void leftButtonPressed() {
...
}
@FXML
private void leftButtonReleased() {
...
}
问题不在您显示的代码中。考虑构造一个小的、可运行的示例来演示意外的行为。我认为问题是您的Enter过滤器在代码> > LeopTutnon()/<代码>中。何时调用此方法?每次按下或释放按钮时都会调用此方法<代码>问题不在显示的代码中。考虑构造一个小的、可运行的示例来演示意外的行为。我认为问题是您的Enter过滤器在代码> > LeopTutnon()/<代码>中。何时调用此方法?每次按下或释放按钮时都会调用此方法<代码>