我正在用java开发一个应用程序来“监控”火车中断。
事实上,我正在阅读 log.txt 文件并将它们显示到 JTabbedPane 中,其中包含对应于不同城市的不同窗格。
有一个带有 JPane 的主 GUI,我正在使用线程在其中插入 JTabbedPane,在此 JTabbedPane 中,我正在使用线程插入窗格。我这样做是因为我想每分钟更新一次窗格(带有日志文件信息)而不阻塞所有的 GUI。这是做监控应用程序的正确方法吗?
我的问题是,当我创建了所有我想要的标签时,我无法更新它们,当我尝试更新一个时,它总是最后一个更新。
这是我的代码:创建新的 JTabbedPane
Controller.Controller_ListeOnglets n = new Controller.Controller_ListeOnglets(View.View_Application.getJPanel());
n.start();
这是我在 JTabbedPane 中添加选项卡的方式
public class Controller_ListeOnglets extends Thread {
JPanel jo = new JPanel();
public Controller_ListeOnglets(JPanel j) {
jo = j;
}
@Override
public void run() {
View.View_ListeOnglets t = new View.View_ListeOnglets();
jo.add(t);
ArrayList<String> list = new ArrayList<>();
list = Controller_Modification.ChercherSystemes();
for (String i : list) {
try {
Controller_Onglet nouveau = new Controller_Onglet(View.View_ListeOnglets.getJTabbedPane(), Controller.Controller_BDD.GetSystemeID(i));
nouveau.start();
this.sleep(100);
} catch (InterruptedException | SQLException | ClassNotFoundException ex) {
Logger.getLogger(Controller_ListeOnglets.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
public class Controller_Onglet extends Thread {
Model.Model_Systeme systemeB;
static JTabbedPane jo = new JTabbedPane();
public Controller_Onglet(JTabbedPane j, Model.Model_Systeme systemeA) {
jo = j;
systemeB = systemeA;
}
@Override
public void run() {
View.View_Onglet onglet = new View.View_Onglet(systemeB);
if (jo.getTabCount() == 0) {
jo.add(onglet);
jo.setTitleAt(jo.getTabCount() - 1, systemeB.getSysteme_Nom());
jo.setBackgroundAt(0, Color.CYAN);
} else {
jo.add(onglet, jo.getTabCount());
jo.setTitleAt(jo.getTabCount() - 1, systemeB.getSysteme_Nom());
jo.setBackgroundAt(jo.getTabCount() - 1, Color.CYAN);
}
/* Timer t = new Timer(5000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {*/
if (EventQueue.isDispatchThread()) {
Controller_TraitementFichier traitement = new Controller_TraitementFichier(jo, systemeB.getSysteme_Nom());
traitement.start();
} else {
Controller_TraitementFichier traitement = new Controller_TraitementFichier(jo, systemeB.getSysteme_Nom());
traitement.start();
}
/*}
});
t.start();*/
}
//A Compléter
public static void removeOnglet(Model.Model_Systeme systeme) {
for (int i = 0; i < jo.getTabCount(); i++) {
if (jo.getTitleAt(i).equals(systeme.getSysteme_Nom())) {
jo.remove(i);
}
}
}
这是我如何尝试更新选项卡的示例
public class Controller_TraitementFichier extends Thread {
String title = null;
JTabbedPane jo = null;
public Controller_TraitementFichier(JTabbedPane j, String titleOnglet) {
title = titleOnglet;
jo = j;
}
@Override
public void run() {
InputStream flux = null;
try {
flux = new FileInputStream("./src/Ressources/Archivage_LISATEL/12203005.TXT");
InputStreamReader lecture = new InputStreamReader(flux);
BufferedReader buff = new BufferedReader(lecture);
String ligne = buff.readLine();
try {
while (ligne != null) {
View_Onglet onglet = View_ListeOnglets.getJTabbedPane(title);
if (title.equals(title)) {
View.View_ListeOnglets.getJTabbedPane().setSelectedIndex(0);
onglet.changeTexteArea1(ligne + "\n");
jo.setBackgroundAt(View.View_ListeOnglets.getJTabbedPane().getTabPlacement(), Color.red);
}
ligne = buff.readLine();
}
System.out.println("Traitement " + title);
buff.close();
lecture.close();
flux.close();
} catch (IOException ex) {
Logger.getLogger(Controller_TraitementFichier.class.getName()).log(Level.SEVERE, null, ex);
}
//System.out.println(View.View_ListeOnglets.getJTabbedPane().getTitleAt(View_ListeOnglets.getJTabbedPane().getSelectedIndex()));
} catch (IOException ex) {
Logger.getLogger(Controller_TraitementFichier.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
flux.close();
} catch (IOException ex) {
Logger.getLogger(Controller_TraitementFichier.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
这不是很清楚,所以如果你想帮助我更多,我会把我的代码发给你。
谢谢