我有一个标签不在GUI的同一实例中更新。 如果我单击应该更新jLabel(代码块中的“testLabel”)的值的jButton,我必须再次运行java程序才能看到所做的更改。我怎样才能让它出现在同一个实例中的按钮点击? 我知道关于invokelater,我一直在玩它试图让它实时更新,但没有运气。我一直坚持这一段时间,所以任何帮助表示赞赏。 通过下面列出的代码块,我仍然需要运行GUI的新实例来获取更新的值。标签不在GUI的相同实例中更新
相关代码:
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MISControlPanel window = new MISControlPanel();
window.frame.setVisible(true);
// testLabel.setText(CN);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
JButton searchComputerButton = new JButton("Search");
searchComputerButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
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();
}
});
try (BufferedReader br = new BufferedReader(new FileReader(
"resultofbatch.txt"))) {
final Pattern PATTERN = Pattern.compile("CN=([^,]+).*");
try {
while ((sCurrentLine = br.readLine()) != null) {
String[] tokens = PATTERN.split(","); // This will return
// you a array,
// containing the
// string array
// splitted by what
// you write inside
// it.
// should be in your case the split, since they are
// seperated by ","
// System.out.println(sCurrentLine);
CN = sCurrentLine.split("CN=", -1)[1].split(",", -1)[0];
System.out.println(CN);
testLabel.setText(CN);
}
全类代码 http://pastebin.com/havyqMxP
电脑查询类(小类) http://pastebin.com/Q89BCjya
您好user6680 ...我不知道您打算执行此操作的执行流程的哪一部分,您正在使用LDAP,我看到..这意味着您正在连接到LDAP服务器,并且可能执行长时间运行的任务... 你是否试图在组件的任何类型的监听器(因此在事件调度线程)的回调中执行此操作?或者你正在另一个线程上执行此操作? Swing.invokelater将无助于此...我建议开始阅读SwingWorker,并在UI上分割表示的操作(处理swing组件的代码) – Victor
试试这个教程... https://docs.oracle.com/javase/tutorial/uiswing/concurrency /simple.html ... – Victor
我认为我需要它做的是在不同的线程上运行,以便在GUI的同一实例内更新值。我看着你提供的教程链接,但我很困惑。它使用了在done方法中加载图像的示例,但我不确定如何将其实现到我的代码中。我一直试图让这个工作一个多星期。你有没有可能向我展示我想要完成的工作示例? – user6680