Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在研究仅显示全屏颜色并循环显示其他颜色的代码。有一条线我一直盯着
getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false), "ESCAPE");
这允许您按“Esc”并退出它(我认为 getRootPane() 与 JButton 相关联?)。但是有没有更有效的方法来编码呢?可以拆分成多行吗?
你的意思是这样的:
InputMap inputMap = getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); KeyStroke escapeStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false); inputMap.put(escapeStroke, "Escape");
虽然这看起来更具可读性,但我认为这不会为当前实现增加任何效率。