ITI0011RUS:javafx ex

Allikas: Kursused
Mine navigeerimisribale Mine otsikasti

Вернуться на страницу предмета

JavaFXIntro.java: <source lang="java"> import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.StackPane; import javafx.stage.Stage;

/**

* @author aleksandr.lenin
*
*/

public class JavaFXIntro extends Application {

/** * @param args */ public static void main(String[] args) { launch(args); }

@Override public void start(Stage primaryStage) throws Exception { Scene sc = new Scene(new StackPane(),800,600); primaryStage.setScene(sc); primaryStage.show(); }

} </source>


HelloWorld.java: <source lang="java"> import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.StackPane; import javafx.stage.Stage;

public class HelloWorld extends Application {

   public static void main(String[] args) {
       launch(args);
   }

   @Override
   public void start(Stage primaryStage) {
       primaryStage.setTitle("Hello World!");
       StackPane root = new StackPane();
       Scene scene = new Scene(root, 300, 250);
       TextField text = new TextField();
       text.setText("My awesome contribution =)");
       Label label = new Label();
       label.setText("Foo");
       
       Button btn = new Button();
       btn.setText("Say 'Hello World'");
       /*
       btn.setOnAction(
       	new EventHandler<ActionEvent>() {

@Override public void handle(ActionEvent event) { System.out.println("Hello World!"); }

       	}
       );
       */
       btn.setOnAction(new MyAwesomeEventHandler(scene));
       StackPane.setAlignment(text, Pos.TOP_CENTER);
       StackPane.setAlignment(label, Pos.BOTTOM_CENTER);
       root.getChildren().add(btn);
       root.getChildren().add(text);
       root.getChildren().add(label);
       primaryStage.setScene(scene);
       primaryStage.show();
   }

} </source>


MyAwesomeEventHandler.java: <source lang="java">

import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.event.Event; import javafx.event.EventHandler; import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.StackPane;

/**

* @author aleksandr.lenin
*
*/

public class MyAwesomeEventHandler implements EventHandler<ActionEvent> {

private Scene scene = null;

public MyAwesomeEventHandler(Scene scene) { this.scene = scene; }

@Override public void handle(ActionEvent event) { StackPane root = (StackPane) this.scene.getRoot(); ObservableList<Node> children = root.getChildren();

Label label = null; TextField text = null; for(Node n : children) { if(n instanceof Label) { label = (Label) n; } else if(n instanceof TextField) { text = (TextField) n; } }

label.setText(text.getText()); }

} </source>