2009-08-28 32 views
5

基本上我想要做的就是獲得一個啓動按鈕,以啓動在另一個類中運行的方法並在另一個對象上執行操作。Java:使用actionlistener在另一個類中調用該類中某個對象的函數

我對聽衆代碼:

button1a.addActionListener(new ActionListener() { 
    public void actionPerformed (ActionEvent event) { 
     // Figure out how to make this work 
     //sim.runCastleCrash(); 
    } 
}); 

我對其他類代碼:

public static void main(String[] args) { 
    CastleCrash sim; 
    sim = new CastleCrash(); 
} 

public void runCastleCrash() { 
    System.out.println("Castle Crash is beginning..."); 
    //Other method parts here to be added 
} 

給我的感覺,這不可能是太硬,但我錯過了一塊。

+0

你得到什麼錯誤?當您嘗試將actionListener添加到按鈕時,是否在範圍內有'sim'變量?一個典型的缺陷是變量需要是最終的,以便可以從匿名內部類(如ActionListener)訪問。 – 2009-08-28 13:36:03

+0

我得到的錯誤: 異常在線程「主要」 java.lang.Error的:未解決的問題,編譯: \t SIM解決不了 我認爲你是正確的,它與SIM卡的範圍不是一個問題,但我不知道如何使它最終.... – Myles 2009-08-28 13:41:34

+0

請參閱McDowell的答案,這就是我會回答。 – 2009-08-28 13:50:57

回答

3

一個來引用一個匿名類的東西的方法是使用final關鍵字:

public static void main(String[] args) { 
    final Object thingIWantToUse = "Hello"; 

    JButton button = new JButton("Click"); 
    button.addActionListener(new ActionListener() { 
     @Override public void actionPerformed(ActionEvent e) { 
     System.out.println(thingIWantToUse); 
     } 
    }); 

    JFrame frame = new JFrame(); 
    frame.setLayout(new FlowLayout()); 
    frame.add(button); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.pack(); 
    frame.setVisible(true); 
    } 

或者,可以訪問一個封閉類型的成員(變量或方法):

public class ActionListenerDemo2 { 
    private final JFrame frame = new JFrame(); 
    private Object thingIWantToUse = "Hello"; 

    public ActionListenerDemo2() { 
    JButton button = new JButton("Click"); 
    button.addActionListener(new ActionListener() { 
     @Override public void actionPerformed(ActionEvent e) { 
     thingIWantToUse = "Goodbye"; 
     System.out.println(thingIWantToUse); 
     } 
    }); 
    frame.setLayout(new FlowLayout()); 
    frame.add(button); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.pack(); 
    frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
    new ActionListenerDemo2().frame.setVisible(true); 
    } 
} 
+0

我不太明白你描述的第二個選項。如何訪問封閉類型的方法? – Myles 2009-08-28 13:54:01

+0

如果您向'AnctionListenerDemo2'添加了一個方法'doFoo()',那麼您可以在'actionPerformed'中調用它。 'actionPerformed'屬於接口'ActionListener'的匿名內部類實現。這裏介紹嵌套類:http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html – McDowell 2009-08-28 15:38:44

0

不知何故,您需要引用可從您的actionListener調用的CastleCrash對象。

您可能想要繼承JFrame,或者包含您的JButton的任何東西,這樣您的主方法和CastleCrash屬性就可以從您的匿名內部類Actionlistener中引用。

但是 - 要小心,你看起來像是在GUI事件線程(動作偵聽器將調用的地方)中調用什麼是一個長時間運行的方法。這通常是一個糟糕的主意,你會讓你的GUI變得沒有反應。

請參閱http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html特別是關於如何避免該問題的SwingWorker類的想法。

1

道爾已經很好地回答了關於如何從事件監聽器(或通常的匿名內部類)訪問變量的很好的例子。然而,a more general Sun resource on Event Listeners in Swing是規範的,並且對編寫它們時需要考慮的所有注意事項都有很好的概述。

2

我有像你一樣的問題,這就是我解決它的方法。你可以讓你的對象成爲最終的(最終CastleCrash sim = new CastleCrash();),但是我不想這樣做,或者你可以使得像setter方法那樣在你的其他類中運行該方法:

我對監聽器類代碼:

button1a.addActionListener(new ActionListener() 
{ 

    public void actionPerformed (ActionEvent event) 
    { 
    //How to make this work ? 
    //Like this: 
    runCC(); 
    } 
}); 

public void runCC() 
{ 
    CastleCrash sim = new CastleCrash(); 
    sim.runCastleCrash(); 
} 

我對其他類代碼:

public void runCastleCrash() 
{ 
    System.out.println("Castle Crash is beginning..."); 
    //Other method parts here to be added 
} 

希望這是有幫助的,祝你好運! :)

相關問題