【问题标题】:java.awt.Robot : hit mnemonics key not workingjava.awt.Robot:点击助记键不起作用
【发布时间】:2015-08-05 07:27:19
【问题描述】:

我创建了一个简单的程序来使用 awt.robot,它应该按 ALT+F 并打开一个菜单项。但它不起作用。 最初我认为焦点问题。然后我添加了一个虚拟按钮来获得焦点,但 id 仍然不起作用。

请看代码

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

class MenuActionListener implements ActionListener {

    public void actionPerformed(ActionEvent actionEvent) {

        System.out.println("Selected: " + actionEvent.getActionCommand());

    }
}

public class MenuAction {

    public static void main(final String args[]) throws AWTException {

        ActionListener menuListener = new MenuActionListener();

        JFrame frame = new JFrame("MenuSample Example");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JMenuBar menuBar = new JMenuBar();

        // File Menu, F - Mnemonic
        JMenu fileMenu = new JMenu("File");
        fileMenu.setMnemonic(KeyEvent.VK_F);
        menuBar.add(fileMenu);

        // File->New, N - Mnemonic
        JMenuItem newMenuItem = new JMenuItem("New", KeyEvent.VK_N);
        newMenuItem.addActionListener(menuListener);
        fileMenu.add(newMenuItem);

        frame.setJMenuBar(menuBar);
        frame.setSize(650, 650);

        JButton button = new JButton("Test");
        frame.getContentPane().add("South", button);
        frame.setBounds(100, 100, 400, 400);
        frame.setVisible(true);

        button.requestFocusInWindow();

        Robot robot = new Robot();

        robot.setAutoDelay(500);

        robot.waitForIdle();

        robot.keyPress(KeyEvent.ALT_MASK);

        robot.keyPress(KeyEvent.VK_F);
        robot.keyRelease(KeyEvent.VK_F);
        robot.keyRelease(KeyEvent.ALT_MASK);
        robot.waitForIdle();
    }
}

【问题讨论】:

    标签: java swing awtrobot mnemonics


    【解决方案1】:

    两件事,首先,您应该使用KeyEvent.VK_ALT 而不是KeyEvent.ALT_MASK,其次,当您执行Robot 代码时,窗口可能不可见/不活动,您需要等到窗口变得可见即使那样,您可能需要在将来的某个时间执行任务以确保操作系统已使窗口可见,例如...

    import java.awt.AWTException;
    import java.awt.Robot;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.InputEvent;
    import java.awt.event.KeyEvent;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.SwingUtilities;
    
    public class MenuAction {
    
        public static void main(final String args[]) throws AWTException {
    
            ActionListener menuListener = new MenuActionListener();
    
            JFrame frame = new JFrame("MenuSample Example");
    
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JMenuBar menuBar = new JMenuBar();
    
            // File Menu, F - Mnemonic
            JMenu fileMenu = new JMenu("File");
            fileMenu.setMnemonic(KeyEvent.VK_F);
            menuBar.add(fileMenu);
    
            // File->New, N - Mnemonic
            JMenuItem newMenuItem = new JMenuItem("New", KeyEvent.VK_N);
            newMenuItem.addActionListener(menuListener);
            fileMenu.add(newMenuItem);
    
            frame.setJMenuBar(menuBar);
            frame.setSize(650, 650);
    
            JButton button = new JButton("Test");
            frame.getContentPane().add("South", button);
            frame.setBounds(100, 100, 400, 400);
            frame.addWindowListener(new WindowAdapter() {
    
                @Override
                public void windowOpened(WindowEvent e) {
                    button.requestFocusInWindow();
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
    
                            try {
                                Robot robot = new Robot();
    
                                robot.setAutoDelay(500);
    
                                robot.waitForIdle();
    
                                robot.keyPress(KeyEvent.VK_ALT);
    
                                robot.keyPress(KeyEvent.VK_F);
                                robot.keyRelease(KeyEvent.VK_F);
                                robot.keyRelease(KeyEvent.VK_ALT);
                                robot.waitForIdle();
                            } catch (AWTException exp) {
                                exp.printStackTrace();
                            }
                        }
                    }).start();
                }
            });
            frame.setVisible(true);
        }
    
        public static class MenuActionListener implements ActionListener {
    
            public void actionPerformed(ActionEvent actionEvent) {
    
                System.out.println("Selected: " + actionEvent.getActionCommand());
    
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-22
      • 1970-01-01
      • 1970-01-01
      • 2015-09-24
      • 2015-05-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多