[英]JavaFX: Line goes automatically to center of the scene
I want to draw Line
s in JavaFX but the program seems not to listen to the coordinates and directly draws it in the middle (ie center) with the same length.我想在JavaFX中绘制Line
,但是程序似乎没有听坐标,直接在中间(即中心)绘制,长度相同。
Code:代码:
package application;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage stage) {
Line line = new Line(200,200,250,250);
StackPane root = new StackPane();
root.setPadding(new Insets(15));
final Scene scene = new Scene(root, 400, 250);
scene.setFill(null);
root.getChildren().addAll(line);
stage.setTitle("JavaFX Line");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Result: Line in Center结果:中心线
A StackPane is a layout pane which means that it will place any node according to its own rules which may come as a surprise as in your case. StackPane 是一个布局窗格,这意味着它将根据自己的规则放置任何节点,这可能会像您的情况一样令人惊讶。 Instead you could just use a Pane which will place your line at the place you have specified.相反,您可以只使用一个 Pane 将您的行放在您指定的位置。 It may be a good idea to place this Pane inside your StackPane so that it fills the whole space.将此 Pane 放在 StackPane 中可能是个好主意,这样它就会填满整个空间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.