Java 更改标签颜色的SWT正在崩溃
ColorDetector.javaJava 更改标签颜色的SWT正在崩溃,java,swt,Java,Swt,ColorDetector.java import java.awt.AWTException; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; public class ColorDetector { pub
import java.awt.AWTException;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class ColorDetector {
public static void main(String[] args) throws AWTException {
Display display = new Display();
//Display starts everything
Shell shell = new Shell(display);
GridLayout gridLayout = new GridLayout();
shell.setLayout(gridLayout);
Label label = new Label(shell, 0);
label.setText("WHAT COLOR AM I");
Refresher ref = new Refresher(display, label);
ref.start();
shell.open();
System.out.println("Shell started");
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}
Refresher.java
import java.awt.AWTException;
import java.awt.Robot;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
public class Refresher extends Thread {
static Display d;
static Label l;
public Refresher(Display display, Label label) {
Refresher.d = display;
Refresher.l = label;
}
private static void colorUpdater(Display display, Label label) {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
Robot colorGet = null;
try {
colorGet = new Robot();
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
java.awt.Color awtColor = colorGet.getPixelColor(0, 700);
Color color = new Color(display, awtColor.getRed(), awtColor.getGreen(), awtColor.getBlue());
label.setForeground(color);
}
});
}
public void run() {
System.out.println("Refresher started");
while (true) {
colorUpdater(d, l);
}
}
}
上面的代码应该创建一个带有标签的窗口。这个标签由Refresher.java获取,并被反复更改为我屏幕上某个像素(0500)的颜色(这是我放置Google Chrome颜色选择器选项卡的地方)。目标是能够通过在(0,500)处更改像素的颜色来动态更改标签的颜色。应用程序正常运行了几秒钟,但随后崩溃,给了我这个消息
#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x000000010971a4d5, pid=11999, tid=46339
#
# JRE version: Java(TM) SE Runtime Environment (12.0.1+12) (build 12.0.1+12)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (12.0.1+12, mixed mode, sharing, tiered, compressed oops, g1 gc, bsd-amd64)
# Problematic frame:
# V [libjvm.dylib+0x31a4d5] _ZN20G1ParScanThreadState22copy_to_survivor_spaceE11InCSetStateP7oopDescP11markOopDesc+0xe1
#
# No core dump will be written. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /Users/myname/eclipse-workspace/Screen Color Detector/hs_err_pid11999.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.java.com/bugreport/crash.jsp
#
错误报告文件是否是由JDK本机代码中的错误导致的类似崩溃,请尝试更新版本的Java。@greg-449有效,谢谢!为什么Java12不能处理我的代码?很可能只是JVM代码中的一个bug,在较新版本中得到了修复version@greg-这似乎只是暂时的解决办法。同样的错误也出现在JVM/AWT/SWT本机代码中,因为init仍然是一个bug。我认为将SWT与AWT Robot类混合使用并没有什么帮助,因为混合使用图形工具包通常会导致问题。在SWT中有几种捕获屏幕的方法-例如,请参阅