0

我已经开始在机器人框架的 swing 库中工作,在 swing 中测试 java 应用程序。

我必须从 jtextarea “showText” 上的 jpopupmenu “menu” 中选择 jcheckboxmenuitem “waveMenu”。运行关键字:

Select From Popup Menu      showText        menu|waveMenu

导致一个非常无用的错误。

Fail: Popup menu

如果我尝试右键单击组件 showText,它只会通过而不会显示与其关联的菜单。菜单由常规鼠标侦听器附加。

showText.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseReleased(MouseEvent e) {
            if (e.isPopupTrigger()) {
                menu.show(e.getComponent(), e.getX(), e.getY());
            }
        }
    });

从顶部开始在上下文中列出组件

Level: 0 Component: ca.acme.myApplication
Index: 0 Name: the name I select when I run Select Window
    Level: 1 Component: javax.swing.JRootPane Index: 0 Name: null
       Level: 2 Component: javax.swing.JPanel Index: 0 Name: null.glassPane
       Level: 2 Component: javax.swing.JLayeredPane Index: 0 Name: null.layeredPane
          Level: 3 Component: javax.swing.JPanel Index: 1 Name: null.contentPane
             Level: 4 Component: javax.swing.JPanel Index: 2 Name: null
                Level: 5 Component: javax.swing.JButton Index: 0 Name: fileOpenButton
                Level: 5 Component: javax.swing.JTextField Index: 0 Name: fileText
                Level: 5 Component: javax.swing.JButton Index: 1 Name: fileSendButton
                Level: 5 Component: javax.swing.JLabel Index: 0 Name: null
                Level: 5 Component: javax.swing.JTextField Index: 1 Name: hostText
                Level: 5 Component: javax.swing.JComboBox Index: 0 Name: formatSelect
                   Level: 6 Component: javax.swing.plaf.metal.MetalComboBoxButton Index: 2 Name: null
                   Level: 6 Component: javax.swing.CellRendererPane Index: 0 Name: null
                      Level: 7 Component: javax.swing.plaf.basic.BasicComboBoxRenderer$UIResource Index: 1 Name: null
                Level: 5 Component: javax.swing.JComboBox Index: 1 Name: voiceSelect
                   Level: 6 Component: javax.swing.plaf.metal.MetalComboBoxButton Index: 3 Name: null
                   Level: 6 Component: javax.swing.CellRendererPane Index: 1 Name: null
                      Level: 7 Component: javax.swing.plaf.basic.BasicComboBoxRenderer$UIResource Index: 2 Name: null
                Level: 5 Component: javax.swing.JComboBox Index: 2 Name: delaySelect
                   Level: 6 Component: javax.swing.plaf.metal.MetalComboBoxButton Index: 4 Name: null
                   Level: 6 Component: javax.swing.CellRendererPane Index: 2 Name: null
                      Level: 7 Component: javax.swing.plaf.basic.BasicComboBoxRenderer$UIResource Index: 3 Name: null
                Level: 5 Component: javax.swing.JTextField Index: 2 Name: messageText
                Level: 5 Component: javax.swing.JButton Index: 5 Name: messageSendButton
                Level: 5 Component: javax.swing.JLabel Index: 4 Name: portLabel
                Level: 5 Component: javax.swing.JTextField Index: 3 Name: portText
                Level: 5 Component: javax.swing.JComboBox Index: 3 Name: requestSelect
                   Level: 6 Component: javax.swing.plaf.metal.MetalComboBoxButton Index: 6 Name: null
                   Level: 6 Component: javax.swing.CellRendererPane Index: 3 Name: null
                      Level: 7 Component: javax.swing.plaf.basic.BasicComboBoxRenderer$UIResource Index: 5 Name: null
                Level: 5 Component: javax.swing.JComboBox Index: 4 Name: speedSelect
                   Level: 6 Component: javax.swing.plaf.metal.MetalComboBoxButton Index: 7 Name: null
                   Level: 6 Component: javax.swing.CellRendererPane Index: 4 Name: null
                      Level: 7 Component: javax.swing.plaf.basic.BasicComboBoxRenderer$UIResource Index: 6 Name: null
                Level: 5 Component: javax.swing.JComboBox Index: 5 Name: siteSelect
                   Level: 6 Component: javax.swing.plaf.metal.MetalComboBoxButton Index: 8 Name: null
                   Level: 6 Component: javax.swing.CellRendererPane Index: 5 Name: null
                      Level: 7 Component: javax.swing.plaf.basic.BasicComboBoxRenderer$UIResource Index: 7 Name: null
                Level: 5 Component: javax.swing.JScrollPane Index: 0 Name: null
                   Level: 6 Component: javax.swing.JViewport Index: 0 Name: null
                      Level: 7 Component: javax.swing.JTextArea Index: 4 Name: showText
                   Level: 6 Component: javax.swing.JScrollPane$ScrollBar Index: 0 Name: null
                      Level: 7 Component: javax.swing.plaf.metal.MetalScrollButton Index: 9 Name: null
                      Level: 7 Component: javax.swing.plaf.metal.MetalScrollButton Index: 10 Name: null
                   Level: 6 Component: javax.swing.JScrollPane$ScrollBar Index: 1 Name: null
                      Level: 7 Component: javax.swing.plaf.metal.MetalScrollButton Index: 11 Name: null
                      Level: 7 Component: javax.swing.plaf.metal.MetalScrollButton Index: 12 Name: null

降低一个级别并尝试与应用程序进行最基本的交互也会导致失败

如果需要,我不介意提供更多细节。我多年来一直被这个问题困扰。

4

1 回答 1

0

我发现了这个问题。

if (e.isPopupTrigger()) {
            menu.show(e.getComponent(), e.getX(), e.getY());
        }

Java 文档:

Returns whether or not this mouse event is the popup menu trigger event for the platform. 

我认为 windows 弹出菜单触发器与机器人框架摇摆库发送的触发器不同。右键单击通过的组件并没有执行任何操作,因为它确实通过了;它只是被 if 语句否定了。

于 2018-03-09T21:04:00.637 回答