Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.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_Focus - Fatal编程技术网

按下热键时,JavaFX应用程序无法接收焦点

按下热键时,JavaFX应用程序无法接收焦点,java,javafx,focus,Java,Javafx,Focus,当按下热键(ALT+SHIFT+V)时,焦点不会回到应用程序。 我用作全局键盘侦听器。我试图添加到stage或节点requestFocus()和toFront()中的任何一个,但没有任何帮助。使用swing时的情况相同。还尝试为swing和javFX应用程序设置模式-没有帮助。我可以选择添加java.awt.Robot,但我不确定这是不是更好的主意。 下面是一个代码示例 import javafx.application.Application; import javafx.applicatio

当按下热键(ALT+SHIFT+V)时,焦点不会回到应用程序。 我用作全局键盘侦听器。我试图添加到stage或节点
requestFocus()
toFront()
中的任何一个,但没有任何帮助。使用
swing
时的情况相同。还尝试为
swing
javFX
应用程序设置模式-没有帮助。我可以选择添加
java.awt.Robot
,但我不确定这是不是更好的主意。 下面是一个代码示例

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import org.jnativehook.GlobalScreen;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;

import java.awt.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ShowStageTest extends Application{

    private static Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());

    private static final int WIDTH = 300;
    private static final int HEIGHT = 400;

    public static void main(String[] args) {
        logger.setLevel(Level.WARNING);

        Application.launch(ShowStageTest.class);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Platform.setImplicitExit(false);

        primaryStage.setAlwaysOnTop(true);
        primaryStage.initStyle(StageStyle.UNDECORATED);
        primaryStage.focusedProperty().addListener((ov, t, t1) -> {
            if (t) {
                primaryStage.hide();
            }
        });

        ListView<String> itemList = new ListView<>();
        itemList.setPrefSize(WIDTH, HEIGHT - HEIGHT / 10);
        itemList.setFixedCellSize(HEIGHT / 10);

        TextField textField = new TextField();
        textField.setPrefSize(WIDTH, HEIGHT / 10);
        textField.setOnKeyPressed(ke -> {
            if (ke.getCode().equals(KeyCode.ENTER)) {
                itemList.getItems().add(0, textField.getText());
                textField.setText("");
            }
        });

        VBox box = new VBox(textField, itemList);
        box.setPrefSize(WIDTH, HEIGHT);

        Scene scene = new Scene(box, WIDTH, HEIGHT);
        primaryStage.setScene(scene);

        GlobalScreen.registerNativeHook();
        GlobalScreen.addNativeKeyListener(new NativeKeyListener() {
            @Override
            public void nativeKeyPressed(NativeKeyEvent e) {
                boolean isAltPressed = (e.getModifiers() & NativeKeyEvent.ALT_MASK) != 0;
                boolean isShiftPressed = (e.getModifiers() & NativeKeyEvent.SHIFT_MASK) != 0;

                if (e.getKeyCode() == NativeKeyEvent.VC_V && isShiftPressed && isAltPressed) {
                    Platform.runLater(() -> {
                        Point mouseLocation = MouseInfo.getPointerInfo().getLocation();

                        primaryStage.setX(mouseLocation.getX());
                        primaryStage.setY(mouseLocation.getY());
                        primaryStage.show();
                        primaryStage.toFront();
                    });
                }
            }

            @Override
            public void nativeKeyReleased(NativeKeyEvent nativeKeyEvent) {}
            @Override
            public void nativeKeyTyped(NativeKeyEvent nativeKeyEvent) {}
        });
    }
}
导入javafx.application.application;
导入javafx.application.Platform;
导入javafx.scene.scene;
导入javafx.scene.control.*;
导入javafx.scene.control.TextField;
导入javafx.scene.input.KeyCode;
导入javafx.scene.layout.VBox;
导入javafx.stage.stage;
导入javafx.stage.StageStyle;
导入org.jnativehook.GlobalScreen;
导入org.jnativehook.keyboard.NativeKeyEvent;
导入org.jnativehook.keyboard.NativeKeyListener;
导入java.awt.*;
导入java.util.logging.Level;
导入java.util.logging.Logger;
公共类ShowStageTest扩展了应用程序{
私有静态记录器Logger=Logger.getLogger(GlobalScreen.class.getPackage().getName());
专用静态最终整数宽度=300;
专用静态最终内部高度=400;
公共静态void main(字符串[]args){
logger.setLevel(Level.WARNING);
应用程序启动(ShowStageTest.class);
}
@凌驾
public void start(Stage primaryStage)引发异常{
Platform.setImplicitExit(false);
primaryStage.setAlwaysOnTop(真);
初级阶段。初始风格(阶段风格。未装饰);
primaryStage.focusedProperty().addListener((ov,t,t1)->{
if(t){
primaryStage.hide();
}
});
ListView itemList=新建ListView();
itemList.setPrefSize(宽度、高度-高度/10);
itemList.setFixedCellSize(高度/10);
TextField TextField=新的TextField();
textField.setPrefSize(宽度、高度/10);
textField.setOnKeyPressed(ke->{
if(ke.getCode().equals(KeyCode.ENTER)){
itemList.getItems().add(0,textField.getText());
textField.setText(“”);
}
});
VBox box=新的VBox(文本字段,项目列表);
框。设置尺寸(宽度、高度);
场景=新场景(框、宽度、高度);
初级阶段。场景(场景);
GlobalScreen.registerNativeHook();
GlobalScreen.addNativeKeyListener(新的NativeKeyListener(){
@凌驾
公共无效nativeKeyPressed(NativeKeyEvent e){
布尔值isAltPressed=(e.getModifiers()&NativeKeyEvent.ALT_MASK)!=0;
布尔值isShiftPressed=(e.getModifiers()&NativeKeyEvent.SHIFT_MASK)!=0;
如果(如getKeyCode()==NativeKeyEvent.VC\u V&&IsShift已按下&&isAltPressed){
Platform.runLater(()->{
点鼠标位置=MouseInfo.getPointerInfo().getLocation();
setX(mouseLocation.getX());
setY(mouseLocation.getY());
primaryStage.show();
初级阶段toFront();
});
}
}
@凌驾
public void nativeKeyReleased(NativeKeyEvent NativeKeyEvent){}
@凌驾
public void nativeKeyTyped(NativeKeyEvent NativeKeyEvent){}
});
}
}

我认为您必须在应用程序之间以及应用程序的每个部分使用API,才能编写所需的界面。
关于
swing
,您可以使用
swing
中的对话框窗口,也可以创建
JFrame
并添加
JPanel

我认为您必须在应用程序之间以及在需要编写所需界面的应用程序的每个部分使用API。这有助于获得关注?swing应用程序上的对话框没有帮助。你有没有想过这一点?