加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 04:27:47 所属栏目:Java 来源:网络整理
导读:::::::更新::::: 我尝试更新我的JFrame以将LinkedList作为参数,它看起来像…… public userLogin(LinkedListdataUser ll) { initComponents(); } 我的主要是现在称它为…… userLogin frame = new userLogin(userLL);frame.setVisible(true); 仍然无法在我的
::::::更新:::::

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

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

我的主要是现在称它为……

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

仍然无法在我的JFrame中使用LinkedList userLL

:::::: END UPDATE ::::::

这是我第一次使用netbeans和GUI构建器.我有一个类TaskManager,我正在使用它作为我的主类.这个类创建了一些LinkedLists并调用我的第一个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中声明它会允许访问,但它现在似乎是本地的主要.

解决方法

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

public class userLogin {

    LinkedList<dataUser> userLL

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

    //.......
}

在你的主类中实例化如下:userLogin frame = new userLogin(userLL);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读