JTextArea .setText / .append不工作

时间:2014-11-07 16:08:05

标签: java multithreading append jtextarea settext

我在设置JTextArea中的文本时遇到问题。我的程序使用3个线程使用增量打印到JTextArea。我可以使用System.out.println打印到命令提示符没有问题,但由于某种原因我无法打印到JTextArea。我是这个网站的新手,所以如果您需要更多信息,请告诉我!谢谢你的帮助!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

class SimpleFrame extends JFrame
{
    public static final int HEIGHT = 500;
    public static final int WIDTH = 600;
    public static JTextArea jta;

    public SimpleFrame()
    {
        setSize(WIDTH, HEIGHT);

        setLayout(new BorderLayout());

        JPanel topPanel = new JPanel();
        final JButton tim = new JButton("Tim (5 sec)");
        final JButton suzy = new JButton("Suzy (3 sec)");
        final JButton edna = new JButton("Edna (2 sec)");
        topPanel.add(tim);
        topPanel.add(suzy);
        topPanel.add(edna);
        add(topPanel, BorderLayout.NORTH);

        JPanel textPanel = new JPanel();
        jta = new JTextArea(10,10);
        jta.setFont(new Font("SansSerif", Font.PLAIN, 20));
        add(new JScrollPane(jta), BorderLayout.CENTER);

        tim.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                    Runnable r1 = new myRunnable(5, "Tim", "Tim", 5000);
                    Thread t1 = new Thread(r1);
                    t1.start();
                    tim.setEnabled(false);
            }
        });

        suzy.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                    Runnable r2 = new myRunnable(3, "Suzy", "Suzy", 3000);
                    Thread t2 = new Thread(r2);
                    t2.start();
                    suzy.setEnabled(false);
            }
        });

        edna.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                    Runnable r3 = new myRunnable(1, "Edna", "Edna", 1000);
                    Thread t3 = new Thread(r3);
                    t3.start();
                    edna.setEnabled(false);
            }
        });
    }
}

class myRunnable extends SimpleFrame implements Runnable
{
    int workerTime;
    String name;
    String threadToRun;
    int runtimeDelay;

    public myRunnable(int time, String workerName, String thread, int delay)
    {
        workerTime = time;
        name = workerName;
        threadToRun = thread;
        runtimeDelay = delay;
    }

    public void run()
    {
        int i = 0;

        while(i < 10)
        {
            try
            {
                jta.append(name + " is working, count = " + workerTime + "\n");
                workerTime += workerTime;
                Thread.sleep(runtimeDelay);
            }
            catch(InterruptedException e)
            {
                System.out.println("Error: " + e);
            }
        }
    }
}

public class Names
{
    public static void main(String[] args)
    {
                SimpleFrame frame = new SimpleFrame();
                frame.setTitle("Multi Thread Workers");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
    }
}

JTextArea应该打印类似的内容:

蒂姆正在工作,计数= 0
蒂姆正在工作,数= 1 Suzy正在工作,计数= 0
Suzy正在工作,count = 1
埃德娜正在工作,计数= 0
蒂姆正在工作,数= 2 埃德娜正在工作,数= 1 Suzy正在工作,计数= 2
埃德娜正在工作,数= 2 蒂姆正在工作,数= 3 Suzy正在工作,计数= 3
埃德娜正在工作,数= 3

以下是我遇到问题的代码片段:

jta.append(name + " is working, count = " + workerTime + "\n");
workerTime += workerTime;
Thread.sleep(runtimeDelay);

它完全符合..我只是没有得到输出 - 这就是为什么我认为这是一个非常简单的东西,我只是缺少。

这是我的新代码:我现在在actionlisteners中找到了“找不到符号”的错误,并且在jta.append上找不到“找不到符号”

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class Names
{
    public static void main(String[] args)
    {
                SimpleFrame frame = new SimpleFrame();
                frame.setTitle("Multi Thread Workers");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
    }
}
class SimpleFrame extends JFrame
{
    public static final int HEIGHT = 500;
    public static final int WIDTH = 600;
    public static JTextArea jta;

    public SimpleFrame()
    {
        setSize(WIDTH, HEIGHT);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        setVisible(true);

        JPanel topPanel = new JPanel();
        final JButton tim = new JButton("Tim (5 sec)");
        final JButton suzy = new JButton("Suzy (3 sec)");
        final JButton edna = new JButton("Edna (2 sec)");
        topPanel.add(tim);
        topPanel.add(suzy);
        topPanel.add(edna);
        add(topPanel, BorderLayout.NORTH);

        JPanel textPanel = new JPanel();
        jta = new JTextArea(10,10);
        jta.setFont(new Font("SansSerif", Font.PLAIN, 20));
        add(new JScrollPane(jta), BorderLayout.CENTER);

        tim.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                    Runnable r1 = new myRunnable(5, "Tim", "Tim", 5000, frame, jta);
                    Thread t1 = new Thread(r1);
                    t1.start();
                    tim.setEnabled(false);
            }
        });

        suzy.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                    Runnable r2 = new myRunnable(3, "Suzy", "Suzy", 3000, frame, jta);
                    Thread t2 = new Thread(r2);
                    t2.start();
                    suzy.setEnabled(false);
            }
        });

        edna.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                    Runnable r3 = new myRunnable(1, "Edna", "Edna", 1000, frame, jta);
                    Thread t3 = new Thread(r3);
                    t3.start();
                    edna.setEnabled(false);
            }
        });
    }
}

class myRunnable implements Runnable
{
    int workerTime;
    String name;
    String threadToRun;
    int runtimeDelay;
    JFrame frame;
    JTextArea jTextArea;

    public myRunnable(int time, String workerName, String thread, int delay, JFrame f, JTextArea j)
    {
        workerTime = time;
        name = workerName;
        threadToRun = thread;
        runtimeDelay = delay;
        frame = f;
        jTextArea = j;
    }

    public void run()
    {
        int i = 0;

        while(i < 10)
        {
            try
            {
                jta.append(name + " is working, count = " + workerTime);
                workerTime += workerTime;
                frame.repaint();
                Thread.sleep(runtimeDelay);
            }
            catch(InterruptedException e)
            {
                System.out.println("Error: " + e);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

这是您的代码编辑,以便它可以按您的意愿工作。

我将在底部包含一系列更改

public class Names {
    public static void main(String[] args) {
        SimpleFrame frame = new SimpleFrame();
        frame.setTitle("Multi Thread Workers");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
class SimpleFrame extends JFrame {
   public static final int HEIGHT = 500;
   public static final int WIDTH = 600;
   public JTextArea jta;

   public SimpleFrame() {
       setSize(WIDTH, HEIGHT);
       setDefaultCloseOperation(EXIT_ON_CLOSE);
       setLayout(new BorderLayout());

       JPanel topPanel = new JPanel();
       final JButton tim = new JButton("Tim (5 sec)");
       final JButton suzy = new JButton("Suzy (3 sec)");
       final JButton edna = new JButton("Edna (2 sec)");
       topPanel.add(tim);
       topPanel.add(suzy);
       topPanel.add(edna);
       add(topPanel, BorderLayout.NORTH);

       JPanel textPanel = new JPanel();
       jta = new JTextArea(10, 10);
       jta.setFont(new Font("SansSerif", Font.PLAIN, 20));
       JScrollPane jsp = new JScrollPane(jta);
       add(jsp, BorderLayout.CENTER);

       tim.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent event) {
               Runnable r1 = new myRunnable(5, "Tim", "Tim", 5000,
                    SimpleFrame.this, jta);
               Thread t1 = new Thread(r1);
               t1.start();
               tim.setEnabled(false);
           }
       });

       suzy.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent event) {
               Runnable r2 = new myRunnable(3, "Suzy", "Suzy", 3000,
               SimpleFrame.this, jta);
               Thread t2 = new Thread(r2);
               t2.start();
               suzy.setEnabled(false);
           }
       });

       edna.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent event) {
               Runnable r3 = new myRunnable(1, "Edna", "Edna", 1000,
                       SimpleFrame.this, jta);
               Thread t3 = new Thread(r3);
               t3.start();
               edna.setEnabled(false);
           }
       });
       jta.setVisible(true);
       jsp.setVisible(true);
       topPanel.setVisible(true);
       setVisible(true);
   }
}
class myRunnable implements Runnable {
int workerTime;
String name;
String threadToRun;
int runtimeDelay;
JFrame frame;
JTextArea jTextArea;

public myRunnable(int time, String workerName, String thread, int delay,
        JFrame f, JTextArea j) {
    workerTime = time;
    name = workerName;
    threadToRun = thread;
    runtimeDelay = delay;
    frame = f;
    jTextArea = j;
}

public void run() {
    int i = 0;

    while (i < 10) {
        try {
            jTextArea.append(name + " is working, count = " + workerTime);
            workerTime += workerTime;
            frame.repaint();
            Thread.sleep(runtimeDelay);
        } catch (InterruptedException e) {
            System.out.println("Error: " + e);
        }
    }
}
}

的变化:

  • 我将句柄中的frame的使用更改为SimpleFrame.this,因为您希望为每个线程提供他们需要使用的jframe的实例。变量框架只在main方法中定义,这就是为什么你不能在SimpleFrame类中使用它。
  • 我给了滚动窗格一个名字,我称之为jsp,并将所有内容设置为在构造函数的底部可见 jta.setVisible(true); jsp.setVisible(true); topPanel.setVisible(true); setVisible(true);
  • 我在SimpleFrame的声明中从jta中删除了static。你不希望每个SimpleFrame都这样。

如果您有任何疑问,可以给我发电子邮件(我的电子邮件可以从我的个人资料中看到) 由于您是网站的新用户,因此您应该通过点击复选标记来接受答案,因为这样可以解决问题。