【问题标题】:Method is not called inside the action listener method in java swingjava swing中的动作侦听器方法内部未调用方法
【发布时间】:2013-01-09 03:01:26
【问题描述】:

我必须在 java swing actionperformed 方法中调用一个方法。但是当我单击按钮时,什么也没有发生。如何解决这个问题?

       private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) 
   {
    hellocalled();
    }
    }

【问题讨论】:

  • 您必须将 ActionListener 添加到按钮中。喜欢button.addActionListener(yourActionListener);发布SSCCE
  • 是时候做一些调试了。您确定 JButton 已添加 ActionListener 吗?您是否在代码中添加了 println 语句以确保您知道什么被调用或未被调用?另外,请在此处发布代码时尝试更好地格式化您的代码。您的代码格式不正确且难以阅读,而且由于您是在向志愿者寻求免费帮助,因此我们要求您不要让帮助您变得过于困难,这并不过分。
  • 向我们展示hellocalled();方法的代码
  • sn-p 中的 5 个空行是什么意思?空行不会使损坏的代码工作。

标签: java swing actionlistener method-call


【解决方案1】:

您需要为您的按钮添加动作监听器以响应点击事件:

Button b = new Button();
b.addActionListener(new ActionListener(){

    public void actionPerformed(ActionEvent evt){
       jButton1ActionPerformed(evt);
       // call the method jButton1ActionPerformed
       // or you can call the one you have defined `hellocalled();` here
    } 
  }
}

【讨论】:

  • +1 这是一个相当大的信仰飞跃,但我敢打赌,它是正确的 ;)
  • 很遗憾原始发帖人从未更新他们的问题,我们不得不求助于信仰的飞跃。 1+
猜你喜欢
  • 1970-01-01
  • 2013-10-11
  • 1970-01-01
  • 2020-06-13
  • 2011-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-23
相关资源
最近更新 更多