[英]JTextArea .setText/.append Not Working
I am having problems setting the text inside of a JTextArea. 我在设置JTextArea内的文本时遇到问题。 My Program is using 3 threads to print onto the JTextArea, using increments.
我的程序使用3个线程以增量方式打印到JTextArea上。 I can print onto the command prompt using System.out.println with no problem, but for some reason I can't print to the JTextArea.
我可以使用System.out.println毫无问题地打印到命令提示符下,但是由于某些原因我无法打印到JTextArea。 I am brand new to this website, so please let me know if you need anymore additional information!
我是该网站的新手,所以如果您需要其他信息,请告诉我! Thanks for any help!
谢谢你的帮助!
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);
}
}
The JTextArea should be printing something similar to this: JTextArea应该打印类似于以下内容的内容:
Tim is working, count = 0 蒂姆在工作,计数= 0
Tim is working, count = 1 蒂姆在工作,计数= 1
Suzy is working, count = 0 Suzy正在工作,计数= 0
Suzy is working, count = 1 Suzy正在工作,计数= 1
Edna is working, count = 0 Edna正在工作,计数= 0
Tim is working, count = 2 蒂姆在工作,数= 2
Edna is working, count = 1 Edna正在工作,计数= 1
Suzy is working, count = 2 Suzy正在工作,计数= 2
Edna is working, count = 2 Edna正在工作,计数= 2
Tim is working, count = 3 蒂姆在工作,数= 3
Suzy is working, count = 3 Suzy正在工作,计数= 3
Edna is working, count = 3 Edna正在工作,计数= 3
Here is a snip-it of the code I'm having a problem with: 这是我遇到问题的代码的片段:
jta.append(name + " is working, count = " + workerTime + "\n");
workerTime += workerTime;
Thread.sleep(runtimeDelay);
It complies just fine.. I just don't get the output-- that's why I think it's something super simple that I'm just missing. 它符合要求。.我只是没有得到输出-这就是为什么我认为它只是我所缺少的一种超级简单的原因。
HERE IS MY NEW CODE: I am getting the "cannot find symbol" errors for frame now inside of the actionlisteners and "cannot find symbol" on jta.append 这是我的新代码:我现在在动作侦听器内收到框架的“找不到符号”错误,而在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);
}
}
}
}
Here is your code edited so that it works as you would like. 这是对您的代码进行了编辑,以便您可以随意使用。
I will include a list of changes at the bottom 我将在底部列出更改列表
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);
}
}
}
}
Changes: 变化:
SimpleFrame.this
, as you want to give each thread an instance of the jframe they need to use. SimpleFrame.this
,因为您想为每个线程提供一个它们需要使用的jframe实例。 the variable frame was only defined within the main method and thats why you couldn't use it in the SimpleFrame class. jta.setVisible(true); jsp.setVisible(true); topPanel.setVisible(true); setVisible(true);
jta.setVisible(true); jsp.setVisible(true); topPanel.setVisible(true); setVisible(true);
的底部可见jta.setVisible(true); jsp.setVisible(true); topPanel.setVisible(true); setVisible(true);
jta.setVisible(true); jsp.setVisible(true); topPanel.setVisible(true); setVisible(true);
static
from jta in its declaration in SimpleFrame. static
变量。 you don't want this to be the same for every SimpleFrame. If you have anymore questions you can email me (my email is visible from my profile) Since your new to the site, you should accept the answer by clicking on the check mark because this closes the question. 如果您还有其他问题,可以给我发送电子邮件(从我的个人资料中可以看到我的电子邮件)。自从您第一次访问该网站以来,您应该通过单击对号接受答案,因为这可以解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.