【发布时间】:2016-04-15 14:43:13
【问题描述】:
我有一个带有复杂场景的 javafx8 应用程序。它由一个拆分窗格组成,在顶部,我有一个具有多个级别(子级)的堆栈窗格。最后面是一个只需要在调整大小时重绘的背景,另一个是网格线,纬度/经度。另一个用于绘图,最后,另一个带有滑动复合控件。当复合(按钮、标签、组合框和文本字段,在堆栈帧的最后一个子项中,它正确拦截并作用于鼠标和键盘事件。但是,在它上面没有事件被传播到下面的子窗格。我切换了顺序,现在按钮和轴控件工作,但现在没有任何东西传播到它下面的复合控件。按钮无法看到鼠标动作。
这似乎与Stackpane - MouseClick to be listened by both its children 相似,但并不清楚是否有正确答案。
我通过一个非常简单的测试简化了我的场景,可以遵循的源代码。在这个我有一个 StackPane 有两个孩子,都是 VBox,一个如果左上对齐,另一个右上对齐。只有第二个按钮,右上角响应被按下。
为什么。我的应用程序有多个窗格的原因是性能。这两种绘图动作都很昂贵且频率很高,我不想经常重绘静态或接近静态的布局。
fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<StackPane fx:id="containerPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="mouseeventdemo.MouseEventDemoController">
<children>
<VBox fx:id="container1" prefHeight="200.0" prefWidth="100.0">
<children>
<Button fx:id="leftButton" mnemonicParsing="false" onAction="#leftButtonDown" text="left button" />
</children>
</VBox>
<VBox fx:id="container2" alignment="TOP_RIGHT" prefHeight="200.0" prefWidth="100.0">
<children>
<Button fx:id="rightButton" mnemonicParsing="false" onAction="#rightButtonDown" text="Right Button" />
</children>
</VBox>
</children>
</StackPane>
控制器:
package mouseeventdemo;
/**
* Sample Skeleton for 'FXMLDocument.fxml' Controller Class
*/
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
public class MouseEventDemoController {
@FXML // ResourceBundle that was given to the FXMLLoader
private ResourceBundle resources;
@FXML // URL location of the FXML file that was given to the FXMLLoader
private URL location;
@FXML // fx:id="containerPane"
private StackPane containerPane; // Value injected by FXMLLoader
@FXML // fx:id="container1"
private VBox container1; // Value injected by FXMLLoader
@FXML // fx:id="leftButton"
private Button leftButton; // Value injected by FXMLLoader
@FXML // fx:id="container2"
private VBox container2; // Value injected by FXMLLoader
@FXML // fx:id="rightButton"
private Button rightButton; // Value injected by FXMLLoader
@FXML
void leftButtonDown(ActionEvent event) {
System.out.println("leftButtonPressed");
}
@FXML
void rightButtonDown(ActionEvent event) {
System.out.println("rightButtonPressed");
}
@FXML // This method is called by the FXMLLoader when initialization is complete
void initialize() {
assert containerPane != null : "fx:id=\"containerPane\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
assert container1 != null : "fx:id=\"container1\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
assert leftButton != null : "fx:id=\"leftButton\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
assert container2 != null : "fx:id=\"container2\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
assert rightButton != null : "fx:id=\"rightButton\" was not injected: check your FXML file 'FXMLDocument.fxml'.";
}
}
主要
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package mouseeventdemo;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author walt
*/
public class MouseEventDemo extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
在我的应用程序中,最后面的窗格是背景画布。它需要绘制一次,并且在调整窗口大小时重新绘制。
下一个是具有纬度/经度网格的画布。只有当窗口大小发生变化或垂直或水平比例发生变化时才需要重新绘制。
下面是主要的绘图画布
<vbox>
<hbox>
drawing canvas
vertical scale
</hbox>
horizontal scale
</vbox>
下一个带有一个或多个旗杆,其中垂直杆是像分划板一样的计算尺。这些需要是可拖动的。如果这一层在顶部,它会完美滑动,但两个秤无法聚焦。如果顶部的两个窗格颠倒过来,天平工作得很好,但旗杆对象永远不会收到事件。
两个按钮的例子与我看到的非常简单。
谢谢!
【问题讨论】:
标签: javafx focus mouseevent