我在设置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正在工作,计数= 1
Edna正在工作,计数= 0
蒂姆在工作,数= 2
Edna正在工作,计数= 1
Suzy正在工作,计数= 2
Edna正在工作,计数= 2
蒂姆在工作,数= 3
Suzy正在工作,计数= 3
Edna正在工作,计数= 3

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

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


它符合要求。.我只是没有得到输出-这就是为什么我认为这是我所缺少的超级简单的原因。

这是我的新代码:我现在在动作侦听器内收到框架的“找不到符号”错误,而在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);
            }
        }
    }
}

最佳答案

这是对您的代码进行了编辑,以便您可以随意使用。

我将在底部列出更改列表

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都一样。


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

关于java - JTextArea .setText/.append不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26805350/

10-17 01:49