Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

I eksemplet under har vi lagt inn et tekstfelt i topp-regionen og reagerer på return ved å kopiere teksten i tekstfeltet inn i Text-objektet i midt-regionen.

Code Block
public class BorderPaneShapesTextFieldApplication extends Application {
    private Text centerText;
    private TextField textField;
    
    @Override
    public void start(Stage stage) throws Exception {
        
        BorderPane root = new BorderPane(); // Root of the scene graph
        
        textField = new TextField("center");
        textField.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent arg0) {
                centerText.setText(textField.getText());
            }
        });
        centerText = new Text(180, 180, textField.getText()); // x, y, text
        
        root.setTop(textField);
        root.setBottom(new Text("bottom"));
        root.setLeft(new Text("left"));
        root.setRight(new Text("right"));
        Pane shapesPane = new Pane();
        shapesPane.setPrefSize(300, 300);
        shapesPane.getChildren().addAll(
               Line line = new Line(10, 10, 100, 100),; // x1, y1, x2, y2
        line.getStrokeDashArray().setAll(10.0d, 10.0d); // dashes
        Rectangle rect = new Rectangle(150, 10, 30, 40),; // x, y, w, h
        rect.setFill(Color.BLUE);
        Ellipse ell = new Ellipse(40, 180, 40, 30),; // cx, cy, rx, ry
        ell.setStroke(Color.RED);
        ell.setStrokeWidth(5);
        ell.setFill(Color.GREEN);
        centerText = new Text(180, 180, "center");
        List<String> fonts = Font.getFamilies();
        centerText.setFont(new Font(fonts.get((int) (Math.random() * fonts.size())), 32));
    centerText
    shapesPane.getChildren().addAll(line, rect, ell,  centerText);
        root.setCenter(shapesPane);
        
        Scene scene = new Scene(root, 500, 500);
    
        stage.setScene(scene);
        stage.setTitle("BorderPaneApplication");
        stage.show();
    }
    public static void main(String[] args) {
        launch(BorderPaneShapesTextFieldApplication.class, args);
    }
}