likes
comments
collection

前端人写后端javafx两个月的日常记录!

作者站长头像
站长
· 阅读数 9

“我报名参加金石计划1期挑战——瓜分10万奖池,这是我的第1篇文章,点击查看活动详情

JavaFX 是一个用于构建富 Internet 应用程序的 Java 库。使用该库编写的应用程序可以在多个平台上一致运行。使用 JavaFX 开发的应用程序可以在各种设备上运行,例如台式机、手机、电视、平板电脑等。

1.获取节点位置

这取决于您所说的绝对"是什么意思.节点有一个坐标系,其父级有一个坐标系,父级有一个坐标系,依此类推,最终有一个用于 Scene 的坐标系和一个用于屏幕的坐标系(这可能是一个物理显示设备的集合).

你可能想要相对于 Scene 的坐标,在这种情况下你可以做

Bounds boundsInScene = node.localToScene(node.getBoundsInLocal()); 

或相对于屏幕的坐标:

Bounds boundsInScreen = node.localToScreen(node.getBoundsInLocal()); 

无论哪种情况,结果都是 Bounds 对象有 getMinX(), getMinY(), getMaxX(), getMaxY()code>、getWidth() 和 getHeight() 方法.

2.ListView监听

    // 待岗容器列表
    ObservableList<Node> oriList = FXCollections.observableArrayList();

    // listview设置数据
    oriBox.setItems(oriList);

    // 监听listview点击
    private void listViewClick(ListView box, ObservableList<Node> list) {
        box.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(final MouseEvent mouseEvent) {
                node = (Pane) list.get(box.getSelectionModel().getSelectedIndex());
                Map params = (Map) node.getProperties().get("params");
                String name = (String) params.get("name");
                paneNode = generateRectangleNode(name);
                root.getChildren().add(paneNode);
                // 获取节点的位置
                Bounds boundsInScene = node.localToScene(node.getBoundsInLocal());
                paneNode.relocate(mouseEvent.getX()+boundsInScene.getMinX(), mouseEvent.getY()+boundsInScene.getMinY());
                box.getSelectionModel().clearSelection();
            }
        });
    }

3.键盘监听事件获取焦点

// 键盘事件
    private void initKeyPress() {
        settingDialog.setOnDialogOpened(event -> {
            settingDialog.requestFocus();
        });

        settingDialog.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {
                switch (event.getCode()) {
                    case F3:
                        System.out.println("保存");
                        break;
                    case F4:
                        closeDialog();
                        break;
                    case F5:
                        System.out.println("同步");
                        break;
                    case F6:
                        System.out.println("应用");
                        break;
                }
            }
        });
    }

4.让组件隐藏且不占用空间

pwdStrengthPane.setVisible(false);
// 设置其托管状态
pwdStrengthPane.setManaged(false);

5.进度条动态变化

    public void init(Stage stage) {
        Timeline task = new Timeline(
                new KeyFrame(
                        Duration.ZERO,
                        new KeyValue(initProgress.progressProperty(), 0)),
                new KeyFrame(
                        Duration.seconds(2),
                        new KeyValue(initProgress.progressProperty(), 1)));
        task.setCycleCount(Timeline.INDEFINITE);
        task.play();
    }

6.动态双向绑定数据

// model
package com.xmcares.scims.desktop.gui.models.cert;

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class CertModel {
    private final StringProperty dpsrCardName=new SimpleStringProperty("");

    public StringProperty dpsrCardNameProperty() {
        return dpsrCardName ;
    }
    public final void setDpsrCardName(String text) {
        dpsrCardNameProperty().set(text);
    }
    public final String getDpsrCardName() {
        return dpsrCardNameProperty().get();
    }
}

// set
certmodel.setDpsrCardName((String) params.get("dpsrCardName"));

// bindData监听数据变化
certModel.dpsrCardNameProperty().addListener(((observable, oldValue, newValue) -> {
            dpsrCardName.setText(newValue);
            openDialog();
}));

7.全局注册数据 (如cookie)

// 设置data
ApplicationContext.getInstance().register("departurePassenger", departurePassenger);

// 全局获取data
JSONObject departurePassenger = (JSONObject) ApplicationContext.getInstance().getRegisteredObject("departurePassenger");

8.键盘大小写和中文限制

     // 大写锁定和限制输入中文
        usernameField.setTextFormatter(new TextFormatter<>((change) -> {
            change.setText(change.getText().toUpperCase().replaceAll("[\u4e00-\u9fa5]+",""));
            return change;
        }));

9.combox修改列表背景和文字颜色

.jfx-combo-box .list-cell:hover {
    -fx-background-color: rgb(245,245,245);
    -fx-text-fill: #000;
}

10.combox如何实现过滤查询

/**
 * @Description combox 过滤
 * @Author lius
 * @Date 2022/10/24 10:37
 */
package com.xmcares.scims.desktop.gui.components.comboBox;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.control.ComboBox;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;

public class AutoCompleteComboBoxListener<T> implements EventHandler<KeyEvent> {

    private ComboBox<T> comboBox;
    private ObservableList<T> data;
    private boolean moveCaretToPos = false;
    private int caretPos;

    public AutoCompleteComboBoxListener(final ComboBox<T> comboBox) {
        this.comboBox = comboBox;
        data = comboBox.getItems();
        this.comboBox.setEditable(true);
        this.comboBox.setOnKeyReleased((EventHandler<? super KeyEvent>) AutoCompleteComboBoxListener.this);
    }

    @Override
    public void handle(KeyEvent event) {
        if (event.getCode() == KeyCode.UP) {
            caretPos = -1;
            moveCaret(comboBox.getEditor().getText().length());
            return;
        } else if (event.getCode() == KeyCode.DOWN) {
            if (!comboBox.isShowing())
                comboBox.show();

            caretPos = -1;
            moveCaret(comboBox.getEditor().getText().length());
            return;
        }

        if (event.getCode() == KeyCode.RIGHT || event.getCode() == KeyCode.LEFT
                || event.isControlDown() || event.getCode() == KeyCode.HOME
                || event.getCode() == KeyCode.END || event.getCode() == KeyCode.TAB) {
            return;
        }

        comboBox.hide();

        if (event.getCode() == KeyCode.BACK_SPACE) {
            moveCaretToPos = true;
            caretPos = comboBox.getEditor().getCaretPosition();
        } else if (event.getCode() == KeyCode.DELETE) {
            moveCaretToPos = true;
            caretPos = comboBox.getEditor().getCaretPosition();
        }


        ObservableList<T> list = FXCollections.observableArrayList();
        for (int i = 0; i < data.size(); i++) {
                if (data.get(i) != null&&data.get(i).toString().toLowerCase().startsWith(
                        AutoCompleteComboBoxListener.this.comboBox
                                .getEditor().getText().toLowerCase())) {
                    list.add(data.get(i));
                }
        }
        String t = comboBox.getEditor().getText();
        comboBox.setItems(list);
        comboBox.getEditor().setText(t);
        if (!moveCaretToPos) {
            caretPos = -1;
        }
        moveCaret(t.length());
        if (!list.isEmpty()) {
            comboBox.show();
        }
    }

    private void moveCaret(int textLength) {
        if (caretPos == -1)
            comboBox.getEditor().positionCaret(textLength);
        else
            comboBox.getEditor().positionCaret(caretPos);
        moveCaretToPos = false;
    }

}



public class JavaFXApplication1 extends Application {

    @Override
    public void start(Stage primaryStage) {
        ComboBox<String> combo = new ComboBox<>();
        ObservableList<String> list = FXCollections.observableArrayList();
        list.add("A");
        list.add("AND");
        list.add("ANDR");
        list.add("ANDRE");
        list.add("B");
        list.add("BP");
        list.add("BPO");
        combo.setItems(list);
        new AutoCompleteComboBoxListener(combo);

        StackPane root = new StackPane();
        root.getChildren().add(combo);

        Scene scene = new Scene(root, 300, 250);

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

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

查阅文档

比较全的文档: www.jc2182.com/javafx/java…

拖拽文档:www.leestorm.com/post/95.htm…

其他:

vimsky.com/examples/de…

jenkov.com/tutorials/j…

fxdocs.github.io/docs/html5/…

www.javaroad.cn/questions/1…