如何从我的 Jframe 按钮调用访问 main 中的 LinkedList

How to access a LinkedList inside of main from my Jframe button call

::::::更新::::

我尝试更新我的 JFrame 以将 LinkedList 作为参数,它看起来像...

 public userLogin(LinkedList<dataUser> ll) {
    initComponents();
 }

我的主程序现在调用它...

userLogin frame = new userLogin(userLL);
frame.setVisible(true);

仍然无法使用我的 JFrame

中的 LinkedList userLL

:::::: 更新结束::::::

这是我第一次使用 netbeans 和 GUI 生成器。我有一个 class TaskManager 用作我的主要 class。这个 class 创建了几个 LinkedList 并像这样调用我的第一个 JFrame GUI:

public static void main(String[] args) {
    LinkedList<dataUser> userLL = new LinkedList<dataUser>(); //creates a LL for userData
    LinkedList<task> taskLL = new LinkedList<task>(); //creates a LL for taskData
    LinkedList<task> progressLL = new LinkedList<task>(); //creates a LL for in progress tasks
    LinkedList<task> completeLL = new LinkedList<task>(); //creates a LL for completed tasks

    userLogin frame = new userLogin();
    frame.setVisible(true);

但是,在我的 userLogin 中,我无法访问我创建的 userLL。 这是我的提交按钮中的代码:

private void submitBActionPerformed(java.awt.event.ActionEvent evt) {                                        

    String user = jTextField1.getText();
    String userPW = jTextField2.getText();

    try {
        //below userLL is not accessible because it can't be found.
        dataUser.userDataSearch(userLL, userPW);
    } 
    catch (Exception e) {
        JOptionPane.showMessageDialog(this, "was not found", "error", JOptionPane.ERROR_MESSAGE);

        return;
    }
}   

正如代码中的注释所述,我无法 运行 该函数,因为我无权访问 userLL(我在主程序中创建的 LinkedList启动这个 JFrame).

我是否必须将 LinkedList 作为参数传入我的 JFrame 才能使用它?我假设在 main 中声明它会允许访问,但它现在似乎是 main 本地的。

您必须像这样将 userLL 作为参数传递给您的 userLogin class :

public class userLogin {

    LinkedList<dataUser> userLL

    public userLogin(List userLL){
        this.userLL = userLL;
    }

    //.......
}

并且在你的主要 class 中像这样实例化它:userLogin frame = new userLogin(userLL);

有两种选择可以解决这个问题。

  • 您已经提到了第一个:传递 LinkedList(在构造函数中或之后,通过一些 setter 方法)。

  • 第二种选择是将 LinkedList 声明为主 class 的对象变量并使用 getter 访问它。如果您没有对主要 class 对象的引用,静态变量应该可以解决问题(如果只有 LinkedList 的一个实例。

我肯定会推荐选项 1,因为它是标准方式并且访问列表的方式受限更好。

您将无法访问在 main() class 中创建的列表。您必须将它们作为参数传递给您创建的 UserLogin class。

类似于:

public class UserLogin
{
     private final LinkedList<datauser> ll1;
     private final LinkedList<task> taskLL;
     private final LinkedList<task> progressLL;
     private final LinkedList<task> completeLL;
     public class UserLogin(LinkedList<datauser> ll1, LinkedList<task> taskLL, LinkedList<task> progressLL, LinkedList<task> completeLL)
     {
        this.ll1 = ll1;
        this.taskLL = taskLL;
        this.progressLL = progressLL;
        this.completeLL = completeLL;
     }  

    //now you should be able to use the linked lists within the event handler
    private void submitBActionPerformed(java.awt.event.ActionEvent evt) {                                        

    String user = jTextField1.getText();
    String userPW = jTextField2.getText();

    try {
        //below userLL is not accessible because it can't be found.
        dataUser.userDataSearch(userLL, userPW);
    } 
    catch (Exception e) {
        JOptionPane.showMessageDialog(this, "was not found", "error", JOptionPane.ERROR_MESSAGE);

        return;
    }
}   

根据 OP 的评论更新。 在 main 方法中,您将初始化 UserLogin,如下所示:

public static void main(String[] args)
{
   LinkedList<datauser> ll1 = new LinkedList<datauser>();
   LinkedList<datauser> taskLL = new LinkedList<datauser>();
   LinkedList<datauser> progressLL = new LinkedList<datauser>();
   LinkedList<datauser> completeLL = new LinkedList<datauser>();

   //feed data within the above linked lists..
   ...

  //initialize the user login class with the linked lists you created
   UserLogin userLogin = new userLogin(ll1,taskLL, progressLL, completeLL);
}

总的来说 - 如果您之前没有广泛使用过 java,您可能想看看与对象创建相关的核心 Java 概念。

您创建了一个扩展 JFrame 的 class 用户登录(这个魔法是由 Netbeans 完成的)。 main class 只是您程序的一个入口点,main 的范围对于用户登录 class.

是未知的

你必须选择(你有其他的静态声明,但我们不能考虑那些):

  1. 使用@Maraboc post

  2. 之类的构造函数传递您需要由class 处理的数据
  3. 在UserLogin class中创建一个setter方法来设置列表或其他数据,但您需要确保在需要时设置数据它。

还有其他选项可以在程序上下文中向 class 提供外部数据。根据 SOLID 范例,使用一些单例模式(例如:GlobalDataStorageSingleton)可能是一个糟糕的设计。

您可以在 OOP 中查看 SOLID 与 STUPID 原则。

最佳 佩德罗