How to make command-w close windows on Java or clojure’s Mac OS

I want to close a window / JFrame in the program I wrote in clojure How will this be achieved? Pure Java solutions are also popular

Solution

This is a method:

Action closeWindow = new AbstractAction("Close Window") {
   @Override public void actionPerformed(ActionEvent e) {
     // window closing code here
   }
 };
 closeWindow.putValue(Action.ACCELERATOR_KEY,Keystroke.getKeystroke(
     KeyEvent.VK_W,Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));

Put this action on the menu on the menu The accelerator will be Ctrl w on windows

Using the keybinding API in each JFrame (assuming multiple) may better bind the main panel to the same keystroke in its (when_focused) input mapping, so that it can operate in the action mapping of closing the frame

public class ClosableWindow extends JFrame {
  public void setUp() {
    JPanel mainPanel = createMainPanel();

    int mask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
    Keystroke closeKey = Keystroke.getKeystroke(KeyEvent.VK_W,mask);

    mainPanel.getInputMap().put(closeKey,"closeWindow");        

    mainPanel.getActionMap().put("closeWindow",new AbstractAction("Close Window") {
          @Override public void actionPerformed(ActionEvent e) {
            setVisible(false);
            dispose();
          }
        });

    getContentPane().add(mainPanel);      
  }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>