Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javafx显示虚拟键盘_Java_Javafx_Javafx 8_Virtual Keyboard - Fatal编程技术网

Javafx显示虚拟键盘

Javafx显示虚拟键盘,java,javafx,javafx-8,virtual-keyboard,Java,Javafx,Javafx 8,Virtual Keyboard,我不想像你一样隐藏虚拟键盘 或(如果无法实现上述点) 我想从虚拟键盘中删除/隐藏/禁用下面(突出显示的绿色左下角)的按钮 我正在使用Java8和Javafx 更新1 我已经整合了。隐藏按钮(.hide)已成功隐藏。现在我很难一直显示键盘 问题: 只要有焦点从文本区域出来键盘就会隐藏。所以当用户点击立即转换按钮隐藏键盘。我试图通过使用textarea.requestFocus()将注意力集中在textarea,但键盘闪烁(先隐藏后显示) 我的目标 在任何情况下都不要隐藏键盘,除非程序未终止

我不想像你一样隐藏虚拟键盘

(如果无法实现上述点)

我想从虚拟键盘中删除/隐藏/禁用下面(突出显示的绿色左下角)的按钮

我正在使用Java8和Javafx


更新1
我已经整合了。隐藏按钮(
.hide
)已成功隐藏。现在我很难一直显示键盘

问题:

只要有焦点从
文本区域出来
键盘就会隐藏。所以当用户点击
立即转换按钮隐藏键盘。我试图通过使用
textarea.requestFocus()将注意力集中在
textarea
,但键盘闪烁(先隐藏后显示)

我的目标

在任何情况下都不要隐藏键盘,除非程序未终止

代码:你也可以查看我的代码

package com.binaryname.view;
导入java.util.Iterator;
导入com.sun.javafx.print.PrintHelper;
导入com.sun.javafx.print.Units;
导入com.sun.javafx.scene.control.skin.FXVK;
导入javafx.application.application;
导入javafx.application.Platform;
导入javafx.beans.value.ChangeListener;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.geometry.Bounds;
导入javafx.geometry.Pos;
导入javafx.geometry.Rectangle2D;
导入javafx.print.PageLayout;
导入javafx.print.PageOrientation;
导入javafx.print.Paper;
导入javafx.print.Printer;
导入javafx.print.PrinterJob;
导入javafx.scene.Node;
导入javafx.scene.Parent;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.control.Label;
导入javafx.scene.control.ScrollPane;
导入javafx.scene.control.TextArea;
导入javafx.scene.layout.HBox;
导入javafx.scene.layout.VBox;
导入javafx.scene.paint.Color;
导入javafx.scene.shape.Path;
导入javafx.scene.text.Font;
导入javafx.scene.text.FontWeight;
导入javafx.scene.text.text;
导入javafx.scene.transform.Scale;
导入javafx.stage.PopupWindow;
导入javafx.stage.Screen;
导入javafx.stage.stage;
导入javafx.stage.StageStyle;
导入javafx.stage.Window;
公共类主扩展应用程序{
专用弹出窗口键盘;
私有最终矩形2D visualBounds=Screen.getPrimary().getVisualBounds();
私有最终矩形2D边界=Screen.getPrimary().getBounds();
private final double taskbarHeight=bounds.getHeight()-visualBounds.getHeight();
@凌驾
public void start(Stage primaryStage)引发异常{
primaryStage.setTitle(“二进制名称”);
标签helloLbl=新标签(“你好”);
helloLbl.设置对齐(位置中心);
helloLbl.setFont(Font.Font(“Comic Sans MS”,fontwweight.BOLD,68));
helloLbl.setStyle(“-fx背景色:红色;填充:20px;”);
helloLbl.setTextFill(Color.web(“#ffffff”);
Label myNameLbl=新标签(“我的名字是”);
myNameLbl.设置对齐(位置中心);
myNameLbl.setFont(Font.Font(“Comic Sans MS”,48));
myNameLbl.setStyle(“-fx背景色:红色;填充:20px;”);
myNameLbl.setTextFill(Color.web(“#ffffff”);
TextArea name txtarea=新建TextArea();
nameTxtArea.setWrapText(Boolean.TRUE);
nameTxtArea.getStyleClass().add(“中间文本区域”);
nameTxtArea.setFont(Font.Font(“Comic Sans MS”,28));
nameTxtArea.setStyle(“padding:20px;”);
按钮打印BTN=新按钮(“打印”);
printBtn.setId(“ipad灰色”);
printbn.setDisable(Boolean.TRUE);
Button convertBtn=新建按钮(“立即转换!”);
convertBtn.setId(“ipad灰色”);
convertBtn.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
nameTxtArea.requestFocus();
convertbn.setDisable(Boolean.TRUE);
printbn.setDisable(Boolean.FALSE);
}
});
HBox HBox=新的HBox(100);
hBox.设置校准(位置中心);
hBox.getChildren().addAll(convertBtn、printBtn);
VBox VBox=新的VBox(10);
vBox.SETALIGNERATION(位置上止点);
vBox.getChildren().addAll(helloLbl、myNameLbl、nameTxtArea、hBox);
vBox.setStyle(“-fx背景色:红色;边距:20px;”);
setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
nameTxtArea.requestFocus();
//开始印刷
打印(vBox,nameTxtArea.getText());
convertbn.setDisable(Boolean.FALSE);
printbn.setDisable(Boolean.TRUE);
nameTxtArea.setText(“”);
}
});
场景=新场景(vBox);
scene.getStylesheets().add(Main.class.getResource(“/style.css”).toExternalForm());
初级阶段。场景(场景);
初级阶段。初始风格(阶段风格。未装饰);
初级阶段。场景(场景);
setX(visualBounds.getMinX());
setY(visualBounds.getMinY());
setWidth(visualBounds.getWidth());
setHeight(visualBounds.getHeight());
调整TextArea布局(名称txtArea);
primaryStage.show();
//将键盘附加到场景中的第一个节点:
Node first=scene.getRoot().getChildrenUnmodifiable().get(0);
如果(第一个!=null){
FXVK.init(第一);
FXVK.attach(第一个);
键盘=getPopupWindow();
}
nameTxtArea.focusedProperty().addListener((ob,b,b1)->{
如果(键盘==null){
键盘=getPopupWindow();
}
keyboard.setHideOnEscape(Boolean.FALSE);
键盘.setAutoHide(布尔值.FALSE);
键盘。中心屏幕();
keyboard.requestFocus();
键盘.yProperty().addListener(
package com.binaryname.view;

import java.util.Iterator;

import com.sun.javafx.print.PrintHelper;
import com.sun.javafx.print.Units;
import com.sun.javafx.scene.control.skin.FXVK;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Bounds;
import javafx.geometry.Pos;
import javafx.geometry.Rectangle2D;
import javafx.print.PageLayout;
import javafx.print.PageOrientation;
import javafx.print.Paper;
import javafx.print.Printer;
import javafx.print.PrinterJob;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Path;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.transform.Scale;
import javafx.stage.PopupWindow;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.Window;

public class Main extends Application {

    private PopupWindow keyboard;

    private final Rectangle2D visualBounds = Screen.getPrimary().getVisualBounds();
    private final Rectangle2D bounds = Screen.getPrimary().getBounds();
    private final double taskbarHeight = bounds.getHeight() - visualBounds.getHeight();

    @Override
    public void start(Stage primaryStage) throws Exception {

        primaryStage.setTitle("Binary Name");

        Label helloLbl = new Label("Hello");
        helloLbl.setAlignment(Pos.CENTER);
        helloLbl.setFont(Font.font("Comic Sans MS", FontWeight.BOLD, 68));
        helloLbl.setStyle("-fx-background-color: red;padding: 20px;");
        helloLbl.setTextFill(Color.web("#ffffff"));

        Label myNameLbl = new Label("my name is");
        myNameLbl.setAlignment(Pos.CENTER);
        myNameLbl.setFont(Font.font("Comic Sans MS", 48));
        myNameLbl.setStyle("-fx-background-color: red;padding: 20px;");
        myNameLbl.setTextFill(Color.web("#ffffff"));

        TextArea nameTxtArea = new TextArea();
        nameTxtArea.setWrapText(Boolean.TRUE);
        nameTxtArea.getStyleClass().add("center-text-area");
        nameTxtArea.setFont(Font.font("Comic Sans MS", 28));
        nameTxtArea.setStyle("padding: 20px;");

        Button printBtn = new Button("PRINT");
        printBtn.setId("ipad-grey");
        printBtn.setDisable(Boolean.TRUE);

        Button convertBtn = new Button("Convert Now!");
        convertBtn.setId("ipad-grey");
        convertBtn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                nameTxtArea.requestFocus();
                convertBtn.setDisable(Boolean.TRUE);
                printBtn.setDisable(Boolean.FALSE);
            }
        });

        HBox hBox = new HBox(100);
        hBox.setAlignment(Pos.CENTER);
        hBox.getChildren().addAll(convertBtn, printBtn);

        VBox vBox = new VBox(10);
        vBox.setAlignment(Pos.TOP_CENTER);
        vBox.getChildren().addAll(helloLbl, myNameLbl, nameTxtArea, hBox);
        vBox.setStyle("-fx-background-color: red;margin: 20px;");

        printBtn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                nameTxtArea.requestFocus();
                // Start printing
                print(vBox, nameTxtArea.getText());
                convertBtn.setDisable(Boolean.FALSE);
                printBtn.setDisable(Boolean.TRUE);
                nameTxtArea.setText("");
            }
        });

        Scene scene = new Scene(vBox);
        scene.getStylesheets().add(Main.class.getResource("/style.css").toExternalForm());

        primaryStage.setScene(scene);
        primaryStage.initStyle(StageStyle.UNDECORATED);
        primaryStage.setScene(scene);
        primaryStage.setX(visualBounds.getMinX());
        primaryStage.setY(visualBounds.getMinY());
        primaryStage.setWidth(visualBounds.getWidth());
        primaryStage.setHeight(visualBounds.getHeight());

        adjustTextAreaLayout(nameTxtArea);

        primaryStage.show();

        // attach keyboard to first node on scene:
        Node first = scene.getRoot().getChildrenUnmodifiable().get(0);
        if (first != null) {
            FXVK.init(first);
            FXVK.attach(first);
            keyboard = getPopupWindow();
        }

        nameTxtArea.focusedProperty().addListener((ob, b, b1) -> {
            if (keyboard == null) {
                keyboard = getPopupWindow();
            }

            keyboard.setHideOnEscape(Boolean.FALSE);
            keyboard.setAutoHide(Boolean.FALSE);
            keyboard.centerOnScreen();
            keyboard.requestFocus();

            keyboard.yProperty().addListener(obs -> {

                Platform.runLater(() -> {
                    Double y = bounds.getHeight() - taskbarHeight - keyboard.getY();
                    nameTxtArea.setMaxHeight((bounds.getHeight() - y) * 0.4);
                    nameTxtArea.setMinHeight((bounds.getHeight() - y) * 0.4);
                });
            });

        });     

    }

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

    private void print(Node node1, String text) {
        // Create a printer job for the default printer

        Printer printer = Printer.getDefaultPrinter();
        Paper label = PrintHelper.createPaper("2.5x3.5", 2.5, 3.5, Units.INCH);
        PageLayout pageLayout = printer.createPageLayout(label, PageOrientation.LANDSCAPE, Printer.MarginType.EQUAL);

        PrinterJob job = PrinterJob.createPrinterJob();

        Node node = createFullNode(text);

        double scaleX = pageLayout.getPrintableWidth() / node1.getBoundsInParent().getWidth();
        double scaleY = pageLayout.getPrintableHeight() / node1.getBoundsInParent().getHeight();
        node.getTransforms().add(new Scale(scaleX, scaleY));

        if (job != null) {
            // Print the node
            boolean printed = job.printPage(node);

            if (printed) {
                // End the printer job
                job.endJob();
            } else {
                // Write Error Message
                System.out.println("Printing failed.");
            }
        } else {
            // Write Error Message
            System.out.println("Could not create a printer job.");
        }

        node.getTransforms().remove(node.getTransforms().size() - 1);
    }

    private PopupWindow getPopupWindow() {

        @SuppressWarnings("deprecation") 
        final Iterator<Window> windows = Window.impl_getWindows();

        while (windows.hasNext()) {
            final Window window = windows.next();
            if (window instanceof PopupWindow) {
                if (window.getScene() != null && window.getScene().getRoot() != null) { 
                    Parent root = window.getScene().getRoot();
                    if (root.getChildrenUnmodifiable().size() > 0) {
                        Node popup = root.getChildrenUnmodifiable().get(0);
                        if (popup.lookup(".fxvk") != null) {
                            FXVK vk = (FXVK) popup.lookup(".fxvk");
                            // hide the key:
                            vk.lookup(".hide").setVisible(false);
                            return (PopupWindow) window;
                        }
                    }
                }
                return null;
            }
        }
        return null;
    }

    private Node createFullNode(String text) {

        Label helloLbl = new Label("Hello");
        helloLbl.setAlignment(Pos.CENTER);
        helloLbl.setFont(Font.font("Comic Sans MS", FontWeight.BOLD, 68));
        helloLbl.setStyle("-fx-background-color: red;padding: 20px;");
        helloLbl.setTextFill(Color.web("#ffffff"));

        Label myNameLbl = new Label("my name is");
        myNameLbl.setAlignment(Pos.CENTER);
        myNameLbl.setFont(Font.font("Comic Sans MS", 48));
        myNameLbl.setStyle("-fx-background-color: red;padding: 20px;");
        myNameLbl.setTextFill(Color.web("#ffffff"));

        TextArea nameTxtArea = new TextArea();
        nameTxtArea.setWrapText(Boolean.TRUE);
        nameTxtArea.setFont(Font.font("Comic Sans MS", 28));
        nameTxtArea.setStyle("padding: 20px;");
        nameTxtArea.setText(text);
        nameTxtArea.getStyleClass().add("center-text-area");

        HBox hBox = new HBox(1000);
        hBox.setAlignment(Pos.CENTER);

        VBox vBox = new VBox(10);
        vBox.setAlignment(Pos.CENTER);
        vBox.getChildren().addAll(helloLbl, myNameLbl, nameTxtArea, hBox);
        vBox.setStyle("-fx-background-color: red;margin: 20px;");

        vBox.getStylesheets().add(Main.class.getResource("/style.css").toExternalForm());

        return vBox;
    }

    private void adjustTextAreaLayout(TextArea textArea) {
        textArea.applyCss();
        textArea.layout();

        ScrollPane textAreaScroller = (ScrollPane) textArea.lookup(".scroll-pane");
        Text text = (Text) textArea.lookup(".text");


        ChangeListener<? super Bounds> listener = 
                (obs, oldBounds, newBounds) -> centerTextIfNecessary(textAreaScroller, text);
        textAreaScroller.viewportBoundsProperty().addListener(listener);
        text.boundsInLocalProperty().addListener(listener);

    }

    private void centerTextIfNecessary(ScrollPane textAreaScroller, Text text) {
        double textHeight = text.getBoundsInLocal().getHeight();
        double viewportHeight = textAreaScroller.getViewportBounds().getHeight();
        double offset = Math.max(0, (viewportHeight - textHeight) / 2 );
        text.setTranslateY(offset);
        Parent content = (Parent)textAreaScroller.getContent();
        for (Node n : content.getChildrenUnmodifiable()) {
            if (n instanceof Path) { // caret
                n.setTranslateY(offset);
            }
        }
    }
}
@Override
public void start(Stage stage) {
    ...
    stage.setScene(scene);
    stage.show();

    // attach keyboard to first node on scene:
    Node first = scene.getRoot().getChildrenUnmodifiable().get(0);
    if (first != null) {
        FXVK.init(first);
        FXVK.attach(first);
    }
}
FXVK.detach();
private PopupWindow getPopupWindow() {

    @SuppressWarnings("deprecation") 
    final Iterator<Window> windows = Window.impl_getWindows();

    while (windows.hasNext()) {
        final Window window = windows.next();
        if (window instanceof PopupWindow) {
            if (window.getScene() != null && window.getScene().getRoot() != null) { 
                Parent root = window.getScene().getRoot();
                if (root.getChildrenUnmodifiable().size() > 0) {
                    Node popup = root.getChildrenUnmodifiable().get(0);
                    if (popup.lookup(".fxvk") != null) {
                        FXVK vk = (FXVK) popup.lookup(".fxvk");

                        return (PopupWindow) window;
                    }
                }
            }
            return null;
        }
    }
    return null;
}
// hide the key:
vk.lookup(".hide").setVisible(false);
public class FXVKAlwaysOn extends Application {

    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Say 'Hello World'");

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

        Scene scene = new Scene(root, 1280, 600);

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

        Node first = scene.getRoot().getChildrenUnmodifiable().get(0);
        if (first != null) {
            FXVK.init(first);
            FXVK.attach(first);
            getPopupWindow();
        }
    }

    private PopupWindow getPopupWindow() {

        @SuppressWarnings("deprecation") 
        final Iterator<Window> windows = Window.impl_getWindows();

        while (windows.hasNext()) {
            final Window window = windows.next();
            if (window instanceof PopupWindow) {
                if (window.getScene() != null && window.getScene().getRoot() != null) { 
                    Parent root = window.getScene().getRoot();
                    if (root.getChildrenUnmodifiable().size() > 0) {
                        Node popup = root.getChildrenUnmodifiable().get(0);
                        if (popup.lookup(".fxvk") != null) {
                            FXVK vk = (FXVK) popup.lookup(".fxvk");
                            // hide the key:
                            vk.lookup(".hide").setVisible(false);
                            return (PopupWindow) window;
                        }
                    }
                }
                return null;
            }
        }
        return null;
    }
}