在JavaFX GUI中显示TextField / TextArea插入符号

时间:2015-02-12 15:37:55

标签: java user-interface javafx

我想知道在使用TextField或TextArea时如何使插入符号可见和/或闪烁。我在GUI中创建的那些工作,因为字母在键入时出现,但没有可见的插入符号。

我查看了TextField文档但没有关于是否可见。我希望找到“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);
    }
}

非常感谢,

1 个答案:

答案 0 :(得分:0)

此问题仅发生在Mac而非Windows(未经测试的Linux)上。修复是降级到符合JavaFX 2.2的最大化窗口,因为JavaFX 8中的setMaximized()也不兼容Mac。

使用找到的here代码修改start方法:

@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();
}

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