gpt4 book ai didi

java - WindowIconfield 和 FocusLost 不能一起使用

转载 作者:行者123 更新时间:2023-11-30 03:52:21 27 4
gpt4 key购买 nike

当 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com