WindowIconfield和FocusLost不能一起工作

时间:2014-06-09 00:20:29

标签: java swing jframe listener focuslistener

我需要在JFrame失去焦点时将鼠标设置在特定位置,但在最小化时则不需要。当帧失去焦点(Robot)时,我使用FocusListener通过启动定时器来设置鼠标位置,该定时器调用机器人来移动鼠标。当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状态。当状态改变时,我得到它在场状态。当Window停用时,我会询问状态是否为ICONFIED,然后给出说明。非常简单,但我整天都在折磨白衣。

1 个答案:

答案 0 :(得分:2)

您不需要添加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();
        }

    });
相关问题