Fer*_*iva 1 java swing netbeans jframe
我正在尝试使用 ActionMap 和 InputMap 为我的 JFrame 创建快捷方式。但还是做不到这个工作。我使用 AbstractAction 创建了 ActionMap 来创建操作,并在创建了 InputMap 来注册事件之后,但不起作用
private void acoesTela(){
JPanel painel = (JPanel)this.getContentPane();
ActionMap actionMap = painel.getActionMap();
actionMap.put("consultaProdutos", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent evt) {
System.out.println("F3 is pressed");
}
});
/** registra acoes */
InputMap imap = painel.getInputMap(JPanel.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
imap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0), "consultaProdutos");
}
Run Code Online (Sandbox Code Playgroud)
发布的代码看起来很合理,但我们不知道如何使用该代码的上下文。例如,您是否向内容窗格添加了任何组件以及它们是否具有焦点。当发布问题并通过SSCCE向我们展示问题时,我们不必猜测您真正在做什么。
在帧级别处理操作时,我通常将键绑定添加到JRootPane帧的。这是SSCCE演示这种方法的一个:
import java.awt.*;
import java.awt.event.*;
import java.util.List;
import javax.swing.*;
import javax.swing.event.*;
/**
** This class will close a JDialog (or a window) when the Escape key is used.
** However, first it must check to see if a popup component is visible in
** which case the Escape key will close the popup normally, then you must use
** the Escape key a second time to close the dialog.
*/
public class EscapeAction extends AbstractAction
{
public void actionPerformed(ActionEvent e)
{
boolean visiblePopup = false;
Component c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
// Check if light weight popup is being used
List<JPopupMenu> popups = SwingUtils.getDescendantsOfType(JPopupMenu.class, (Container)c, true);
for (JPopupMenu p: popups)
{
p.setVisible( false );
visiblePopup = true;
}
// Check if a heavy weight popup is being used
Window window = SwingUtilities.windowForComponent(c);
for (Window ownedWindow: window.getOwnedWindows())
{
if (ownedWindow.isVisible())
{
Component rootPane = ownedWindow.getComponent(0);
List<JPopupMenu> ownedPopups =
SwingUtils.getDescendantsOfType(JPopupMenu.class, (Container)rootPane, true);
for (JPopupMenu ownedPopup: ownedPopups)
{
ownedPopup.setVisible( false );
visiblePopup = true;
ownedWindow.dispose();
}
}
}
// No popups so close the Window
if (! visiblePopup)
//SwingUtilities.windowForComponent(c).setVisible(false);
SwingUtilities.windowForComponent(c).dispose();
}
public static void main(String[] args)
{
String laf = null;
laf = "javax.swing.plaf.metal.MetalLookAndFeel";
// laf = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
// laf = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
try { UIManager.setLookAndFeel(laf); }
catch (Exception e2) { System.out.println(e2); }
JDialog dialog = new DialogEscape();
JPopupMenu popup = new JPopupMenu();
popup.add( new JMenuItem("SubMenuA") );
popup.add( new JMenuItem("SubMenuB") );
popup.add( new JMenuItem("SubMenuC") );
popup.add( new JMenuItem("SubMenuD") );
String[] items = { "Select Item", "Color", "Shape", "Fruit" };
JComboBox<String> comboBox = new JComboBox<String>( items );
dialog.add(comboBox, BorderLayout.NORTH);
JTextField textField = new JTextField("Right Click For Popup");
textField.setComponentPopupMenu(popup);
dialog.add(textField);
dialog.setDefaultCloseOperation( JDialog.DISPOSE_ON_CLOSE );
dialog.setSize(200, 200);
dialog.setLocationRelativeTo(null);
dialog.setVisible( true );
// Add the Key Bindings to the JRootPane for the EscapeAction
JRootPane rootPane = dialog.getRootPane();
String escapeText = "ESCAPE";
KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(escapeText);
rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeKeyStroke, escapeText);
rootPane.getActionMap().put(escapeText, new EscapeAction());
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:
此示例还需要Swing Utils类。