使 TextField/TextArea 插入符号在 JavaFX GUI 中可见

Make TextField/TextArea caret visible in JavaFX GUI

我想知道如何在使用 TextField 或 TextArea 时使插入符号可见 and/or。我在我的 GUI 中创建的那些在键入时出现字母但没有可见的插入符号。

我查看了 TextField 文档,但 none 是关于使其可见或不可见的。我希望找到符合 "setCaretVisible(Boolean);"

的内容

我必须通过 CSS 使其可见吗?如果是这样,欢迎提出任何建议!

请查看我快速整理的用于说明问题的代码:

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCombination;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class Test extends Application {
    public static void main(String[] arguments) { launch(arguments); }

    @Override public void start(Stage stage) {
        Scene scene = new Scene(new Group());
        scene.setRoot(new BuildLayout(stage));

        stage.setTitle("Application Name");
        stage.setScene(scene);
        stage.initStyle(StageStyle.UNDECORATED);
        stage.initStyle(StageStyle.TRANSPARENT);
        stage.setFullScreen(true);
        stage.setFullScreenExitHint("");
        stage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
        stage.show();
    }
}

final class BuildLayout extends BorderPane {
    protected BuildLayout(Stage stage) {
        TabPane tabpane = new TabPane();

        Tab tab = new Tab();
        tab.setGraphic(new Text("Video Browser"));
        tab.setClosable(false);
        tab.setContent(new Input(1));

        tabpane.getTabs().addAll(tab);
        setTop(new Toolbar(stage));
        setCenter(tabpane);
    }
}

final class Input extends VBox {
    Input(int id) {
        HBox hbox = new HBox();
        TextField videoTitle = new TextField("video_title");
        TextArea description = new TextArea( "video_description");
        Button share = new Button("share");
        Button unshare = new Button("unshare");

        setPadding(new Insets(5,10,10,5));
        setAlignment(Pos.TOP_CENTER);

        hbox.getChildren().addAll(videoTitle, new Text("   Creator: " + "   Date: " + "   Views: " + "   "));

        if(true) {
            videoTitle.setEditable(true);
            description.setEditable(true);
            if(true) {
                hbox.getChildren().add(share);
            } else { hbox.getChildren().add(unshare); }
        }
        getChildren().addAll(hbox, description);
    }
}

final class Toolbar extends HBox {
    protected Toolbar(Stage stage) {
        Button close = new Button("close");
        close.setOnAction((ActionEvent e) -> { stage.close(); });

        setAlignment(Pos.CENTER_LEFT);
        getChildren().addAll(close);
    }
}

非常感谢,

此问题只发生在 Mac 而不是 Windows(未经测试 Linux)。解决方法是降级到符合 JavaFX 2.2 的最大化 window,因为 JavaFX 8 中的 setMaximized() 也不 Mac 兼容。

使用找到的一些代码修改启动方法here:

@Override public void start(Stage stage) {
    Scene scene = new Scene(new Group());
    scene.setRoot(new BuildLayout(stage));

    Screen screen = Screen.getPrimary();
    Rectangle2D bounds = screen.getVisualBounds();

    stage.setTitle("Application Name");
    stage.setScene(scene);
    stage.setX(bounds.getMinX());
    stage.setY(bounds.getMinY());
    stage.setWidth(bounds.getWidth());
    stage.setHeight(bounds.getHeight());          
    stage.show();
}

生成带有可见和闪烁插入符号的全屏应用程序。