【问题标题】:JAVA : Output text from a class to main frameJAVA:将文本从类输出到主框架
【发布时间】:2014-01-27 15:22:51
【问题描述】:

我是 JAVA 的新手。 尝试一个简单的应用程序,一个线程。我有一个带有文本区域的框架。当我从一个类启动一个线程时,我想输出到文本区域,而不是控制台。 我无法在任何地方找到如何做到这一点。但是应该很简单??? 请帮忙。 以下是文件:

文件 01/03 = MaFenetre.java

public class MaFenetre
{
public static void main (String args[])
    {
    Fenetre fen = new Fenetre() ;
    fen.setVisible(true) ;

    MyThread test = new MyThread (15) ;
    test.start() ;      
    }
}

文件 02/03 = Fenetre.java

import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;

public class Fenetre extends JFrame// implements ActionListener
{
private JTextArea zoneTexte;

// Constructeur
public Fenetre ()
    {
    setTitle ("Avec deux boutons") ;
    setSize (700, 550) ;

    Container contenu = getContentPane() ;
    contenu.setLayout(new FlowLayout()) ;

    zoneTexte=new JTextArea(5,20);
    contenu.add(zoneTexte) ;

    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

public void AddTxt(String txt)
    {
    this.zoneTexte.append(txt);
    }   
}

文件 03/03:MyThread.java

public class MyThread extends Thread
{
private int nb ;

public MyThread (int nb)
    {
    this.nb = nb ;
    }

public void run ()
    {
    try
        {
        for(int i=0 ; i<nb ; i++)
            {
            System.out.print ("test "+i+"\n");
            sleep (500);
            }
        }
    catch (InterruptedException e) {}
    }
}

所以我只想: AddTxt("测试"+i+"\n"); 代替 : System.out.print("测试"+i+"\n"); 但是,当然,它不起作用。 问候。

【问题讨论】:

  • 您需要对 Fenetre 对象的引用-您可以将一个引用传递给构造函数并存储在实例变量中-就像您对 nb 实例 var 所做的那样(尽管这当然不是引用) - 或添加一个注入器(例如 setFenetre)

标签: java multithreading frame


【解决方案1】:

Fenetre 对象传递给线程,这样就可以在run 方法中访问该对象。 在run方法中,假设feFenetre的对象。

public void run ()
    {
    try
        {
        for(int i=0 ; i<nb ; i++)
            {
            System.out.print ("test "+i+"\n");
                SwingUtilities.invokeLater(
                    new Runnable()
                    {
                       fe.AddTxt("test "+i+"\n");
                    });sleep (500);
            }
        }
    catch (InterruptedException e) {}
    }

【讨论】:

  • 好的,它可以正常工作: public MyThread (int nb,Fenetre fe) { this.nb = nb ;这个.fen=fe; }
  • 是的,这样我们需要将Fenetre对象传递给线程对象
【解决方案2】:

所以我只想: AddTxt("test "+i+"\n");而不是: System.out.print ("test "+i+"\n");但是,当然,它不起作用。问候。

它不能工作,因为你的线程类不知道你的框架类。

public class MyThread extends Thread
{
private int nb ;

private Fenetre fen;

public void setFenetre(final Fenetre fen) {
   this.fen = fen;
}

public MyThread (int nb)
    {
    this.nb = nb ;
    }

public void run ()
    {
    try
        {
        for(int i=0 ; i<nb ; i++)
            {
            fen.AddText ("test "+i+"\n");
            sleep (500);
            }
        }
    catch (InterruptedException e) {}
    }
}

public class MaFenetre
{
public static void main (String args[])
    {
    Fenetre fen = new Fenetre() ;
    fen.setVisible(true) ;

    MyThread test = new MyThread (15) ;
    test.setFenetre(fen);
    test.start() ;      
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-22
    • 2016-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多