主题:到底是什么让他们感到困惑?两个带有鼠标侦听器的 Runnable

发布于 2024-08-25 21:26:33 字数 1888 浏览 5 评论 0原文

我有一个 JWindow 和一个 JFrame ,它们都可以运行,并且都实现了鼠标侦听器。 我有很多测试要做,为了我的一个项目,为了简化它,我希望能够自动化其中的大部分,所以我启动了我自己的鼠标记录器和重放器(使用 Java 机器人类)。

有点像简化的 AutoHotKey 或 AutoIt 东西...但它可以在我的 Ubuntu 机器以及我的 Windows 机器上运行!!!

我制作的 JWindow 是半透明的,覆盖整个屏幕,当你单击它,它会消失并重播单击后面的对象,然后重新出现。这就是录制过程。当用户右键单击时,设置为不可见,并重播记录的操作。

在重播期间,我希望该选项能够退出整个应用程序,因此我认为最好的方法是使 JFrame 和 JWindow 可运行。

JFrame 只是为应用程序提供了一个关闭选项。

因此,在我的 Main 类中,我

public static void Main(String[] args){
    recorder = new Recorder();
    gui = new GUI();
    Thread tr = new Thread(recorder);
    Thread tg = new Thread(gui);
    tr.setName("Recorder");
    tg.setName("GUI");
    tr.start();
    tg.start();
}

的理解是 Recorder 和 GUI 是可运行对象,它们通过新的 Thread 命令制成线程。当我使用 .start() 时,我开始执行线程,从这里系统决定在任何特定时间运行哪个线程。

进入 Recorder 和 GUI 类。

public class Recorder
        implements Runnable, MouseListener {

//Constructor and other code

    public void mouseClicked(MouseEvent e) {

        if (e.getButton() == MouseEvent.BUTTON1) {
             //Record events
        }else{
             //Replay events
        }
        System.exit(0);
    }

    public void run() {
        System.out.println("Recorder");
    }
}

public class GUI 
    implements Runnable, MouseListener {

//Constructor, simply constructs JFrame and sets mouselistener to it

   public void mouseClicked(MouseEvent e) {
       System.exit(0);
   }

   public void run() {
        System.out.println("GUI");
   }

}

我的应用程序打印出 Recorder,然后打印出 GUI 允许我记录我的事件 然后右键单击 JWindow 来重播它们...

但是当我单击 JFrame 的关闭按钮或什至由于鼠标侦听器而在框架中单击时,它不会退出,直到所有操作都完全重播为止?

我确实想知道的一件事是,我在运行中放入的是什么使线程保持运行?所以当 System.out.println("");执行完线程就死掉了?所以我尝试了一段 while 循环,我的应用程序成功打印了

GUI 图形用户界面 图形用户界面 录音机 录音机 图形用户界面 录音机 ETC 等等

所以我可以看到它们的线程正在同时运行...只是运行之外的所有其他操作似乎都没有被执行...我如何在线程执行中包含我的鼠标侦听器等?

I have a JWindow and a JFrame both I have made runnable and both implement a mouse listener.
I have alot of testing to do for a project of mine and to simplify it I wish to be able to automate most of it, so I have starting my own mouse recorder and replayer (uses Java Robot Class).

Kinda like a simplified AutoHotKey or AutoIt thing... but it'll run on my Ubuntu machine aswell as my Windows one!!!

The JWindow I have made is translucent and covers the entire screen, when you click on it it disappears and replays the click to the object behind and then reappears. This is the recording process. When the User right clicks the is set to not visible and the recorded actions are replayed.

During replay I want the option to be able to exit the entire application and so I though the best way to do this would be to make the JFrame and the JWindow Runnable.

The JFrame is simply their to offer a close option from the application.

So, in my Main class I have

public static void Main(String[] args){
    recorder = new Recorder();
    gui = new GUI();
    Thread tr = new Thread(recorder);
    Thread tg = new Thread(gui);
    tr.setName("Recorder");
    tg.setName("GUI");
    tr.start();
    tg.start();
}

My understanding is Recorder and GUI are runnable objects and they are made into threads via the new Thread command. When I use .start() I am beginning the execution of the thread and from here on the system decides which thread is running at any particular time.

Onto the Recorder and GUI classes.

public class Recorder
        implements Runnable, MouseListener {

//Constructor and other code

    public void mouseClicked(MouseEvent e) {

        if (e.getButton() == MouseEvent.BUTTON1) {
             //Record events
        }else{
             //Replay events
        }
        System.exit(0);
    }

    public void run() {
        System.out.println("Recorder");
    }
}

public class GUI 
    implements Runnable, MouseListener {

//Constructor, simply constructs JFrame and sets mouselistener to it

   public void mouseClicked(MouseEvent e) {
       System.exit(0);
   }

   public void run() {
        System.out.println("GUI");
   }

}

My application prints out Recorder and then GUI
Allows me to record my events
then right click on the JWindow to replay them...

but then when I click on the JFrame's close button or even in the frame because of the mouse listener it won't exit until all of the actions have been fully replayed?

One thing I did wonder is what ever I put in run is what is what keeps the thread running? So when System.out.println(""); is executed the thread dies? So I tried a while loop around them and my application successfully prints

GUI
GUI
GUI
RECORDER
RECORDER
GUI
RECORDER
etc
etc

So I can see that they threads are running concurrently... it's just that all of the other actions outside of the run don't seem to get executed... How can I include my mouse listener, etc within the threads execution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

在梵高的星空下 2024-09-01 21:26:33

您将线程对象混淆了。当您有一个 Object 时,它是一个 Runnable,它只是为线程提供了一个起点。然而,这并不意味着当另一个线程(在本例中是处理 MouseListener 的 Event 线程)调用 Runnable 中的方法时,该方法由该线程执行执行Runnable。当一个方法被调用时,它永远不会切换到另一个线程。如果你想要这个,你需要一个机制。例如,MouseListener 可以将任务发布到一个队列,然后在您的 Runnable.run() 中继续寻找新任务。

You are confusing Threads with Objects. When you have an Object that is a Runnable that just gives a starting point for a Thread. However this doesn't mean that when another thread (in this case the Event thread that handles the MouseListener) calls a method in your Runnable that it executed by the thread that is executing the Runnable. When a method is called, it never switches to another thread. If you want this you need a mechanism. For instance a queue that the MouseListener can post tasks to, and in your Runnable.run() you then keep looking for a new task.

轻拂→两袖风尘 2024-09-01 21:26:33

当 swing 初始化时,它启动它自己的事件调度线程。所有侦听器方法都在此线程中执行。您的侦听器对象是否实现 runnable 并不重要。

请参阅本教程以掌握 Swing 上下文中的多线程基础知识。 http://java.sun.com/docs/books/ tutorial/uiswing/concurrency/index.html

您问题的实际答案位于本教程的这一部分:

http://java.sun.com/docs/books/tutorial/uiswing/concurrency/cancel.html

但我建议您阅读整个教程。

When swing initializes it starts it's own Event Dispatch thread. All your listener methods executes within this thread. It doesn't matter whether or not your listener object implements runnable.

See this tutorial to grasp the basics of multi-threading in context of Swing. http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html

The actual answer to your question is in this part of the tutorial:

http://java.sun.com/docs/books/tutorial/uiswing/concurrency/cancel.html

But I suggest that you go through the whole tutorial.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文