栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

侦听器JavaFX

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

侦听器JavaFX

您应该签出Ensemble示例。这是关键的侦听器代码。

import javafx.application.Application;import javafx.scene.Group;import javafx.scene.Scene;import javafx.stage.Stage;import javafx.collections.FXCollections;import javafx.collections.ListChangeListener;import javafx.event.EventHandler;import javafx.scene.Group;import javafx.scene.Node;import javafx.scene.control.ListView;import javafx.scene.control.TextField;import javafx.scene.effect.DropShadow;import javafx.scene.effect.PerspectiveTransform;import javafx.scene.input.KeyEvent;import javafx.scene.layout.VBox;import javafx.scene.paint.Color;public class KeyEventsSample extends Application {    private void init(Stage primaryStage) {        Group root = new Group();        primaryStage.setScene(new Scene(root));        //create a console for logging key events        final ListView<String> console = new ListView<String>(FXCollections.<String>observableArrayList());        // listen on the console items and remove old ones when we get over 20 items in the list        console.getItems().addListener(new ListChangeListener<String>() { @Override public void onChanged(Change<? extends String> change) {     while (change.next()) {         if (change.getList().size() > 20) change.getList().remove(0);     } }        });        // create text box for typing in        final TextField textBox = new TextField();        textBox.setpromptText("Write here");        textBox.setStyle("-fx-font-size: 34;");        //add a key listeners        textBox.setonKeyPressed(new EventHandler<KeyEvent>() { public void handle(KeyEvent ke) {     console.getItems().add("Key Pressed: " + ke.getText()); }        });        textBox.setonKeyReleased(new EventHandler<KeyEvent>() { public void handle(KeyEvent ke) {     console.getItems().add("Key Released: " + ke.getText()); }        });        textBox.setonKeyTyped(new EventHandler<KeyEvent>() { public void handle(KeyEvent ke) {     String text = "Key Typed: " + ke.getCharacter();     if (ke.isAltDown()) {         text += " , alt down";     }     if (ke.isControlDown()) {         text += " , ctrl down";     }     if (ke.ismetaDown()) {         text += " , meta down";     }     if (ke.isShiftDown()) {         text += " , shift down";     }     console.getItems().add(text); }        });        VBox vb = new VBox(10);        vb.getChildren().addAll(textBox, console);        root.getChildren().add(vb);    }    @Override public void start(Stage primaryStage) throws Exception {        init(primaryStage);        primaryStage.show();    }    public static void main(String[] args) { launch(args); }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/570431.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号