我试图将一个方法从一个单独的类文件调用到我的主程序java文件中,但是当我尝试在一个对象上调用该方法时,我得不到该方法的错误符号。两个文件都在同一目录中。
//This is my main java file for the program
package pwmanager;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author 176878
*/
public class PWManager extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Login.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("Password Manager");
stage.setPrevStage(); //Error occurs here.
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
//This is the other .java file where the method is declared.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pwmanager;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.EventType;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javax.xml.crypto.Data;
/**
*
* @author 176878
*/
public class FXMLDocumentController implements Initializable {
@FXML
private Button loginButton;
private Button addAcct;
private Button removeAcct;
@FXML
public Stage prevStage;
public Stage currentStage;
@FXML
public void loginButtonAction(ActionEvent event) throws IOException {
System.out.println("You clicked me, logging in!");
Stage stage = new Stage();
FXMLLoader loader = new FXMLLoader(getClass().getResource("MainScreen.fxml"));
Parent mainScreen = (Parent)loader.load();
Scene scene = new Scene(mainScreen);
stage.setScene(scene);
stage.setTitle("Password Manager");
//prevStage.close();
stage.show();
}
@FXML
public void addAcctAction(ActionEvent event) throws IOException {
System.out.println("You clicked me, adding account!");
}
@FXML
public void removeAcctAction(ActionEvent event) throws IOException {
System.out.println("You clicked me, removing account!");
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
我需要存储当前舞台,以便我可以回拨或关闭舞台。
答案 0 :(得分:1)
setPrevStage
在FXMLDocumentController
而非Stage
中定义。您需要将前者注入主PWManager
类,以便可以调用它
controller.setPrevStage(stage);