我正在编写一个程序,当它启动时,其中一个锚定面板会在光标位置处打开.我用一个方法创建了它,该方法在初始化()中被调用,并且运行良好.问题是,基本上,当我隐藏PrimiyStage或关闭它,并再次实例化一个stage以显示时,它将不再获得光标位置,并且UI一直显示在第一次相同的位置.就像控制器一直在运行一样.有人知道为什么会发生这种事吗?我怎么才能解决这个问题呢? 有没有一种方法可以在锚定面板启动或可见时更改控制器中的位置?还是停止那些一直在运行的东西?让它保持开放的解决方案对性能会更好.我正在使用JnativeHook来调用它,并使用SceneBuilder. 谢谢. JDK 21,JAVAFX 21.0.1,JNativeHook 2.2.2.
主要内容:
package MRE;
import java.io.IOException;
import com.github.kwhat.jnativehook.GlobalScreen;
import com.github.kwhat.jnativehook.NativeHookException;
import com.github.kwhat.jnativehook.keyboard.NativeKeyEvent;
import com.github.kwhat.jnativehook.keyboard.NativeKeyListener;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class Main extends Application implements NativeKeyListener{
private Stage primaryStage;
public static boolean shortcutIsPressed;
public static void main(String[] args) {
Platform.setImplicitExit(false);
nativeKeyListener();
}
@Override
public void start(Stage primaryStage) throws Exception {
}
public void testStage() throws IOException {
primaryStage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("/MRE/test.fxml"));
Scene scene = new Scene(root);
//SHOW STAGE
primaryStage.setAlwaysOnTop(true);
primaryStage.setScene(scene);
primaryStage.show();
}
//JNATIVEHOOK///////////////////////////////////////////////////////////////////////
public void nativeKeyPressed(NativeKeyEvent e) {
// System.out.println("Key Pressed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
Platform.runLater( () -> {
if (e.getKeyCode() == NativeKeyEvent.VC_F5) {
shortcutIsPressed = true;
// System.out.println("F5: " + isPressed);
if(shortcutIsPressed = true) {
if(primaryStage == null) {
try {
testStage();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
} );
}
public void nativeKeyReleased(NativeKeyEvent e) {
System.out.println("Key Released: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
Platform.runLater( () -> {
if (e.getKeyCode() == NativeKeyEvent.VC_F5) {
shortcutIsPressed = false;
if(primaryStage != null) {
primaryStage.hide();
primaryStage = null;
// Platform.exit();
}
}
});
}
public void nativeKeyTyped(NativeKeyEvent e) {
// System.out.println("Key Typed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
}
public static void nativeKeyListener() {
//NATIVEKEYLISTENER
try {
GlobalScreen.registerNativeHook();
}
catch (NativeHookException ex) {
System.err.println("There was a problem registering the native hook.");
System.err.println(ex.getMessage());
System.exit(1);
}
GlobalScreen.addNativeKeyListener(new Main());
}
}
控制器:
package MRE;
import java.io.IOException;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
public class Controller {
@FXML
private AnchorPane aPaneInside;
@FXML
private AnchorPane aPaneOutside;
@FXML
private Button btn;
public void initialize() throws IOException {
setUiPosition();
}
public void setUiPosition() {
aPaneInside.setLayoutX(Utils.cursorPoint.getX()-aPaneInside.getPrefWidth()/2);
aPaneInside.setLayoutY(Utils.cursorPoint.getY()-aPaneInside.getPrefHeight()/2);
}
}
实用程序:
package MRE;
import java.awt.MouseInfo;
import java.awt.Point;
public class Utils {
public static Point cursorPoint = MouseInfo.getPointerInfo().getLocation();
}
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane fx:id="aPaneOutside" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="1080.0" prefWidth="1920.0" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1" fx:controller="MRE.Controller">
<children>
<AnchorPane fx:id="aPaneInside" layoutX="860.0" layoutY="440.0" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: blue;">
<children>
<Button fx:id="btn" layoutX="74.0" layoutY="88.0" mnemonicParsing="false" text="Button" />
</children>
</AnchorPane>
</children>
</AnchorPane>
模块信息:
module MRE {
requires javafx.fxml;
requires javafx.graphics;
requires javafx.controls;
requires java.desktop;
requires com.github.kwhat.jnativehook;
opens MRE to javafx.graphics, javafx.fxml;
}