gpt4 book ai didi

java - 我需要如何重新实现 JCheckBox

转载 作者:行者123 更新时间:2023-12-02 06:12:10 26 4
gpt4 key购买 nike

我希望我的程序在我启用一个名为 doos 的复选框时开始在鼠标下打印 RGB 值,并且我希望它在取消选择时停止。

    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”对象一起提供的可运行参数,以使我的代码在 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;
}

最佳答案

您的无限循环阻塞了事件调度线程。

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

Swing 是一个单线程环境,并且不是线程安全的,因此虽然您可以生成一个新的线程,但您需要确保与 UI 交互的任何代码都是这样来回完成的在 EDT 的范围内。

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

看看Concurrency in Swing了解更多详情。

关于java - 我需要如何重新实现 JCheckBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21790621/

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