簡體   English   中英

如何在javafx2中的TextField上的focusOut上打印一些東西

[英]How do i print something on focusOut on TextField in javafx2

我有一個TextField,並希望對textfiels焦點執行一些操作

TextField textField = new TextField();

我該如何打印

System.ou.prinln("Focus Out");

從文本區域焦點出來時。

JavaFX 2.2 TextField有一個從Node類繼承的public focusedProperty ,只需添加一個ChangeListener並實現更改后的方法。

public class App extends Application {

    @Override
    public void start(Stage primaryStage) {
        TextField txt1= new TextField("text 1");
        TextField txt2= new TextField("text 2 ");
        Button btn= new Button("hello");

        StackPane root = new StackPane();
        VBox vBox= VBoxBuilder.create()
                .children(txt1, btn, txt2)
                .build();

        txt1.focusedProperty().addListener(new ChangeListener<Boolean>() {

            @Override
            public void changed(ObservableValue<? extends Boolean> ov, Boolean t, Boolean t1) {
                if (t1) {
                    System.ou.prinln("Focus In");
                } else {
                    System.ou.prinln("Focus Out");
                }

            }
        });

        root.getChildren().add(vBox);       
        Scene scene = new Scene(root, 500, 400);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * The main() method is ignored in correctly deployed JavaFX application.
     * main() serves only as fallback in case the application can not be
     * launched through deployment artifacts, e.g., in IDEs with limited FX
     * support. NetBeans ignores main().
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM