我已经写了一个使用VBox作为布局的代码。我希望按钮出现在顶行,然后绘制两条水平线,这应该是在y=200和300在400x400的场景。但输出显示了我给出的不同坐标下的线条。
我知道这是因为我正在定义的布局。我的问题是:
1)我能以某种方式在保持相同布局的真正坐标上绘制线条吗?
2)如果不是,哪种javafx布局最适合此操作?
3)假设有一个MenuBar而不是那个按钮,那么哪种布局最合适?
package practise;
import java.util.Random;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
public class Practise extends Application {
private int c = 0;
@Override
public void start(Stage primaryStage) {
Button btn = new Button();
VBox root = new VBox();
Group group = new Group();
final Line l1 = new Line(0,200,400,200);
final Line l2 = new Line(0,300,400,300);
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
}
);
l1.setStroke(Color.YELLOW);
l1.setStrokeWidth(2);
l1.setLayoutX(0);
l1.setLayoutY(0);
l2.setStroke(Color.YELLOW);
l2.setStrokeWidth(2);
l2.setLayoutX(0);
l2.setLayoutY(0);
group.getChildren().add(l1);
group.getChildren().add(l2);
root.getChildren().add(btn);
root.getChildren().add(group);
Scene scene = new Scene(root, 400, 400, Color.WHITE);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}https://stackoverflow.com/questions/38340556
复制相似问题