我正在尝试从Controller.java中的connectEvent方法内部访问一个方法,它给了我错误:
Java: cannot find symbol
symbol: method getConnection()
location: variable main of type sample.Main
我忽略了什么吗? 我尝试了一切 干杯
Controller.java
package sample;
import javafx.beans.NamedArg;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventType;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import lombok.Getter;
import lombok.Setter;
public class Controller{
@Getter
private Main main;
public Controller(Main main){
this.main = main;
}
//FXML to Java Links
@FXML
@Getter
private Button connectButton;
@FXML
@Getter
private TextArea passwordText;
@FXML
@Getter
private TextArea ipText;
@FXML
@Getter
private TextArea usernameText;
//FXML linked methods
/**
* Called when user tries to connect using supplied information
*/
@FXML
public void connectEvent(ActionEvent event){
main.getConnection().connect(getUsernameText().getText(), passwordText.getText(), ipText.getText());
}
}
Main.java
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import lombok.Getter;
public class Main extends Application {
@Getter
private Controller controller;
@Getter
private Connection connection = new Connection();
@Override
public void start(Stage primaryStage) throws Exception{
FXMLLoader loader = new FXMLLoader(getClass().getResource("Startup.fxml"));
controller = loader.getController();
Parent root = loader.load(getClass().getResource("Startup.fxml"));
primaryStage.setTitle("MCManager");
primaryStage.setScene(new Scene(root, 850, 600));
primaryStage.setResizable(false);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
连接
package sample;
public class Connection {
public void connect(String username, String password, String ip){
}
}
答案 0 :(得分:5)
在您的控制器中,您正在呼叫main.getConnection()
@FXML
public void connectEvent(ActionEvent event){
main.getConnection().connect(getUsernameText().getText(), passwordText.getText(), ipText.getText());
}
但是在您的主类中没有getConnection()
方法。
将以下代码放入您的Main类中。
public Connection getConnection(){
return connection;
}