在actionPerformed中引用此

发布于 2024-12-27 18:58:56 字数 387 浏览 3 评论 0原文

在主窗口的构造函数中,我使用以下简单代码创建了一个按钮:

JButton jbOptions = new JButton("Options");
buttonsPanel.add(jbOptions);
jbOptions.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e){
    new OptionsDialog();
  }
});

现在,我需要将对 JFrame 的引用传递给 OptionsDialog 的构造函数。怎么做呢? this 是对 ActionListener 的引用。

In the constructor of my main window I create a button with the following simple code:

JButton jbOptions = new JButton("Options");
buttonsPanel.add(jbOptions);
jbOptions.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e){
    new OptionsDialog();
  }
});

Now, I need to pass to the constructor of the OptionsDialog a reference to the JFrame. How to do that? this is a reference to ActionListener.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌︼了一个春 2025-01-03 18:58:56
new OptionsDialog(MyJFrame.this);

显然,MyJFrame 是封闭类的名称。

new OptionsDialog(MyJFrame.this);

MyJFrame being the name of the enclosing class, obviously.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文