Jpopup 不适用于 ESc 键

发布于 2024-09-09 00:27:52 字数 1044 浏览 9 评论 0原文

一般来说,Esc 键用于隐藏菜单。但就我而言,我必须在单击 Esc 键时显示菜单。 我有一个组合 我正在执行以下操作

public class MyFrame extends JFrame implements KeyListener{

JPopupMenu menu = new JPopupMenu();
JTextField txt = new JTextField("TestField1");
JTextField txt1 = new JTextField("TestField2");
public MyFrame(){
    init();
}
private void init(){

    setLayout(new BorderLayout());
    txt.addKeyListener(this);
    add( txt,BorderLayout.WEST);
    add(txt1,BorderLayout.CENTER);
    pack();
    setVisible(true);

}
@Override
public void keyPressed(KeyEvent e) {

    System.out.println("keypressed");

    menu = new JPopupMenu();
    menu.add("item1");
    menu.add("item2");
    menu.show(e.getComponent(),e.getComponent().getX(),e.getComponent().getY());
}

@Override
public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub

}

public static void main(String args[]){
    new MyFrame();
}

}

这对于我测试的所有键(Esc 键除外)都适用。我怎样才能启用它?

in general the Esc key is used to hide the menu.. but in my case i have to show a menu on click of an Esc key.
I have a combo
I am doing the following

public class MyFrame extends JFrame implements KeyListener{

JPopupMenu menu = new JPopupMenu();
JTextField txt = new JTextField("TestField1");
JTextField txt1 = new JTextField("TestField2");
public MyFrame(){
    init();
}
private void init(){

    setLayout(new BorderLayout());
    txt.addKeyListener(this);
    add( txt,BorderLayout.WEST);
    add(txt1,BorderLayout.CENTER);
    pack();
    setVisible(true);

}
@Override
public void keyPressed(KeyEvent e) {

    System.out.println("keypressed");

    menu = new JPopupMenu();
    menu.add("item1");
    menu.add("item2");
    menu.show(e.getComponent(),e.getComponent().getX(),e.getComponent().getY());
}

@Override
public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub

}

public static void main(String args[]){
    new MyFrame();
}

}

This works fine for all the keys i tested except for Esc key. How can i enable it ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

大海や 2024-09-16 00:27:52

它几乎就像 Escape 键也被转发到菜单,因此一旦打开它就会自动关闭。

无论如何,正确的方法是使用键绑定,而不是 KeyListener。阅读我关于按键绑定的介绍,使用链接中的建议你的代码是:

Action action = new AbstractAction()
{
    public void actionPerformed(ActionEvent e)
    {
        menu = new JPopupMenu();
        menu.add("item1");
        menu.add("item2");
        Component component = (Component)e.getSource();
        menu.show(component, component.getX(), component.getY());
    }
};
String keyStrokeAndKey = "ESCAPE";
KeyStroke keyStroke = KeyStroke.getKeyStroke(keyStrokeAndKey);
txt.getInputMap().put(keyStroke, keyStrokeAndKey);
txt.getActionMap().put(keyStrokeAndKey, action);

Its almost like the Escape key is also being forwarded to the menu so it closes automatically as soon as it is opened.

Anyway, the proper way to do this is to use Key Bindings, NOT a KeyListener. Read my intro on Key Bindings, Using the suggestion from the link your code would be:

Action action = new AbstractAction()
{
    public void actionPerformed(ActionEvent e)
    {
        menu = new JPopupMenu();
        menu.add("item1");
        menu.add("item2");
        Component component = (Component)e.getSource();
        menu.show(component, component.getX(), component.getY());
    }
};
String keyStrokeAndKey = "ESCAPE";
KeyStroke keyStroke = KeyStroke.getKeyStroke(keyStrokeAndKey);
txt.getInputMap().put(keyStroke, keyStrokeAndKey);
txt.getActionMap().put(keyStrokeAndKey, action);
聆听风音 2024-09-16 00:27:52

只需使用 KeyEvent.VK_ESCAPE:

@Override
public void keyPressed(KeyEvent e) {
    System.out.println("keypressed");
    Component c = e.getComponent();
    if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
        e.consume();
    }
    menu.show(c, c.getX(), c.getY());
}

Just consume the KeyEvent.VK_ESCAPE:

@Override
public void keyPressed(KeyEvent e) {
    System.out.println("keypressed");
    Component c = e.getComponent();
    if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
        e.consume();
    }
    menu.show(c, c.getX(), c.getY());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文