无法绑定:字段是静态的:form.MainForm.nameTextField [英] Cannot bind: field is static: form.MainForm.nameTextField

查看:26
本文介绍了无法绑定:字段是静态的:form.MainForm.nameTextField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我创建静态方法并在另一个类中调用它们时出现错误:

I'm getting an error when I create static methods and call them in another class:

public static JTextField getNameTxtField(){
    return nameTxtField;
}
public static JTextField getNewUserNameTxtField(){
    return newUserNameTxtField;
}
public static JPasswordField getNewPasswordTextField(){
    return newPasswordTxtField;
}

以上所有的getter都位于MainForm类中,并在这个类中调用:

All the above getters are located in the MainForm class and are called in the this class:

GameLogic:

public void addToDatabase() throws SQLException {
    controller.addUserToDatabase(MainForm.getNameTxtField().getText(),MainForm.getNewUserNameTxtField().getText(), String.valueOf(MainForm.getNewPasswordTextField()) , "insert into application_user values(?, ?, ?)");
}

为什么我会收到消息?我不太明白这条消息,所以有人可以向我解释一下吗?

Why am I getting the message? I don't really understand the message so can someone explain to me?

我无法在 GameLogic 类中创建这样的对象:MainForm form = new MainForm(); 因为我会得到一个 StackOverflowError.

I can't create an object like this: MainForm form = new MainForm(); in the class GameLogic because I will get a StackOverflowError.

问题是不是因为我在非静态方法中调用了静态getter?

Does the problem occur because I call the static getters in a non-static method?

推荐答案

忘记static.我假设您要做的是将一个实例变量从一个类引用到另一个类中.而您的尝试是使用 static 变量.并且您正在使用 stackoverflow,因为您正在尝试这样的事情

Forget the statics. I assume what you're trying to do is reference an instance variable from one class into another. And your attempt is to use static variables. And you're getting stackoverflow because you're attempting something like this

public class MainForm ... {
    public MainForm() {
        GameForm game = new GameForm();
    }
}
public class GameForm ... {
    public GameForm() {
        MainForm main = new MainForm();
    }
}

这肯定会给你一个stackoverflow,因为你创建了一个GameForm,它创建了MainForm,它创建了一个GameForm,它创建了一个MainForm,它创建了一个 GameForm,它创建了一个 MainForm(明白了吗?),它创建了一个 stackoverflow.

This will definitely give you a stackoverflow because you create a GameForm which creates MainForm, which creates a GameForm, which creates a MainForm, which creates a GameForm, which creates a MainForm (get the point?), which creates a stackoverflow.

一个解决方案是通过引用将MainForm 传递给GameForm 并在 中使用gettersetterMainForm 必要时.类似的东西

A solution is to pass MainForm to GameForm by reference and use getter and setters in MainForm where necessary. Something like

public class MainForm ... {
    private JTextField field;
    private GameForm game;

    public MainForm() {
        game = new GameForm(MainForm.this);
    }

    public JTextField getField (){
        return field;
    }
}

public class GameField ... {
    private MainForm main;
    private JTextField field;

    public GameForm(final MainForm main) {
        this.main = main;
        this.field = main.getField();
    }
}

现在,在 GameForm 中,您正在引用同一个实例 JTextField.

Now, in GameForm you are referencing the same instance JTextField.

一个更好,可能更合适的解决方案是使用 interface 并让 MainForm 实现它.您可以查看此答案的示例.

An even better, and probably more appropriate solution though, would be to use an interface and have MainForm implement it. You can see this answer for an example.

这篇关于无法绑定:字段是静态的:form.MainForm.nameTextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆