如何将DATA类中的数据从一个方法发送到guiTest类中的jTextArea?

huangapple go评论87阅读模式
英文:

How do I send data from a method in DATA class to a jTextArea in guiTest class

问题

    public class GUITest {

	private JFrame frame;
 	JTextArea textArea_1 = new JTextArea();
	
	public static void main(String[] args) throws InterruptedException, IOException {
		
		System.out.println("Thread Name 0 " + Thread.currentThread().getName());
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					GUITest window = new GUITest();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		
		Data data = new Data();
		
		for(int i = 0; i < 100; i++) {
			Thread.sleep(20);
			data.DataOut();
		}
	}

	public GUITest() throws IOException {
	
		initialize();
	}
		
	public void initialize() throws IOException {
		frame = new JFrame();
		frame.setBounds(1200, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		textArea_1.setBounds(120, 45, 100, 23);
		textArea_1.setText("");
		frame.getContentPane().add(textArea_1);
	}
}
    import java.io.IOException;

    public class Data {

	public void DataOut() throws IOException {
		int S = 0;
		for(int i = 0; i < 20; i++) {
			S++;
		}
		System.out.println(S);  // This should print to the JTextArea of class GUITest
	}
}
英文:

How do I send data from the method in Data class to a JTextArea in GUITest class, or System.out.println redirected to the JTextArea?

I need to do it in a way that only when data in DataOut method is updated, its sent, not polled for new data from the main method.

This is the sample code

    public class GUITest 

	private JFrame frame;
 	JTextArea textArea_1 = new JTextArea();
	
	public static void main(String[] args) throws InterruptedException, IOException {
		
		System.out.println (&quot;Thread Name 0 &quot;+ Thread.currentThread().getName());
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					GUITest window = new GUITest();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		
		Data data = new Data();
		
		for(int i = 0; i &lt; 100; i++) {
			Thread.sleep(20);
			data.DataOut();
		}
	}

	public GUITest() throws IOException {
	
		initialize();
	}
		
	public  void initialize() throws IOException {
		frame = new JFrame();
		frame.setBounds(1200, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		textArea_1.setBounds(120, 45, 100, 23);
		textArea_1.setText(&quot;&quot;);
		frame.getContentPane().add(textArea_1);
	}
}
    import java.io.IOException;

    public class Data {

	public void DataOut() throws IOException {
		int S = 0;
		for(int i = 0; i &lt; 20; i++) {
			S++;
		}
		System.out.println(S);  // This should print to the JTextArea of class GUITest
	}
}

答案1

得分: 0

有多种方法可以做到这一点。我脑海中首先想到的是:

让 Data 类的构造函数接受一个 GUITest 的实例,这样它就变成了:

Data data = new Data(this);

然后 Data 类变成了:

public class Data {
    GUITest instance;

    public Data(GUITest instance) {
        this.instance = instance;
    }
}

接着,在 GUITest 中创建一个名为 setTextAreaMessage(String message) 的方法,在 Data 类中,不再使用 System.out.println(S);,而是调用 instance.setTextAreaMessage(S);
不要忘记在 setTextAreaMessage(String message) 方法内使用这行代码来设置文本:text_Area1.setText(message);

这个解决方案是有效的,但老实说,我不记得你是否可以将像 jTextArea 这样的小部件声明为静态。如果可以的话,你可以直接这样做:GUITest.text_Area1.setText(S);

英文:

There are multiple ways to do that. The first thing that comes in my mind is:

Let the Data class constructor accept an istance of GUITest, so it becomes:

Data data = new Data(this);

and Data becomes:

public class Data{
  GUITest istance;

  public Data(GUITest istance){
     this.istance=istance;
  }
}

Then, create a method in GUITest called setTextAreaMessage(String message) and in the Data class, instead of System.out.println(S); you call istance.setTextAreaMessage(S);
Don't forget to set the text inside the setTextAreaMessage(String message) method with the line text_Area1.setText(message);

This solution works, but honestly I don't remember if you can declare a widget like jTextArea as static. If you can, you can just do something like GUITest.text_Area1.setText(S);

huangapple
  • 本文由 发表于 2020年10月19日 17:34:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64424675.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定