繁体   English   中英

调用禁用的JButton的actionlistener

[英]Calling actionlistener of a disabled JButton

我正在做一些需要从另一个函数调用禁用的jbuttonactionlistner的事情。 怎么做?

制作一个新的方法,该方法将由禁用的jbutton调用,并在其中单击该按钮时将执行的所有代码都写入其中。 您不能以其他方式调用actionlistiner

...
JButton disButton = new JButton("Disabled");
disButton.addActionListener(new ActionListener() {
  @Override
  public void actionPerformed(ActionEvent arg0) {
    //do not write any statement here
    doSomething();
  }
});

...
private void doSomething() {
  //all action event execution code here
  System.out.println("I am in the action listener");
}

....

//in  the other method or another button click event call doSomething()
//even button is disables like
JButton Button = new JButton("Submit");
Button.addActionListener(new ActionListener() {
  @Override
  public void actionPerformed(ActionEvent arg0) {
    doSomething();
  }
});

//or from another method
public void method() {
  doSomething();
}

您不能disabled GUI控件上调用/执行actions ,这实际上是disable意思

您可以做的是创建一个单独的通用方法,如doClick()并在需要的地方调用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM