java - WindowIconfield 和 FocusLost 不能一起使用

标签 java swing jframe listener focuslistener

当 JFrame 失去焦点时,我需要将鼠标设置在特定位置,但在最小化时则不需要。当框架失去焦点(FocusListener)时,我使用 Robot 通过启动调用机器人移动鼠标的计时器来将鼠标设置在位置上。当 WindowListener 看到框架最小化时,它会停止计时器(因为它不会再调用机器人)。一切工作正常,除了机器人即使在最小化时仍在工作。通过获得焦点,机器人会停下来,一切都会顺利,但在最小化时却不会。

这是一个代码示例。谁能帮我吗?

public class Test extends JFrame {

Clipboard clipboard;
Robot robot;
Timer tmr;

private static final long serialVersionUID = 1L;

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
    public void run() {
        try {
            Test frame = new Test();
            frame.setVisible(true);
            frame.timer();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});
}
void timer(){
ActionListener actionListener = new ActionListener() {
    public void actionPerformed(ActionEvent actionEvent) {
        System.out.println("Test");
        StringSelection selection = new StringSelection("");
        clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(selection, selection);
    }
};
Timer tmr = new Timer(500, actionListener);
tmr.start();
}

public Test() throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {


addWindowListener(new WindowAdapter() {
    @Override
    public void windowIconified(WindowEvent arg0) {
        robot=null;
        tmr.stop();
    }
});
addFocusListener(new FocusAdapter() {
    @Override
    public void focusLost(FocusEvent arg0) {
        try {
            robot=new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }
        ActionListener actionListener = new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                robot.mouseMove(200, 200);
            }
        };
        tmr = new Timer(500, actionListener);
        tmr.start();
    }
    @Override
    public void focusGained(FocusEvent arg0) {
        robot=null;
        tmr.stop();
    }
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(100, 100, 431, 286);
getContentPane().setLayout(null);

}
}

FocusGained(工作正常)和windowIconfied(工作不正常)具有相同的事件句柄,但工作方式不同...

编辑:

public class Test extends JFrame {

Clipboard clipboard;
Robot robot;
Timer tmr;

private static final long serialVersionUID = 1L;

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
    public void run() {
        try {
            Test frame = new Test();
            frame.setVisible(true);
            frame.timer();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});
}
void timer(){
ActionListener actionListener = new ActionListener() {
    public void actionPerformed(ActionEvent actionEvent) {
        System.out.println("Test");
        StringSelection selection = new StringSelection("");
        clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(selection, selection);
    }
};
Timer tmr = new Timer(500, actionListener);
tmr.start();
}

void startRobot(){
    try {
        robot = new Robot();
    } catch (AWTException e) {
        e.printStackTrace();
    }
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println("Robot");
            //robot.mouseMove(200, 200);
        }
    };
    tmr = new Timer(500, actionListener);
    tmr.start();
}

public Test() throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
    startRobot();
        addWindowListener(new WindowAdapter() {

            @Override
            public void windowOpened(WindowEvent arg0) {
                tmr.stop();
            }

            @Override
            public void windowIconified(WindowEvent arg0) {
                tmr.stop();
            }

            @Override
            public void windowDeiconified(WindowEvent arg0) {
                tmr.stop();
            }

            @Override
            public void windowDeactivated(WindowEvent arg0) {
                tmr.start();
            }
            @Override
            public void windowActivated(WindowEvent arg0) {
                tmr.stop();
            }

            @Override
            public void windowClosing(WindowEvent arg0) {
                robot = null;
                tmr.stop();
            }

        });
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(100, 100, 431, 286);
getContentPane().setLayout(null);

}

}

我刚刚意识到,当我使用按钮最小化窗口时,它会启动计时器,而当通过单击任务栏程序图标最小化它时,它会停止正常。发生了什么事?

已解决:

我刚刚添加了 WindowStateListener 并创建了 int state 字段。当状态发生变化时,我将其置于现场状态。当窗口停用时,我询问状态是否为 ICONFIED,然后给出指示。非常简单,但我一整天都在折磨。

最佳答案

您不需要添加FocusListener,而您可以通过重写WindowListener的其他方法来实现它,您可以在窗口打开/激活/去图标时启动计时器并在窗口关闭/关闭/图标化时停止时间。

示例代码:

public TestWidnowMinimize() throws ClassNotFoundException, InstantiationException,
        IllegalAccessException, UnsupportedLookAndFeelException {

    addWindowListener(new WindowAdapter() {

        @Override
        public void windowOpened(WindowEvent arg0) {
            try {
                robot = new Robot();
            } catch (AWTException e) {
                e.printStackTrace();
            }
            ActionListener actionListener = new ActionListener() {
                public void actionPerformed(ActionEvent actionEvent) {
                    System.out.println("Robot");
                }
            };
            tmr = new Timer(500, actionListener);
            tmr.start();
        }

        @Override
        public void windowIconified(WindowEvent arg0) {
            tmr.stop();
        }

        @Override
        public void windowDeiconified(WindowEvent arg0) {
            tmr.start();
        }

        @Override
        public void windowDeactivated(WindowEvent arg0) {
            tmr.stop();
        }

        @Override
        public void windowClosing(WindowEvent arg0) {
            robot = null;
            tmr.stop();
        }

    });

关于java - WindowIconfield 和 FocusLost 不能一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24112208/

相关文章:

java - 如何从 Java JFrame 检查屏幕状态

java - 你最好的 Swing 设计模式和技巧是什么?

Java泛型通配符混淆

java - 更改 DatePicker 标题文本颜色 Android

java - 如果事件调度线程没有暂停,模式对话框如何等待?

java - JPanels : One with a JTextArea and another with a JLabel

java - JFrame我应该怎么做(包括图片)?

java - Java中如何读写XML文件并在保存时将注释节点视为文本节点

java - 关于带有 Action 监听器的 JComboBox 的问题

java - 切换JFrame但闪屏