I have written a code using VBox as layout. I want the button to appear at the top row, and then draw 2 horizontal lines which should be at y=200 and 300 in the 400x400 scene. But the output shows the lines at different co-ordinates I give it.
I understand this is because of the Layout I am defining. My question is this:
1) Can I somehow draw lines at true co-ordinates keeping the same layout?
2) If not, which javafx layout would be best for this operation?
3) Assuming there is a MenuBar instead of that button, then which layout would be most suitable?
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);
}
}