【问题标题】:Cannot find symbol error when calling method from different class file从不同的类文件调用方法时找不到符号错误
【发布时间】:2015-01-09 17:14:56
【问题描述】:

我试图从一个单独的类文件中调用一个方法到我的主程序 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
}    

}

我需要存储当前阶段,以便我可以调用它或关闭阶段。

【问题讨论】:

  • 您需要向我们展示您的场景类代码,不是吗?您是在 Scene 实例上调用该方法,而不是在 FXMLDocumentController 实例上,实际上,我不确定您为什么发布后一个类的代码。您是否对哪个类实际上具有 setPrevStage() 方法感到困惑?
  • 我从我的两个 Java 文件中发布了代码,所以这就是我所拥有的一切......我想我对我想要完成的事情感到困惑,因为这是我第一次尝试 GUI。我正在尝试存储当前的舞台/场景,以便我可以根据需要关闭并检索它。我想我需要做更多的研究来找出我需要做的事情。
  • 我已经发布了我的项目的所有代码,包括我的所有 FXMLDocumentController.java 文件和我所有的 PWManager.java 文件。我只有两个类文件。我正在尽力向您提供我所拥有的信息和我收到的错误。

标签: java methods stage scene


【解决方案1】:

setPrevStage 是在 FXMLDocumentController 中定义的,而不是在 Stage 中定义的。您需要将前者注入到主 PWManager 类中,以便可以调用它

controller.setPrevStage(stage);

【讨论】:

  • 好的,所以我尝试用 root.setPrevStage(stage); 替换我的代码和 controller.setPrevStage(stage);这些都不起作用。我是否在我的代码中遗漏了需要首先定义的其他内容?
  • 请解释它是如何不工作的。同时,stage 不是null,所以setPrevStage 中不会显示任何内容...
  • 我仍然收到相同的错误“找不到符号”所以我不确定我是否缺少更多代码...
  • @Mageneto:再次,请改进您的问题。
猜你喜欢
  • 2014-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-06
  • 2018-11-08
相关资源
最近更新 更多