我买了多线程多线程显示Java控制台登录GUI的JFrame
我创建了一个Java类InformationConsole
让我的java控制台日志,每次一个小问题,当我使用System.out.print("...");
消息将发送到JTextArea中,它就像
public class InformationConsole extends OutputStream {
private JTextArea textArea;
private final StringBuilder sb = new StringBuilder();
private String title;
public InformationConsole(final JTextArea textArea, String title) {
this.textArea = textArea;
this.title = title;
sb.append(title + "> ");
}
@Override
public void flush() {
}
@Override
public void close() {
}
@Override
public void write(int b) throws IOException {
if (b == '\r')
return;
if (b == '\n') {
final String text = sb.toString() + "\n";
SwingUtilities.invokeLater(new Runnable() {
public void run() {
textArea.append(text);
}
});
sb.setLength(0);
sb.append(title + "> ");
return;
}
sb.append((char) b);
}
}
我用它与我的GUI显示日志
public class Lancer{
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
CreerEtAffichierGUI();
}
});
}
public static void CreerEtAffichierGUI(){
JFrame fenetre = new JFrame();
JTextArea log = new JTextArea();
InformationConsole consoleInfo = new InformationConsole(log, "InfoConsole");
fenetre.setVisible(true);
fenetre.setResizable(false);
fenetre.setLocationRelativeTo(null);
fenetre.setTitle("Modélisation");
fenetre.setBounds(0, 0, 800, 800);
fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton bouton1 = new JButton("Start");
fenetre.setLayout(new GridLayout(2, 1));
//On ajoute le bouton au content pane de la JFrame
fenetre.getContentPane().add(bouton1);
fenetre.getContentPane(). add(new JScrollPane(log, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));System.setOut(new PrintStream(consoleInfo));
bouton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CreerModelJCL();
JOptionPane.showMessageDialog(fenetre, "Modelisation JCL Fini");
}
});
fenetre.setVisible(true);
}
}
钍“连接”与Java控制台和我的JTextArea e JTextArea在GUI中不会实时更新,它会显示应用程序完成时的所有日志,我是多线程的新手,我找不到原因...
请给我我有些帮助?
这里是CreerModelJCL()
他只是有很多println
与控制台
public static void CreerModelJCL(){
System.out.println("-------- Exporter fichier résultat XMI Fini--------------");
System.out.println("------------------------Modelisation JCL FINI------------------------");
}
请添加代码'CreerModelJCL();'我用一个新的线程中运行一个循环,打印一些文字,以每秒控制台取代这个方法调用测试程序它工作正常。所以我怀疑这种方法(我认为在控制台中产生了一些输出)在GUI线程上运行,并且锁定了GUI的任何更新。 –
@ d.j.brown新增 –