我试图在单击按钮时为 swing 应用程序运行一个线程,但该值没有更新。 它应该获取我正在搜索的计算机名称,但为了更新该值,我必须启动一个新的 GUI 实例。 我创建了一个线程,但由于某种原因它不起作用。如有任何帮助,我们将不胜感激。
(t.start位于代码块末尾)
searchComputerButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Thread t = new Thread("my non EDT thread") {
public void run() {
//my work
testLabel.setText(CN);
}
};
String line;
BufferedWriter bw = null;
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(tempFile));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// String lineToRemove = "OU=Workstations";
String s = null;
Process p = null;
/*
* try { // p = Runtime.getRuntime().exec(
* "cmd /c start c:\\computerQuery.bat computerName"); } catch
* (IOException e1) { // TODO Auto-generated catch block
* e1.printStackTrace(); }
*/
try {
p = Runtime.getRuntime().exec("c:\\computerQuery.bat");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
StringBuffer sbuffer = new StringBuffer();
BufferedReader in = new BufferedReader(new InputStreamReader(p
.getInputStream()));
try {
while ((line = in.readLine()) != null) {
System.out.println(line);
// textArea.append(line);
String dn = "CN=FDCD111304,OU=Workstations,OU=SIM,OU=Accounts,DC=FL,DC=NET";
LdapName ldapName = new LdapName(dn);
String commonName = (String) ldapName.getRdn(
ldapName.size() - 1).getValue();
}
ComputerQuery.sendParam();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (InvalidNameException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} finally
{
try {
fw.close();
}
catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
try {
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ComputerQuery.sendParam();
t.start();
}
});
更新
private void threadStart() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
testLabel.setText(CN);
}
});
我把方法放在这里
JButton searchComputerButton = new JButton("Search");
searchComputerButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
threadStart();
String line;
最佳答案
注意 Swing 线程 https://docs.oracle.com/javase/tutorial/uiswin/concurrency/
看这里: http://www.javamex.com/tutorials/threads/invokelater.shtml
您必须使用方法将 JLabel 更新方法调用排入队列 SwingUtilities.invokeLater(???)。 下面的例子可以做到这一点
此外,我认为这与 .batch 文件调用有关。看看这里:How do I run a batch file from my Java Application? 可运行任务 = new UpdateJob("查询:"+ i); SwingUtilities.invokeLater(任务);
<小时/>让其更容易理解。
Swing 在一个线程内管理所有绘制操作。 它提供了一个队列。如果您从该队列外部调用方法,则行为是完全不可预测的.. NullPointer... RuntimeExc .... 但是如果您调用 SwingUtilities.invokeLater(...) 您的方法将被排队到 Swing-Queue 中并尽快被调用!
因评论而更新:
检查你的主线程(GUI) 检查你的线程。 当子线程(例如 ActionListener)想要调用 JLabel::setText 时 它必须使用方法 SwingUtils::InvokeLater("...");
这意味着 invokeLater()
必须在不直接属于主线程的所有线程中调用。
由于问题而更新
在我看来,您当前的代码根本不需要 SwingUtilities.invok..。 您是否更改了分配给您的问题的代码。
关于java - 使用线程更新 JLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31221332/