这是我的主要应用程序
package com.example.lclicker;
import com.github.kwhat.jnativehook.GlobalScreen;
import com.github.kwhat.jnativehook.NativeHookException;
import com.github.kwhat.jnativehook.keyboard.NativeKeyEvent;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.stage.Stage;
import java.io.IOException;
import com.github.kwhat.jnativehook.keyboard.NativeKeyListener;
public class HelloApplication extends Application implements NativeKeyListener {
private LClickerController controller;
public void nativeKeyPressed(NativeKeyEvent e) {
String code = NativeKeyEvent.getKeyText(e.getKeyCode());
if (controller.getEndButtonText() == String.valueOf(KeyCode.F3)) {
System.out.println("Hi");
}
System.out.println("Hello");
}
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("LClicker.fxml"));
Scene scene = new Scene(fxmlLoader.load());
this.controller = fxmlLoader.getController();
stage.setTitle("LClicker");
stage.setScene(scene);
stage.setResizable(false);
stage.show();
stage.setOnCloseRequest(event -> {
//If hotKeyWindow Stage has been created, close it. Or else it does nothing
if (LClickerController.hotKeyWindow != null) {
LClickerController.hotKeyWindow.close();
} else {
}
//If helpWindow Stage has been created, close it. Or else it does nothing
if (LClickerController.helpWindow != null) {
LClickerController.helpWindow.close();
} else {
}
//If updatesWindow Stage has been created, close it. Or else it does nothing
if (LClickerController.updatesWindow != null) {
LClickerController.updatesWindow.close();
}else {
}
//Unregisters the NativeHook, prints StackTrace if error occurs
try {
GlobalScreen.unregisterNativeHook();
} catch (NativeHookException n) {
n.printStackTrace();
}
System.out.println("Closed Successfully");
});
}
public static void main(String[] args) {
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 HelloApplication());
launch();
}
}
LClickerController.java类
package com.example.lclicker;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.input.KeyEvent;
import javafx.scene.text.Text;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import java.io.IOException;
public class LClickerController {
@FXML
private RadioButton cLButton;
@FXML
private TextField x_Field;
@FXML
private TextField y_Field;
@FXML
private Text x_Label;
@FXML
private Text y_Label;
@FXML
private ChoiceBox mouseButtonChoiceBox;
@FXML
private ChoiceBox singleDoubChoiceBox;
@FXML
private Button startButton;
@FXML
private Button endButton;
public static Stage helpWindow;
public static Stage hotKeyWindow;
public static Stage updatesWindow;
@FXML
public void initialize() {
mouseButtonChoiceBox.setItems(FXCollections.observableArrayList("Left","Right","Middle"));
mouseButtonChoiceBox.getSelectionModel().selectFirst();
singleDoubChoiceBox.setItems(FXCollections.observableArrayList("Single", "Double"));
singleDoubChoiceBox.getSelectionModel().selectFirst();
}
@FXML
public void cLOnSelected(ActionEvent actionEvent) {
if (cLButton.isSelected()) {
x_Field.setVisible(false);
y_Field.setVisible(false);
x_Label.setVisible(false);
y_Label.setVisible(false);
}
}
@FXML
public void sLOnSelected(ActionEvent actionEvent) {
x_Field.setVisible(true);
y_Field.setVisible(true);
x_Label.setVisible(true);
y_Label.setVisible(true);
}
@FXML
public void helpButtonClicked(ActionEvent actionEvent) throws IOException {
if (helpWindow == null) {
FXMLLoader loader = new FXMLLoader(getClass().getResource("helpWindow.fxml"));
Parent root = loader.load();
helpWindow = new Stage();
helpWindow.setScene(new Scene(root));
helpWindow.setTitle("Help Window");
helpWindow.getIcons().add(new Image("C:\\Users\\lzhan\\OneDrive\\Documents\\Random Coding projects\\LClicker\\src\\main\\resources\\Images\\screwdriver.png"));
helpWindow.setResizable(false);
helpWindow.show();
} else if (helpWindow.isShowing()) {
helpWindow.toFront();
} else {
helpWindow.show();
}
if (helpWindow.isIconified()){
helpWindow.setIconified(false);
helpWindow.toFront();
}
}
@FXML
public void hotKeyButtonClicked(ActionEvent actionEvent) throws IOException{
if (hotKeyWindow == null) {
FXMLLoader loader = new FXMLLoader(getClass().getResource("HotKeySettingsWindow.fxml"));
Parent root = loader.load();
hotKeyWindow = new Stage();
hotKeyWindow.setScene(new Scene(root));
hotKeyWindow.setTitle("HotKey Settings");
hotKeyWindow.setResizable(false);
hotKeyWindow.show();
}else if (hotKeyWindow.isShowing()) {
hotKeyWindow.toFront();
}else {
hotKeyWindow.show();
}
if (hotKeyWindow.isIconified()) {
hotKeyWindow.setIconified(false);
hotKeyWindow.toFront();
}
}
@FXML
public void updatesButtonClicked(ActionEvent actionEvent) throws IOException{
if (updatesWindow == null) {
FXMLLoader loader = new FXMLLoader(getClass().getResource("Updates_Preview.fxml"));
Parent root = loader.load();
updatesWindow = new Stage();
updatesWindow.setScene(new Scene(root));
updatesWindow.setTitle("Updates Previews");
updatesWindow.setResizable(false);
updatesWindow.show();
}else if (updatesWindow.isShowing()) {
updatesWindow.toFront();
}else{
updatesWindow.show();
}
if (updatesWindow.isIconified()) {
updatesWindow.setIconified(false);
updatesWindow.toFront();
}
}
public String getStartButtonText() {
String[] parts = startButton.getText().split("/");
return parts[1];
}
public String getEndButtonText() {
return endButton.getText();
}
public void setButton(){
}
}
Exception in thread "JNativeHook Dispatch Thread" java.lang.NullPointerException: Cannot invoke "com.example.lclicker.LClickerController.getEndButtonText()" because "this.controller" is null
at com.example.lclicker/com.example.lclicker.HelloApplication.nativeKeyPressed(HelloApplication.java:20)
at [email protected]/com.github.kwhat.jnativehook.GlobalScreen$EventDispatchTask.processKeyEvent(GlobalScreen.java:559)
at [email protected]/com.github.kwhat.jnativehook.GlobalScreen$EventDispatchTask.run(GlobalScreen.java:524)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
at java.base/java.lang.Thread.run(Thread.java:833)
我试图从 main 的控制器调用一个方法,发现了 this。我不太确定为什么控制器为空。我试图将它实现到我的代码中,但每当我按下一个键时,它都会告诉我 this.controller 为空,我很不确定为什么会这样。谁能解释我做错了什么? LClicker.fxml 文件的 fx:controller 使用的是 LClickerController.java