gpt4 book ai didi

java - 使用 ActionMap 的 JFrame 键盘快捷键?

转载 作者:行者123 更新时间:2023-11-29 07:01:51 25 4
gpt4 key购买 nike

我正在尝试使用 ActionMap 和 InputMap 为我的 JFrame 创建快捷方式。但是还是做不了这个工作。我用 AbstractAction 创建了 ActionMap 来创建 Action ,在我创建了 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");

}

最佳答案

发布的代码看起来很合理,但我们不知道代码的使用上下文。例如,您是否将任何组件添加到内容 Pane 并且它们是否具有焦点。发布问题时发布 SSCCE这说明了问题,我们不必猜测您真正在做什么。

在框架级别处理 Action 时,我通常将键绑定(bind)添加到框架的 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());
}
}

编辑:

此示例还需要 Swing Utils类。

关于java - 使用 ActionMap 的 JFrame 键盘快捷键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24724883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com