2013-06-27 11 views
0

在我的應用程序退出時,我喜歡提示一下JOptionPane以確保用戶想要退出。 我已經使用JMenuItem實現了退出行爲,並且還在MenuItem中實現了該功能,該功能在Cliking在TrayIcon時彈出;以及鍵入ALT + F4或關閉主窗口時。所以,所有的退出處理都是通過包含必要的actionPerformed方法的Action完成的。 另外,我喜歡應用程序退出時沒有任何JOptionPane如果SHIFT鍵被按下;所以我已經把這段代碼片段做到了這一點。使用修飾符實例化ActionEvent(如果有的話)

if ((e.getModifiers() & ActionEvent.SHIFT_MASK) == ActionEvent.SHIFT_MASK) { 
     abandonApplication(); 
    } 

我現在面臨關閉我需要實例隨修飾一個ActionEvent窗口時,我不知道如何做到這一點的問題。總之,我需要的是這種

private void formWindowClosing(java.awt.event.WindowEvent evt) {         
    ExitAction.getInstance().actionPerformed(new ActionEvent(evt,ActionEvent.ACTION_PERFORMED, "close", modifiers)); 
}         

以一種方式,修飾符包含任何已被按下的修飾鍵(如果有的話)。

有人可以幫助我嗎?

回答

0

流連於互聯網我找到了適合我需求的解決方案。它如下所示:

我定義了一個用於包含修飾符的int變量。然後,我註冊以這樣的方式按了一個鍵釋放聽衆的關鍵,以下做:

private void formKeyPressed(java.awt.event.KeyEvent evt) {         
    if (evt.getKeyCode() == KeyEvent.VK_SHIFT) { 
     modifiers = evt.getModifiers(); 
    } 
    return; 
} 

private void formKeyReleased(java.awt.event.KeyEvent evt) {         
    if (evt.getKeyCode() == KeyEvent.VK_SHIFT) { 
     modifiers = evt.getModifiers(); 
    } 
    return; 
} 

所以,調用ExitAction當我有需要的改性劑和退出操作在任何條件下成功地完成(菜單項,系統托盤,ALT-F4)。

我很感激任何其他解決方案。

0

另外,在我看來更好的解決方案,我發現是在考慮使用鍵綁定。此方法通知用於包含修飾符的int變量。我認爲是不言自明的。

/** 
* Describes what is going to be done when SHIFT key is pressed and released 
* while the JFrame that has the focus. 
* <p> 
* When the action pressed SHIFT is detected we get the modifiers and 
* immediately disable it in order to avoid useless action executions while 
* SHIFT key is being pressed. Alternively, when released SHIFT is detected 
* we enable pressing SHIFT action again. 
* <p> 
* In this case we get the modifiers to pass them to the action done when 
* window closign event is detected to check if the SHIFT key is pressed to 
* force exiting of the application without prompting anything. 
*/ 
private void shiftKeyOperation() { 
    final Action shiftPressedAction = new AbstractAction() { 
     @Override 
     public void actionPerformed(ActionEvent ae) { 
      modifiers = ae.getModifiers(); 
      setEnabled(false); 
     } 
    }; 

    final Action shiftReleasedAction = new AbstractAction() { 
     @Override 
     public void actionPerformed(ActionEvent ae) { 
      modifiers = ae.getModifiers(); 
      shiftPressedAction.setEnabled(true); 
     } 
    }; 
    JRootPane jRootPane = getRootPane(); //here we get the RootFrame from the JFrame 
    ActionMap actionMap = jRootPane.getActionMap(); 
    InputMap inputMap = jRootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); 
    final String SHIFT_KEY_PRESSED_ACTION = "shift_key_pressed"; 
    actionMap.put(SHIFT_KEY_PRESSED_ACTION, shiftPressedAction); 
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_SHIFT, InputEvent.SHIFT_DOWN_MASK, false), SHIFT_KEY_PRESSED_ACTION); 
    final String SHIFT_KEY_RELEASED_ACTION = "shift_key_released"; 
    actionMap.put(SHIFT_KEY_RELEASED_ACTION, shiftReleasedAction); 
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_SHIFT, 0, true), SHIFT_KEY_RELEASED_ACTION); 
    return; 
}