我如何重新实现JCheckBox

时间:2014-02-14 22:28:20

标签: java concurrency mouseevent

我希望我的程序开始在我的鼠标下打印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;

    }
}

感谢您的时间

编辑:我不知道我是否应该在这个问题上发布另一个帖子但我能够更进一步思考。 我通过多线程尝试了一些东西,下面你可以看到更新的代码。 Check类是我使用'mythread'对象给出的runnable参数,使我的代码在EDT和工作线程之间交替。 现在它有点做我想要的,除了我的代码在离开和输入元素时产生许多错误:

    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)

我的更新代码:

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;
        }

2 个答案:

答案 0 :(得分:2)

您正在使用无限循环阻止事件调度线程。

EDT负责许多事情,包括事件处理和绘画。阻止/停止此线程的任何操作都将阻止您的应用程序接收新事件或处理新的绘制请求。

Swing是一个单线程环境并且不是线程安全的,所以当你可以产生一个新的Thread时,你需要确保任何与UI交互的代码都是在上下文中完成的。美国东部时间。

更简单的解决方案是使用SwingWorker,这将允许您从后台线程中监控鼠标位置,但具有简化EDT的publishprocess方法同步过程。

请查看Concurrency in Swing了解详情。

答案 1 :(得分:0)

您的while循环将阻止您的程序执行任何其他操作。单击复选框(zwevendeMuis = doos.isEnabled())时应切换布尔值,并且应该在应用程序的一个或多个组件上注册MouseMotionListener,如果zwevendeMuis为真,则会打印颜色值