在每個窗口/面板中,我都將Enter鍵映射爲defaultButton(意思是即使按下的按鈕沒有對焦,按下Enter鍵也會觸發按鈕按下)。我需要做的是將退出鍵映射到另一個按鍵,這也會觸發第二個按鍵,而不管焦點如何。對於回車鍵的代碼是:如何將「默認按鈕」功能添加到Swing中的兩個按鈕?
// We get the button
IButton iButton = (IButton)actionToolbar.getComponent(actionToolbar.getComponentCount() - 1);
// We get the window
Window window = SwingUtilities.getWindowAncestor(this);
// Only these two types of windows should have this behaviour
if (window instanceof JFrame) {
((JFrame)window).getRootPane().setDefaultButton(iButton);
}
else if (window instanceof JDialog) {
((JDialog)window).getRootPane().setDefaultButton(iButton);
}
現在我需要的是基本相同的代碼,但不斷變化與逃生輸入,或添加一個監聽器....東西,我不能確定什麼。
編輯:我必須在Java 1.4中做到這一點,我知道如果我立即說出它會很好。 (的SwingX項目)
可能使用'鍵Listeners'來解決這個問題。 –
想到了它並嘗試將它們映射到窗口/ rootPane,但它不起作用。可能只是我做錯了什麼。 – Andrija
@哈里noooooo - 忘記keyListeners存在;-) – kleopatra