简体   繁体   English

我如何重新实现JCheckBox

[英]How do I need to reimplement the JCheckBox

I want my program to start printing out RGB-values under my mouse ,the moment I enable a checkbox, called doos, and I want it to stop the moment it is deselected. 我希望我的程序开始在我的鼠标下打印RGB值,我启用一个名为doos的复选框的那一刻,我希望它在取消选择时停止。

    private void doosActionPerformed(java.awt.event.ActionEvent evt) {

    if (doos.isEnabled()) {
        try {
            zwevendeMuis =true;
            robot = new Robot();

            while (zwevendeMuis==true) {

                pointer = MouseInfo.getPointerInfo();
                point = pointer.getLocation();
                color = robot.getPixelColor((int) point.getX(), (int) point.getY());
                System.out.println("Color at: " + point.getX() + "," + point.getY() + " is: " + color);
            }
        } catch (Exception e) {
        }            
    }
    else{
        zwevendeMuis =false;

    }
}

Thank you for your time 感谢您的时间

EDIT : I dont know if i should post another thread on this matter but i was able to take it one step further i think. 编辑:我不知道我是否应该在这个问题上发布另一个帖子,但我能够更进一步,我认为。 I have tried some stuff out via multithreading ,below you can see the updated code. 我通过多线程尝试了一些东西,下面你可以看到更新的代码。 The Check class is the runnable parameter i give with the 'mythread' object to make my code alternate between EDT and worker thread. Check类是我使用'mythread'对象给出的runnable参数,使我的代码在EDT和工作线程之间交替。 Now it kinda does what I want with the exception of my code spawning of numerous errors when leaving and entering the element: 现在它有点做我想要的,除了我的代码在离开和输入元素时产生许多错误:

    Exception in thread "AWT-EventQueue-0" java.lang.IllegalThreadStateException
    at java.lang.Thread.start(Thread.java:682)
    at testgui.TestGUI$1.mouseEntered(TestGUI.java:126)
    at java.awt.Component.processMouseEvent(Component.java:6514)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6270)
Color at: 495.0,520.0 is: java.awt.Color[r=168,g=51,b=162]
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.trackMouseEnterExit(Container.java:4620)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4474)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
Color at: 498.0,516.0 is: java.awt.Color[r=168,g=51,b=162]

    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.awt.EventQueue$3.run(EventQueue.java:686)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:702)
    at java.awt.EventQueue$4.run(EventQueue.java:700)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
Color at: 499.0,515.0 is: java.awt.Color[r=168,g=51,b=162]
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

My updated code: 我的更新代码:

class Check implements Runnable {

            @Override
            public void run() {
                go();
            }

            public void go() {
                System.out.println("");

            }

        }

        @Override
        public void mousePressed(MouseEvent me) {
        }

        @Override
        public void mouseReleased(MouseEvent me) {
        }

        @Override
        public void mouseEntered(MouseEvent me) {
            zwevendeMuis = true;
            Runnable control = new Check();
            Thread mythread = new Thread(control);
            try {
                robot = new Robot();

            } catch (AWTException ex) {
                Logger.getLogger(TestGUI.class.getName()).log(Level.SEVERE, null, ex);
            }
            while (zwevendeMuis == true) {
                pointer = MouseInfo.getPointerInfo();
                point = pointer.getLocation();
                color = robot.getPixelColor((int) point.getX(), (int) point.getY());
                System.out.println("Color at: " + point.getX() + "," + point.getY() + " is: " + color);
                mythread.start();
            }
        }

        @Override
        public void mouseExited(MouseEvent me) {
            zwevendeMuis=false;
        }

You're blocking the Event Dispatching Thread with your infinite loop. 您正在使用无限循环阻止事件调度线程。

The EDT is responsible for many things, including event processing and painting. EDT负责许多事情,包括事件处理和绘画。 Any action which blocks/stops this thread will prevent your application from receiving new events or processing new paint requests. 阻止/停止此线程的任何操作都将阻止您的应用程序接收新事件或处理新的绘制请求。

Swing is a single threaded environment and isn't thread safe, so while you could spawn off a new Thread , you would need to ensure that any code that interacted with the UI was done so fro within the context of the EDT. Swing是一个单线程环境并且不是线程安全的,所以当你可以产生一个新的Thread ,你需要确保任何与UI交互的代码都是在EDT的上下文中完成的。

A simpler solution would be to use a SwingWorker , which would allow you to monitor the mouse position from within a background thread, but has publish and process methods which simplifies the EDT synchronization process. 一个更简单的解决方案是使用SwingWorker ,它允许您从后台线程中监视鼠标位置,但具有简化EDT同步过程的publishprocess方法。

Take a look at Concurrency in Swing for more details. 有关更多详细信息,请参阅Swing中的Concurrency

Your while loop is going to prevent your program from doing anything else. 你的while循环将阻止你的程序做任何其他事情。 You should toggle a boolean value when clicking your checkbox ( zwevendeMuis = doos.isEnabled() ), and you should register a MouseMotionListener with one or more components of your application which will print the color value if zwevendeMuis is true. 单击复选框时应切换一个布尔值( zwevendeMuis = doos.isEnabled() ),并且应该为应用程序的一个或多个组件注册MouseMotionListener ,如果zwevendeMuis为true,将打印颜色值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM