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

Java 如何从Jframe按钮调用访问main中的LinkedList,java,swing,user-interface,netbeans,jframe,Java,Swing,User Interface,Netbeans,Jframe,:更新: 我尝试更新我的JFrame以将LinkedList作为参数,它看起来像 public userLogin(LinkedList<dataUser> ll) { initComponents(); } 仍然无法在myJFrame中使用LinkedList userLL :结束更新: 这是我第一次使用netbeans和GUI构建器。我有一个类TaskManager,用作我的主类。该类创建一些LinkedLists,并调用我的第一个JFrameGUI,如下所示: p

:更新:

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

 public userLogin(LinkedList<dataUser> ll) {
    initComponents();
 }
仍然无法在my
JFrame中使用LinkedList userLL

:结束更新:

这是我第一次使用netbeans和GUI构建器。我有一个类
TaskManager
,用作我的主类。该类创建一些
LinkedList
s,并调用我的第一个
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);
正如代码中的注释所述,我无法运行该函数,因为我无权访问
userLL
(我在启动此
JFrame
的主程序中创建的
LinkedList


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

必须将
userLL
作为参数传递给
userLogin
类,如下所示:

public class userLogin {

    LinkedList<dataUser> userLL

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

    //.......
}
公共类用户登录{
链接列表userLL
公共用户登录(列出userLL){
this.userLL=userLL;
}
//.......
}

在主类中,像这样实例化它:
userLogin frame=newuserlogin(userLL)

有两个选项可以解决此问题

  • 您已经提到了第一个:传递
    LinkedList
    (在构造函数中或更高版本中,通过某种setter方法)

  • 第二个选项是声明
    LinkedList
    为主类的对象变量,并使用getter访问它。如果没有对主类对象的引用,则静态变量应该起作用(如果只有一个
    LinkedList
    实例)


我绝对推荐选项1,因为这是一种标准的方式,访问列表的权限要有限得多。

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

比如:

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;
    }
}   
公共类用户登录
{
私人最终链接列表ll1;
私有最终链接列表taskl;
私人最终LinkedList progressLL;
私人最终LinkedList Completel;
公共类用户登录(LinkedList ll1、LinkedList taskLL、LinkedList progressLL、LinkedList Completel)
{
这1.ll1=ll1;
this.taskl=taskLL;
this.progressLL=progressLL;
this.completel=completel;
}  
//现在,您应该能够在事件处理程序中使用链接列表了
私有void submitBActionPerformed(java.awt.event.ActionEvent evt){
字符串user=jTextField1.getText();
字符串userPW=jTextField2.getText();
试一试{
//由于找不到下面的userLL,因此无法访问它。
userDataSearch(userLL,userPW);
} 
捕获(例外e){
showMessageDialog(这是“未找到”、“错误”、JOptionPane.error\u消息);
返回;
}
}   
根据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);
}
publicstaticvoidmain(字符串[]args)
{
LinkedList ll1=新LinkedList();
LinkedList taskl=新建LinkedList();
LinkedList progressLL=新建LinkedList();
LinkedList completel=新建LinkedList();
//在上述链接列表中输入数据。。
...
//使用您创建的链接列表初始化用户登录类
UserLogin UserLogin=新的UserLogin(ll1、taskl、progressLL、completel);
}

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

您创建了一个扩展JFrame的类userlogin(这个魔术是由Netbeans完成的).main类只是程序的入口点,userlogin类不知道main的作用域

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

  • 使用@Maraboc post之类的构造函数传递您需要由类处理的数据

  • 在UserLogin类中创建一个setter方法来设置列表或其他数据,但需要确保在需要时设置数据

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

    您可以在OOP中查看可靠与愚蠢的原则

    最好的
    Pedro

    你的想法似乎非常正确。我将LinkedList参数添加到我的main中,而我的'JFrame'仍然无法访问。正在更新要显示的主要帖子。你能给我们显示你的
    initComponents()吗
    body?这是否意味着我没有在main中声明我的LinkedList?因此,如果我在main中声明一个LinkedList,从文件中加载带有数据的链接列表,在userLogin中创建的LinkedList将是另一个链接列表?@user3622460在上面显示的示例代码中,您只声明了LinkedList的一些实例,但尚未初始化它们。您现在可以在main()中初始化链接列表,然后将它们传递给UserLogin()构造函数。我将更新答案以显示此步骤。这非常有效,谢谢。但是,它是否会维护main使用此方法提供给LinkedList的任何数据?
    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;
        }
    }   
    
    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);
    }