在 actionListener 中获取 JTextField 的参考

getting refrence for JTextFeild in actionListener

我正在尝试在附加的 ActionListener 中使用 JTextFeild 中的 getText() 方法...问题是我没有指向它的引用...也就是说我正在添加那些textFeilds 在一个循环中从 arraylist 中获取一个字符串并显示新的 textFeild ,我在互联网上搜索试图找到一种使用 getText() 的方法,但这毫无意义,因为我没有参考。关于它,我的问题是如何在此操作侦听器中获取 JTextFeild 中的文本,是否有任何方法可以获取对该操作执行的 JTextFeild 的引用????

 JTextField t;
 for(MyClass m: MyArraylist) {
     t=new JTextField(m.toString());
     t.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                    System.out.println(getText());
                    }
                    });
      }

我试过getText(); super.getText(); t.getTaxt();并且肯定它不会工作,因为 t 总是变化,我也试过 system.out.println(m.toString());并且不起作用

你必须使用对象中的函数

t.getText()

here

您应该获取事件源并将其投射到 TextField

t.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        TextField tf = (TextField) e.getSource();
        System.out.println(tf.getText());
    }
});