gpt4 book ai didi

Java找不到方法符号

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:44:43 24 4
gpt4 key购买 nike

我试图从 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());
}

}

主.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){

}

}

最佳答案

在你的 Controller 中你正在调用 main.getConnection()

@FXML
public void connectEvent(ActionEvent event){
main.getConnection().connect(getUsernameText().getText(), passwordText.getText(), ipText.getText());
}

但是在您的主类中没有getConnection() 方法。

将以下代码放在您的主类中。

public Connection  getConnection(){
return connection;
}

关于Java找不到方法符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29531451/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com