2013-07-29 25 views

回答

4

不能呼叫/做actionsdisabled GUI controls.That實際上是disable意味着什麼

你可以做的是創建一個單獨的常用方法類似doClick(),並呼籲在任何你需要的。

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(); 
} 
+0

+1爲實現:) –

+0

其實在創建的JButton我需要固定的某些參數在它的行動所進行的方法有哪些不是靜態的,所以我根本無法調用另一種方法 – avinash

+0

在被調用的函數中修復這些參數,或者將其從預先形成的操作中傳遞出來。 –