2013-03-25 90 views
1

我有一個計算器應用程序,我無法使用setMnemonic。我試圖讓鍵盤上的按鍵與計算器上的按鈕鏈接。它一直告訴我<標識符>預計。這是我一直從功能http://docs.oracle.com/javase/tutorial/uiswing/components/button.html獲取信息的頁面。鍵盤按鈕和Jbutton

任何想法,我可以得到它的工作?

+1

請顯示完整的錯誤信息,並指出哪些行會導致錯誤發生。實際上,在上面的代碼中,我沒有看到對'setMnemonic(...)'的調用。 – 2013-03-25 01:37:18

+1

而不要比較這種'createEquasion!=「」'字符串,而是使用'「」.equals(...) – drzymala 2013-03-25 02:04:33

回答

3

您的問題與Swing或助記符無關,並且都與嘗試在方法或構造函數之外進行方法調用有關。你不能這樣做:

public class calculator_ui implements ActionListener { 
    /**Creates a new instance of the window "Buttons"*/ 
    JFrame frame = new JFrame("Buttons"); 

    // .... etc... 
    JButton buteq = new JButton("="); 
    JButton butclear = new JButton("C"); 
    butclear.setMnemonic(KeyEvent.VK_B); // .... **** this is misplaced 
    JButton back = new JButton("<"); 

而是將代碼行移動到你的類的構造函數中,它是合法的。

另外,設置助記符將設置按鈕將重新調用的alt-key組合。如果你想要更有趣,並讓按鈕響應按下非ALT數字鍵,那麼你會想要使用鍵綁定。