gpt4 book ai didi

java - 更新文本字段时出现无效线程访问异常

转载 作者:行者123 更新时间:2023-12-01 11:47:48 25 4
gpt4 key购买 nike

我正在尝试使用可运行线程更新属于另一个类的文本字段,但出现“无效线程访问”异常。

我的代码是:

 Thread t2 = new Thread () {

public void run () {
System.out.println("t2 thread içindeyim.");
try {
String sql = " RESTORE DATABASE Genius3"+
" FROM DISK = '"+collected.getdbpath()+"'"+
" WITH MOVE 'GeniusIII_Data' TO 'C:\\SQLDATA\\Genius3.mdf',"+
" MOVE 'GeniusIII_Log' TO 'C:\\SQLDATA\\Genius3_1.ldf'";

Class.forName(driver).newInstance();
con = DriverManager.getConnection(url);
stmt = con.prepareStatement(sql);
rs = stmt.executeQuery();}
catch (Exception ef) {ef.printStackTrace();}
finally {

ekran5.text_1.setText("done");

}
}
};

以及另一个包含该文本字段的类;

 public class ekran5 {

public static Label islemlabel;

public static Composite composite_1 ;
public static Label detail;
public static Text text_1;
public static void start(){


composite_1 = new Composite(Loader.composite, SWT.BORDER | SWT.EMBEDDED);
composite_1.setBackground(SWTResourceManager.getColor(192,192,192));
composite_1.setBounds(362, 83, 668, 536);

islemlabel = new Label(composite_1, SWT.NONE);
islemlabel.setFont(SWTResourceManager.getFont("Arial CYR", 10, SWT.NORMAL));
islemlabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_GRAY));
islemlabel.setBounds(10, 448, 84, 15);
islemlabel.setText("\u0130\u015Flem Detay\u0131");

text_1 = new Text(composite_1, SWT.BORDER);
text_1.setBounds(10, 469, 628, 37);
}

}

在finally block 中,我正在更新文本字段,但因此它给了我一个无效的线程访问异常。

最佳答案

在 SWT 中,仅允许 UI 线程更新小部件,请尝试使用 Display.syncExec 执行所有 UI 更新。 (或者,可以使用 UI 作业。)

另外,阅读 SWT 常见问题解答也是明智之举,这对我来说听起来像是一个常见问题解答。

关于java - 更新文本字段时出现无效线程访问异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29021634/

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