【问题标题】:How do I specify what 'this' refers to? [duplicate]如何指定“this”指的是什么? [复制]
【发布时间】:2013-03-11 16:32:28
【问题描述】:

我有一个 JFrame,其中一个按钮必须将它自己作为父框架传递,我会使用 this 关键字,但它返回的是 actionlistener,而不是 JFrame。有解决方法还是我写得不好?

代码:

start.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            kMeans=new KMeans(mainWindow.table, Integer.parseInt(centroids.getText()),this);
        }
    });

【问题讨论】:

  • 包含该代码的类名是什么?
  • 否则你可以做JFrame that = this然后通过that而不是this
  • @11684: 那必须是final JFrame that = this;虽然这很有效,但它会使您的范围变得混乱,而且只需执行ClassName.this 就更简单了
  • 忘记尝试了,谢谢,解决了所有问题;)
  • @WChargin 当然你是对的,但我记得我曾经无法让ClassName.this 工作(可能是因为我搞砸了),所以我只是想为 OP 提供一个替代方案。

标签: java this actionlistener


【解决方案1】:

有一个解决方法。要在引用外部类时使用 this 关键字,可以使用 ClassName.this。例如:

class MyFrame extends JFrame {
    public void someMethod () {
        someButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                ActionListener thisListener = this; // obviously
                MyFrame outerThis = MyFrame.this; // here's the trick
            }
        });
    }
}

【讨论】:

    【解决方案2】:

    因为这段代码:

    new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            kMeans=new KMeans(mainWindow.table, Integer.parseInt(centroids.getText()),this);
        }
    }
    

    实际上已经创建了一个新对象。 当您在此ActionListener 实现的方法中使用关键字this 时,它实际上使用this 对象,即ActionListener

    如果你在上面的块之外使用this,它将引用JFrame实例。

    如果你想在 ActionListener 中引用 this 的 AFrame 实例,你可以像 cmets 中提到的那样做 AFrame.this。其中 AFrame 是您的框架类的名称,不确定您的代码中有哪个名称。

    【讨论】:

    • 创建一个新对象是什么意思?您的答案还有不完整的代码(缺少 })
    • Iswanto,感谢您贬低正确答案。好样的。
    【解决方案3】:

    您正在尝试将外部类的引用传递给匿名内部类。为此,您应该使用OuterClassName.this。请参阅下面给出的示例。

    import javax.swing.*;
    import java.awt.event.*;
    class FrameExample extends JFrame
    {
        private void createAndShowGUI()
        {
            JButton button = new JButton("Click");
            getContentPane().add(button);
            button.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent evt)
                {
                    JOptionPane.showMessageDialog(FrameExample.this,"This is the message","Message",JOptionPane.OK_OPTION);//Passing the reference of outer class object using FrameExample.this
                }
            });
            pack();
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
        }
        public static void main(String[] args) 
        {
            SwingUtilities.invokeLater( new Runnable()
            {
                @Override
                public void run()
                {
                    FrameExample fe = new FrameExample();
                    fe.createAndShowGUI();
                }
            });
        }
    }
    

    【讨论】:

      【解决方案4】:

      您应该使用JFrameClassName.this。因此,如果 JFrame 的名称是 MainWindow,您的代码将是:

      new ActionListener()
      {
          public void actionPerformed(ActionEvent e)
          {
              kMeans=new KMeans(mainWindow.table, Integer.parseInt(centroids.getText()), MainWindow.this);
          }
      }
      

      【讨论】:

        【解决方案5】:

        使用 ActionEvent 中的 getSource() 方法访问发生事件的对象。 示例:JMenuItem menuItem = (JMenuItem) e.getSource();

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-09-30
          • 1970-01-01
          • 2013-05-05
          • 1970-01-01
          • 2010-09-14
          • 1970-01-01
          • 2019-06-12
          • 2019-01-19
          相关资源
          最近更新 更多