【问题标题】:Variable cannot be called outside of MouseListener constructor不能在 MouseListener 构造函数之外调用变量
【发布时间】:2012-11-18 08:48:39
【问题描述】:

我正在尝试使用 Swing 创建一个 Java 程序。我要完成的一件事是使用 MouseListener 获取 JList 中单击项目的索引并检索与数组索引关联的变量。我的问题是,当我尝试在 MouseListener 之外调用变量时,它不会被识别。我的代码是:

public class UserListPanel extends JPanel {

LibraryController ctrl = new LibraryController();
JScrollPane scrollpane;
public int userid;
public String userName;

public UserListPanel(final Borrower[] borrowersArray) {

    String userArray [] = new String [borrowersArray.length];
    for (int i = 0; i < userArray.length; i++) {
        userArray[i] = borrowersArray[i].getName();
    }

    JList userList = new JList(userArray);
    scrollpane = new JScrollPane(userList);
    this.add(scrollpane);

    // Adds a mouse click listener to assign values from the JList to a variable on click
    userList.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent evt) {
            JList userList = (JList)evt.getSource();
            if (evt.getClickCount() >= 0) {
                int index = userList.locationToIndex(evt.getPoint());
                ListModel dlm = userList.getModel();
                Object item = dlm.getElementAt(index);
                userList.ensureIndexIsVisible(index);
                userid = borrowersArray[index].getbID();
                userName = borrowersArray[index].getName();
                JOptionPane.showMessageDialog(null, userName);
            }
        }
    });
}

userid = borrowersArray[index].getbID();

}

例如,在 MouseListener 构造函数中,我能够正确获取变量并将其存储在 userid 变量中,并且我的 JOptionPane 通过返回一个数字来确认这一点。但是,在构造函数之外,整数“索引”无法识别,因此如果我调用 userid,它将返回 null。我将如何在 MouseListener 之外获得索引副本?

【问题讨论】:

    标签: java variables constructor mouseevent mouselistener


    【解决方案1】:

    如果你想在mouseClicked 方法之外使用index(不是构造函数),那么你应该在mouseClicked 方法之外对其进行初始化,并在mouseClicked 方法中为其分配一个值,然后你将能够得到它index 在构造函数之外。

    您在mouseClicked 方法内声明和初始化index,以便变量的范围取决于mouseClicked 方法,因此它在其范围之外不可用,即在mouseClicked 方法之外。

    public UserListPanel(final Borrower[] borrowersArray) {
        int index=0;
        String userArray [] = new String [borrowersArray.length];
        for (int i = 0; i < userArray.length; i++) {
            userArray[i] = borrowersArray[i].getName();
        }
    
          ..... all other stuff
    }
    

    【讨论】:

    • 其实,不,经过进一步检查,它不起作用。该变量永远不会从 MouseListener 构造函数中获取值,并且默认为 0。例如,当我调用 username 时,它​​总是会返回与列表的第一个索引 (0) 关联的名称。显然,构造函数外部的索引永远不会从内部传递整数。
    • 您在声明时是否将其初始化为某个值
    • 不,它只是声明为 int 索引。 Java 默认值为 0。因为 0 是数组的第一个条目,而 JList 的第一个条目是返回的内容,所以我认为构造函数中的值分配在构造函数之外会被忽略。
    • 在构造函数开始时声明并初始化它,请参阅我编辑的答案
    • 通过移动 int index = 0;进入 UserListPanel(final Borrower[] borrowersArray) 构造函数时,我在所有索引实例上都收到一个错误,说我不能在另一个方法中定义的内部类中引用非最终变量索引。但是,通过声明 index final,index = userList.locationToIndex(evt.getPoint()) 会出错,因为我无法在封闭类型中定义 index(它是 final)。
    【解决方案2】:

    index 变量在MouseListener 实现中声明。因此,编译器不理解您尝试访问它的任何 index 变量。

    要解决此问题,请尝试在 Listener 实现之外声明 index

    public class UserListPanel extends JPanel {
    
      LibraryController ctrl = new LibraryController();
      JScrollPane scrollpane;
      public int userid;
      public String userName;
      public int index ; //declare it as a global (member) variable
    ...
    }
    

    【讨论】:

    • 显然我忽略了这一点。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2012-11-10
    • 2016-07-16
    • 2012-06-30
    • 2017-05-12
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 2018-02-20
    相关资源
    最近更新 更多