简体   繁体   English

显示两个非模式JDialogs后,JMenuItem加速器不起作用? (仅限Mac?)

[英]JMenuItem accelerator not working after showing two non-modal JDialogs? (Mac only?)

I have the problem that the accelerators of JMenuItems aren't working anymore after showing two JDialogs directly after one another. 我有一个问题,就是在紧接着直接显示两个JDialogs之后,JMenuItems的加速器不再工作了。

Please take a look at this small example that reproduces the problem: 请看一下这个重现问题的小例子:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class DialogBug
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new StartupRunnable(args.length == 0));
    }

    public static class StartupRunnable
        implements Runnable
    {
        private boolean both;

        public StartupRunnable(boolean both)
        {
            this.both=both;
        }

        public void run()
        {
            MyFrame myFrame=new MyFrame();
            myFrame.setVisible(true);
            myFrame.startUp(both);
        }
    }

    public static class MyFrame
        extends JFrame
    {
        private MyDialog dialog1;
        private MyDialog dialog2;

        public MyFrame()
        {
            super("MyFrame");
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            initUI();
        }

        private void initUI()
        {
            dialog1=new MyDialog(this);
            dialog2=new MyDialog(this);
            JMenuBar menuBar=new JMenuBar();
            JMenu fileMenu=new JMenu("File");
            menuBar.add(fileMenu);
            fileMenu.add(new JMenuItem(new OpenAction()));
            setJMenuBar(menuBar);
            setSize(200,200);
        }

        public void startUp(boolean both)
        {
            dialog1.setVisible(true);
            if(both)
            {
                dialog2.setVisible(true);
            }
        }

        private class OpenAction
            extends AbstractAction
        {
            public OpenAction()
            {
                super("Open");
                KeyStroke accelerator = KeyStroke.getKeyStroke("ctrl O");
                putValue(Action.ACCELERATOR_KEY, accelerator);
            }

            public void actionPerformed(ActionEvent e)
            {
                System.out.println("Open executed");
            }
        }

    }

    public static class MyDialog
        extends JDialog
    {
        public MyDialog(JFrame parent)
        {
            super(parent);
            setTitle("Dialog");
            setModal(false);
            add(new JButton(new OkAction()));
            pack();
        }

        private class OkAction
            extends AbstractAction
        {
            public OkAction()
            {
                super("Ok");
            }

            public void actionPerformed(ActionEvent e)
            {
                setVisible(false);
            }
        }
    }
}

Compile it using javac DialogBug.java and execute it running java DialogBug . 使用javac DialogBug.java对其进行javac DialogBug.java ,然后运行java DialogBug执行。

You'll see two with "Ok"-Buttons. 您会看到两个带有“确定”按钮的按钮。 Dismiss both dialogs clicking them. 关闭两个对话框,单击它们。 Now press "Ctrl-O". 现在按“ Ctrl-O”。 This should print "Open executed" to the console but this won't happen. 这应该在控制台上显示“已执行打开”,但这不会发生。 Now click the "File" menu. 现在,单击“文件”菜单。 Now "Ctrl-O" will work as expected. 现在,“ Ctrl-O”将按预期工作。

If you start the app with any argument, eg java DialogBug x then only one dialog will open and "Ctrl-O" will work immediately after dismissing the dialog, as expected. 如果您使用任何参数(例如java DialogBug x启动应用程序,则仅打开一个对话框,并且按预期在关闭对话框后将立即运行“ Ctrl-O”。

My environment is the following: 我的环境如下:

java version "1.6.0_15"
Java(TM) SE Runtime Environment (build 1.6.0_15-b03-219)
Java HotSpot(TM) 64-Bit Server VM (build 14.1-b02-90, mixed mode)

But as far as I know this app behaves similar in other VMs. 但据我所知,此应用程序在其他VM中的行为类似。

Please help me! 请帮我!
I really don't have any idea what might go wrong here. 我真的不知道这里可能出什么问题。 If this is a Java bug (and I expect it to be one) then please let me know if you have a workaround... 如果这是一个Java错误(我希望它是一个),请告诉我您是否有解决方法...

If you can or can't reproduce this on other systems please tell me so in the comments. 如果您可以或不能在其他系统上重现此内容,请在评论中告诉我。
Thanks a lot! 非常感谢!

Update 更新资料
After installing Java 1.5 on Snow Leopard ( sigh ) I can confirm that this happens with 1.5.0_19, too, at least on Snow Leopard. Snow Leopard上安装Java 1.5之后(可以叹息 ),我可以确认至少在Snow Leopard上,这也发生在1.5.0_19上。

Update 2 更新2
Works for me on Windows XP. 在Windows XP上对我有效。

java version "1.6.0_13"
Java(TM) SE Runtime Environment (build 1.6.0_13-b03)
Java HotSpot(TM) Client VM (build 11.3-b02, mixed mode, sharing)

java version "1.5.0_13"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_13-b05)
Java HotSpot(TM) Client VM (build 1.5.0_13-b05, mixed mode)

Update 3 更新3
Seems to work on Windows Vista, too. 似乎也可以在Windows Vista上使用。 This makes this a Mac OS X 10.5+10.6 issue, AFAIK so far. 到目前为止,这已成为Mac OS X 10.5 + 10.6问题,即AFAIK。

Update 4 更新4
This bug is filed under Problem ID #7240026 at Apple. 此错误已存在于Apple的问题ID#7240026下。

Replace the actionPerformed method of the OkAction class in the MyDialog class with the following: 将MyDialog类中OkAction类的actionPerformed方法替换为以下内容:

public void actionPerformed(ActionEvent e) {
            setVisible(false);
            MyDialog.this.getParent().requestFocus();
        }

For some reason (maybe because the dialogs aren't modal), OS X is not returning the focus to your MyFrame. 由于某种原因(可能是因为对话框不是模态对话框),OS X并未将焦点返回到MyFrame。 The focus returns to MyFrame naturally if you dismiss the dialogs by closing them, but there might be something with simply hiding the dialogs. 如果通过关闭对话框来关闭对话框,焦点自然会返回到MyFrame,但是可能存在一些隐藏对话框的情况。

Works fine for me on XP using Java(TM) SE Runtime Environment (build 1.6.0_07-b06). 使用Java™SE Runtime Environment(内部版本1.6.0_07-b06)在XP上对我来说工作正常。

For some reason I seem to think that Ctrl+O is the accelerator for changing a components orientation. 由于某种原因,我似乎认为Ctrl + O是更改组件方向的加速器。 Just wondering if this happens with all accelerators or only Ctrl+O? 只是想知道所有加速器还是仅Ctrl + O都会发生这种情况?

Edit: you can probably forget this suggestion. 编辑:您可能会忘记这个建议。 I can't seem to find/recall where I may have thought this and and can't verify this in any of my test programs. 我似乎无法找到/回想起我可能曾经想到过的地方,也无法在我的任何测试程序中对此进行验证。

I ran it without problem on my mac that has OS X 10.4 and Java version "1.5.0_19". 我在具有OS X 10.4和Java版本“ 1.5.0_19”的Mac上毫无问题地运行了它。

I'm wondering if after you close the two modal dialogs the main frame has the focus. 我想知道在关闭两个模式对话框之后,是否将主框架放在焦点上。 I have not used accelerators in a long while, but I think that maybe there has been some recent change regarding accelerators registered in the menu bar, and accelerators registered in the frame (like shortcuts). 我已经很长时间没有使用加速器了,但是我认为也许最近对菜单栏中注册的加速器和框架中注册的加速器(如快捷方式)进行了一些更改。

Good luck with it. 祝你好运。

I've tried on Leopard/10.5 and the program does not work with either: 我在Leopard / 10.5上尝试过,但该程序不适用于以下任何一个:

Java 5 (Apple JVM) Java 5(Apple JVM)

java version "1.5.0_20"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_20-b02-315)
Java HotSpot(TM) Client VM (build 1.5.0_20-141, mixed mode, sharing)

Java 6 (SoyLatte/Open JDK BSD Port) Java 6(SoyLatte / Open JDK BSD端口)

java version "1.6.0_03-p3"
Java(TM) SE Runtime Environment (build 1.6.0_03-p3-landonf_19_aug_2008_14_55-b00)
Java HotSpot(TM) Server VM (build 1.6.0_03-p3-landonf_19_aug_2008_14_55-b00, mixed mode)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM