提问人:Mostafa Ayaz 提问时间:11/17/2023 最后编辑:Mostafa Ayaz 更新时间:11/17/2023 访问量:40
stage.show() 命令更改 Bounds [duplicate] 的值
stage.show() command changes value of Bounds [duplicate]
问:
我正在用 JavaFX 编写一个应用程序。我希望能够在运行应用程序期间向 a 添加 s 并与他们的孩子一起工作。Chart
Scene
Bounds
.chart-plot-background
请考虑以下两个代码片段及其输出:
代码 1
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Region;
import javafx.stage.Stage;
public class Test8 extends Application {
@Override
public void start(Stage stage) {
AnchorPane anchorPane=new AnchorPane();
Scene scene=new Scene(anchorPane,600,400);
stage.setScene(scene);
stage.show();
LineChart<Number,Number> lineChart=new LineChart<>(new NumberAxis(),new NumberAxis());
Region region= (Region) lineChart.lookup(".chart-plot-background");
anchorPane.getChildren().add(lineChart);
System.out.println(region.getBoundsInParent());
}
public static void main(String[] args) {
launch();
}
}
输出:BoundingBox [minX:0.0, minY:0.0, minZ:0.0, width:0.0, height:0.0, depth:0.0, maxX:0.0, maxY:0.0, maxZ:0.0]
代码 2
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Region;
import javafx.stage.Stage;
public class Test8 extends Application {
@Override
public void start(Stage stage) {
AnchorPane anchorPane=new AnchorPane();
Scene scene=new Scene(anchorPane,600,400);
stage.setScene(scene);
LineChart<Number,Number> lineChart=new LineChart<>(new NumberAxis(),new NumberAxis());
Region region= (Region) lineChart.lookup(".chart-plot-background");
anchorPane.getChildren().add(lineChart);
stage.show();
System.out.println(region.getBoundsInParent());
}
public static void main(String[] args) {
launch();
}
}
输出:BoundingBox [minX:38.0, minY:10.0, minZ:0.0, width:443.0, height:346.0, depth:0.0, maxX:481.0, maxY:356.0, maxZ:0.0]
这两个代码段之间的唯一区别是命令的位置。stage.show()
问题
我应该如何修复代码 1,以便在执行后将子命令添加到时输出有效结果?anchorPane
stage.show()
region.getBoundsInParent()
答:
1赞
SedJ601
11/17/2023
#1
我不知道我是否真的正确地理解了这个问题,但如果我真的理解了,我会使用或类似的东西。setOnMouseEntered
例:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Region;
import javafx.stage.Stage;
public class App extends Application {
@Override
public void start(Stage stage) {
AnchorPane anchorPane=new AnchorPane();
Scene scene=new Scene(anchorPane,600,400);
stage.setScene(scene);
LineChart<Number,Number> lineChart=new LineChart<>(new NumberAxis(),new NumberAxis());
anchorPane.getChildren().add(lineChart);
lineChart.setOnMouseEntered((event) -> {
Region region= (Region) lineChart.lookup(".chart-plot-background");
System.out.println(region.getBoundsInParent());
});
stage.show();
}
public static void main(String[] args) {
launch();
}
}
评论
0赞
Mostafa Ayaz
11/17/2023
这是一个有趣的方法和观点。在我的应用程序中,鼠标仍然可能永远不会进入折线图,并且 setOnMouseEntered 方法永远不会运行。
2赞
jewelsea
11/17/2023
@MostafaAyaz您需要更好地提供有关原始问题中的约束和操作和要求的信息,以便您能够获得相关答案。
评论
show()
LineChart
layoutPlotChildren()
getDisplayPosition()