text – 如何在javafx 2.2中创建可编辑标签

text – 如何在javafx 2.2中创建可编辑标签,第1张

概述我希望在我正在编写的窗格上的任意位置创建一个可编辑标签.我的印象是TextField或TextArea对象是我可以用来实现该功能的.显然还有更多,因为我不知道在创建对象时如何定位对象.我在“混沌 Java”网站上找到了一个例子,但我需要做更多的工作来了解那里发生了什么. http://chaoticjava.com/posts/another-javafx-example-the-editabl 我希望在我正在编写的窗格上的任意位置创建一个可编辑的标签.我的印象是TextFIEld或TextArea对象是我可以用来实现该功能的.显然还有更多,因为我不知道在创建对象时如何定位对象.我在“混沌 Java”网站上找到了一个例子,但我需要做更多的工作来了解那里发生了什么. http://chaoticjava.com/posts/another-javafx-example-the-editable-label/

我正在寻找这个小组的更多意见.

(没有错误,因为我没有写任何代码.)

解决方法 我对如何实现这一点感到好奇,所以我试了一下.这就是我提出的.

使用的方法与James在评论中提出的方法非常相似:

I would start with a Pane,. . .,TextFIElds to represent text while being edited. Register mouse Listeners with the Pane and Text objects,and use the layoutX and layoutY propertIEs to position things . . . just to use text fIElds,and to use CSS to make them look like labels when not focused and text fIElds when focused.

唯一非常棘手的部分是确定如何正确调整文本字段的大小,因为文本字段中的文本不会通过公共API公开,以允许您监听它的布局边界.您也许可以使用CSS查找函数来获取随附的Text,但我选择使用私有sun FontMetrics API(将来可能会弃用),以获取文本的大小.在Java 9的未来,您应该能够在不使用私有API的情况下执行任务.

该解决方案不会尝试做任何棘手的事情,如处理多格式或多行文本,它只是可以放置在场景上的几个单词的简短单行注释.

TextCreator.java

// ## CAUTION: beware the com.sun imports...import com.sun.javafx.tk.FontMetrics;import com.sun.javafx.tk.Toolkit;import javafx.application.Application;import javafx.application.Platform;import javafx.scene.Cursor;import javafx.scene.Scene;import javafx.scene.control.TextFIEld;import javafx.scene.image.Image;import javafx.scene.image.ImageVIEw;import javafx.scene.layout.Pane;import javafx.scene.layout.StackPane;import javafx.stage.Stage;/** * displays a map of the lonely mountain upon which draggable,editable labels can be overlaID. */public class TextCreator extends Application {    private static final String MAP_IMAGE_LOC =            "http://images.wikia.com/lotr/images/archive/f/f6/20130209175313!F27c_thorins_map_from_the_hobbit.jpg";    public static voID main(String[] args) throws Exception {        launch(args);    }    @OverrIDe    public voID start(final Stage stage) throws Exception {        Pane pane = new Pane();        pane.setonMouseClicked(event -> {            if (event.getTarget() == pane) {                pane.getChildren().add(                        new EditableDraggableText(event.getX(),event.getY())                );            }        });        EditableDraggableText cssstyled =                 new EditableDraggableText(439,253,"Style them with CSS");        cssstyled.getStyleClass().add("highlighted");        pane.getChildren().addAll(                new EditableDraggableText(330,101,"Click to add a label"),new EditableDraggableText(318,225,"You can edit your labels"),cssstyled,new EditableDraggableText(336,307,"And drag them"),new EditableDraggableText(309,346,"Around The Lonely Mountain")        );        StackPane layout = new StackPane(            new ImageVIEw(                    new Image(                            MAP_IMAGE_LOC                    )            ),pane        );        Scene scene = new Scene(layout);        scene.getStylesheets().add(getClass().getResource(            "editable-text.CSS"        ).toExternalForm());        stage.setScene(scene);        stage.setResizable(false);        stage.show();    }    /**     * A text fIEld which has no special decorations like background,border or focus ring.     *   i.e. the EditableText just looks like a vanilla Text node or a Label node.     */    class EditableText extends TextFIEld {        // The right margin allows a little bit of space        // to the right of the text for the editor caret.        private final double RIGHT_margin = 5;        EditableText(double x,double y) {            relocate(x,y);            getStyleClass().add("editable-text");            //** CAUTION: this uses a non-public API (FontMetrics) to calculate the fIEld size            //            the non-public API may be removed in a future JavaFX version.            // see: https://javafx-jira.kenai.com/browse/RT-8060            //      Need Font/text measurement API            FontMetrics metrics = Toolkit.getToolkit().getFontLoader().getFontMetrics(getFont());            setPrefWIDth(RIGHT_margin);            textproperty().addListener((observable,oldTextString,newTextString) ->                setPrefWIDth(metrics.computeStringWIDth(newTextString) + RIGHT_margin)            );            Platform.runLater(this::requestFocus);        }    }    /**     * An EditableText (a text fIEld which looks like a label),which can be dragged around     * the screen to reposition it.     */    class EditableDraggableText extends StackPane {        private final double padding = 5;        private EditableText text = new EditableText(padding,padding);        EditableDraggableText(double x,double y) {            relocate(x - padding,y - padding);            getChildren().add(text);            getStyleClass().add("editable-draggable-text");            // if the text is empty when we lose focus,// the node has no purpose anymore            // just remove it from the scene.            text.focusedproperty().addListener((observable,hadFocus,hasFocus) -> {                if (!hasFocus && getParent() != null && getParent() instanceof Pane &&                    (text.getText() == null || text.getText().trim().isEmpty())) {                    ((Pane) getParent()).getChildren().remove(this);                }            });            enableDrag();        }        public EditableDraggableText(int x,int y,String text) {            this(x,y);            this.text.setText(text);        }        // make a node movable by dragging it around with the mouse.        private voID enableDrag() {            final Delta dragDelta = new Delta();            seton@R_301_3491@ed(mouseEvent -> {                this.toFront();                // record a delta distance for the drag and drop operation.                dragDelta.x = mouseEvent.getX();                dragDelta.y = mouseEvent.getY();                getScene().setCursor(Cursor.MOVE);            });            setonMouseReleased(mouseEvent -> getScene().setCursor(Cursor.HAND));            setonMouseDragged(mouseEvent -> {                double newX = getLayoutX() + mouseEvent.getX() - dragDelta.x;                if (newX > 0 && newX < getScene().getWIDth()) {                    setLayoutX(newX);                }                double newY = getLayoutY() + mouseEvent.getY() - dragDelta.y;                if (newY > 0 && newY < getScene().getHeight()) {                    setLayoutY(newY);                }            });            setonMouseEntered(mouseEvent -> {                if (!mouseEvent.isPrimarybuttonDown()) {                    getScene().setCursor(Cursor.HAND);                }            });            setonMouseExited(mouseEvent -> {                if (!mouseEvent.isPrimarybuttonDown()) {                    getScene().setCursor(Cursor.DEFAulT);                }            });        }        // records relative x and y co-ordinates.        private class Delta {            double x,y;        }    }    }

编辑-text.CSS

.editable-text {    -fx-background-color: transparent;    -fx-background-insets: 0;    -fx-background-radius: 0;    -fx-padding: 0;}.editable-draggable-text:hover .editable-text {    -fx-background-color: yellow;}.editable-draggable-text {    -fx-padding: 5;    -fx-background-color: rgba(152,251,152,0.2); // translucent palegreen}.editable-draggable-text:hover {    -fx-background-color: orange;}.highlighted {    -fx-background-color: rgba(255,182,93,0.3);  // translucent mistyrose    -fx-border-style: dashed;    -fx-border-color: firebrick;}

如果您有时间,可以清理样本实施并将其捐赠给ControlsFX项目.

总结

以上是内存溢出为你收集整理的text – 如何在javafx 2.2中创建可编辑标签全部内容,希望文章能够帮你解决text – 如何在javafx 2.2中创建可编辑标签所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1074617.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存