所以我尝试绑定我的Numpad键,以便在计算器应用程序中使用它们,但当我尝试从主窗口以字符串形式发送keyEvent时。java到我的MainWindowController公共void方法它给了我一个错误“不能从静态上下文引用非静态方法”,即使我的类都不是静态的?以下是主窗口代码:
package main`import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class MainWindow extends Application implements EventHandler<KeyEvent> {
private static Stage historyStage;
@Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("MainWindowInterface.fxml"));
Scene scene = new Scene(loader.load());
scene.setFill(Color.TRANSPARENT);
stage.setScene(scene);
stage.initStyle(StageStyle.TRANSPARENT);
stage.setResizable(false);
stage.setTitle("Calculator");
stage.getIcons().add(new Image(getClass().getResourceAsStream("../images/icon.png")));
((MainWindowController)loader.getController()).init(stage);
stage.show();
//scene.setOnKeyPressed(MainWindowController.handle());
scene.setOnKeyPressed(this);
scene.setOnKeyPressed(event -> {
if (event.getCode() == KeyCode.ESCAPE){
stage.close();
}
});
createHistoryStage();
}
public void createHistoryStage() throws Exception{
historyStage = new Stage();
historyStage.initStyle(StageStyle.TRANSPARENT);
historyStage.setTitle("Calculator History");
historyStage.setAlwaysOnTop(true);
historyStage.setResizable(false);
historyStage.getIcons().add(new Image(getClass().getResourceAsStream("../images/icon.png")));
historyStage.initModality(Modality.APPLICATION_MODAL);
}
public static Stage getHistoryStage(){
return historyStage;
}
public void run() {
launch();
}
@Override
public void handle(KeyEvent event) {
String valueOfKey = String.valueOf(event.getCode());
MainWindowController.keyPress(valueOfKey);
}
}
这里是MainWindowController文件(看keyPress方法)
package main`import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import java.io.IOException;
import java.util.ArrayList;`;
public class MainWindowController {
@FXML private Pane titlePane;
@FXML private ImageView btnMinimize, btnClose;
@FXML private Label lblResult;
private double x, y;
private double num1 = 0;
private String operator = "+";
private ArrayList<String> calculation_history = new ArrayList<>();
public void init(Stage stage) {
titlePane.setOnMousePressed(mouseEvent -> {
x = mouseEvent.getSceneX();
y = mouseEvent.getSceneY();
});
titlePane.setOnMouseDragged(mouseEvent -> {
stage.setX(mouseEvent.getScreenX()-x);
stage.setY(mouseEvent.getScreenY()-y);
});
btnClose.setOnMouseClicked(mouseEvent -> stage.close());
btnMinimize.setOnMouseClicked(mouseEvent -> stage.setIconified(true));
}
public void openHistoryWindow(){
try{
FXMLLoader loader2 = new FXMLLoader(getClass().getResource("history.fxml"));
Parent root = loader2.load();
Scene historyScene = new Scene(root);
historyScene.setFill(Color.TRANSPARENT);
MainWindow.getHistoryStage().setScene(historyScene);
HistoryController historyController = loader2.getController();
historyController.initializeCalculations(calculation_history);
((HistoryController)loader2.getController()).init(MainWindow.getHistoryStage());
MainWindow.getHistoryStage().show();
historyScene.setOnKeyPressed(event -> {
if (event.getCode() == KeyCode.ESCAPE){
MainWindow.getHistoryStage().close();
}
});
}catch (IOException ex){
System.out.println(ex);
}
}
public void addCalculation(String num1, String operator, String num2, String result){
String expression = num1 + " " + operator + " " + num2;
this.calculation_history.add(expression + " = " + result);
}
@FXML
void onNumberClicked(MouseEvent event) {
int value = Integer.parseInt(((Pane)event.getSource()).getId().replace("btn",""));
lblResult.setText(Double.parseDouble(lblResult.getText())==0?String.valueOf((double)value):String.valueOf(Double.parseDouble(lblResult.getText())*10+value));
}
public void keyPress(String keyPressed){
int value = 0;
switch (keyPressed){
case "NUMPAD1": value = 1;
lblResult.setText(Double.parseDouble(lblResult.getText())==0?String.valueOf((double)value):String.valueOf(Double.parseDouble(lblResult.getText())*10+value));
break;
case "NUMPAD2": value = 2;
lblResult.setText(Double.parseDouble(lblResult.getText())==0?String.valueOf((double)value):String.valueOf(Double.parseDouble(lblResult.getText())*10+value));
break;
case "NUMPAD3": value = 3;
lblResult.setText(Double.parseDouble(lblResult.getText())==0?String.valueOf((double)value):String.valueOf(Double.parseDouble(lblResult.getText())*10+value));
break;
case "NUMPAD4": value = 4;
lblResult.setText(Double.parseDouble(lblResult.getText())==0?String.valueOf((double)value):String.valueOf(Double.parseDouble(lblResult.getText())*10+value));
break;
case "NUMPAD5": value = 5;
lblResult.setText(Double.parseDouble(lblResult.getText())==0?String.valueOf((double)value):String.valueOf(Double.parseDouble(lblResult.getText())*10+value));
break;
case "NUMPAD6": value = 6;
lblResult.setText(Double.parseDouble(lblResult.getText())==0?String.valueOf((double)value):String.valueOf(Double.parseDouble(lblResult.getText())*10+value));
break;
case "NUMPAD7": value = 7;
lblResult.setText(Double.parseDouble(lblResult.getText())==0?String.valueOf((double)value):String.valueOf(Double.parseDouble(lblResult.getText())*10+value));
break;
case "NUMPAD8": value = 8;
lblResult.setText(Double.parseDouble(lblResult.getText())==0?String.valueOf((double)value):String.valueOf(Double.parseDouble(lblResult.getText())*10+value));
break;
case "NUMPAD9": value = 9;
lblResult.setText(Double.parseDouble(lblResult.getText())==0?String.valueOf((double)value):String.valueOf(Double.parseDouble(lblResult.getText())*10+value));
break;
case "NUMPAD0": value = 0;
lblResult.setText(Double.parseDouble(lblResult.getText())==0?String.valueOf((double)value):String.valueOf(Double.parseDouble(lblResult.getText())*10+value));
break;
default: break;
}
}
@FXML
void onSymbolClicked(MouseEvent event) {
String symbol = ((Pane)event.getSource()).getId().replace("btn","");
if(symbol.equals("Equals")) {
double num2 = Double.parseDouble(lblResult.getText());
switch (operator) {
case "+" -> lblResult.setText((num1+num2) + "");
case "-" -> lblResult.setText((num1-num2) + "");
case "*" -> lblResult.setText((num1*num2) + "");
case "/" -> lblResult.setText((num1/num2) + "");
}
addCalculation(String.valueOf(num1), operator, String.valueOf(num2), lblResult.getText());
operator = ".";
}
else if(symbol.equals("Clear")) {
lblResult.setText(String.valueOf(0.0));
operator = ".";
}
else if(symbol.equals("HIST")){
openHistoryWindow();
}
else {
switch (symbol) {
case "Plus" -> operator = "+";
case "Minus" -> operator = "-";
case "Multiply" -> operator = "*";
case "Divide" -> operator = "/";
}
num1 = Double.parseDouble(lblResult.getText());
lblResult.setText(String.valueOf(0.0));
}
}
}
错误消息告诉您问题:keyPress(...)
是一个实例(“非静态”)方法,您试图访问它,就好像它是一个静态方法一样(即通过类名)。
请注意,按键(…) 访问实例变量,因此不能将其设置为静态。您唯一的选择是通过控制器实例访问该方法。因为您已经通过加载程序检索了该实例。getController(),这很简单;只需将其保存到实例变量:
public class MainWindow extends Application implements EventHandler<KeyEvent> {
private static Stage historyStage;
private MainWindowController controller ;
@Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("MainWindowInterface.fxml"));
Scene scene = new Scene(loader.load());
scene.setFill(Color.TRANSPARENT);
stage.setScene(scene);
stage.initStyle(StageStyle.TRANSPARENT);
stage.setResizable(false);
stage.setTitle("Calculator");
stage.getIcons().add(new Image(getClass().getResourceAsStream("../images/icon.png")));
controller = loader.getController();
controller.init(stage);
stage.show();
//scene.setOnKeyPressed(MainWindowController.handle());
scene.setOnKeyPressed(this);
scene.setOnKeyPressed(event -> {
if (event.getCode() == KeyCode.ESCAPE){
stage.close();
}
});
createHistoryStage();
}
// ...
@Override
public void handle(KeyEvent event) {
String valueOfKey = String.valueOf(event.getCode());
controller.keyPress(valueOfKey);
}
}
问题内容: 我想一劳永逸地理解这一点。 为此,请原谅下面粘贴的大量代码,但是我不想遗漏任何细节。 我唯一更改的是加载的URL。但这不会导致错误。 我想将我的函数称为“ readPosiitons ”。简单的解决方案,使其静态。真正的解决方案,我不确定。 请帮助我更好地了解如何以正确的方式解决此错误。 谢谢!! 问题答案: 真正的解决方案?不要在方法中放太多东西。那是给菜鸟的。 Java是一种面向对
我想更改FrameLayout的背景。这是我现在使用的代码: 但出现以下错误: 无法从静态上下文引用非静态方法“setBackground(android.graphics.drawable.drawable)”。 怎么了?
我从这行中得到了“无法从静态上下文引用非静态方法”错误:
mainactivity.java: xmlReader.java: (如果需要,我也可以为station.java和dbhandler.java添加代码) 我怎样才能绕过这个编程悖论呢?我通常能为这里的任何问题找到答案,但这是我第一次找不到答案。如果你知道我错过的解决方案的链接,请在下面发布。多谢。
这是一个代码的一部分,但当我执行时,我得到了一个错误程序是关于线性规划对不起我的英语,或关于愚蠢的问题 “错误:不能从静态上下文引用非静态方法get_total_profity(String)
问题内容: 不需要任何代码,只需要某种指导。希望保持我的学术诚信;) 我不断收到那个烦人的错误。我需要为每个Room实例调用toString方法。有什么建议?如果可能的话,我希望在2小时内给出答案。 问题答案: 您不应尝试调用Room类,而应调用Room 对象 。在该方法中,请使用for循环遍历房间数组,并打印通过调用数组中保存的每个Room对象返回的String,因为这是您的方法应做的事情。 例