2

我正在做一些我需要从另一个函数调用actionlistner禁用的东西。jbutton怎么做?

4

2 回答 2

5

创建一个将由禁用的 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();
}
于 2013-07-29T11:26:22.253 回答
4

您不能actions在GUI 控件上调用/执行操作。disabled这实际上是什么disable意思

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

于 2013-07-29T11:24:00.837 回答