java 从 JTextarea 获取文本到 OutputStream 或将文本打印到控制台
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16697478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get text from JTextarea into OutputStream or print text into console
提问by Jithin Corleone
How to get text from JTextareainto OutputStreamor print text from JTextareainto console in Java?
如何从获取文本JTextarea进入OutputStream或打印文本JTextarea到控制台的Java?
import java.io.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.net.*;
class Clients implements ActionListener
{
JFrame fr;
JPanel p1,p2;
JTextArea asend,achat;
JButton b1,b2;
String s1;
String f;
Clients()
{
fr=new JFrame();
fr.setLayout(new GridLayout(2,1));
p1=new JPanel(new GridLayout(1,1));
p2=new JPanel(new GridLayout(1,2));
achat=new JTextArea(80,80);
asend=new JTextArea(30,30);
b1=new JButton("send");
b2=new JButton("close");
p1.add(new JScrollPane(achat));
p2.add(asend);
p2.add(b1);
p1.setVisible(true);
p2.setVisible(true);
p1.setSize(400,300);
p2.setSize(400,100);
fr.add(p1);
fr.add(p2);
fr.setVisible(true);
fr.setSize(400,400);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
s1 = asend.getText();
appendData(s1);
send();
asend.setText( " ");
set(s1);
}
}
public void set(String a)
{
f=a;
}
public void appendData(String a)
{
String b=a;
//get data from 'asned' textarea into 'achat' textarea
achat.append( "\n SENT: "+b);
}
public String send()
{
String h=f;
//(return the value of string caught from textbox)
return(h);
}
public void setRec(String g)
{
String s2=g;
achat.append("\n RECIEVED: "+s2);
}
public static void main(String s[])
{
Clients d=new Clients();
/*
* object calls da method send 2 acesss non static
* data from static block of main method
*/
String sendn=d.send();
try
{
Socket so=new Socket("169.254.121.33 ",255);
DataInputStream inp=new DataInputStream(so.getInputStream());
PrintStream outp=new PrintStream(so.getOutputStream());
System.out.println(sendn);
Boolean b=true;
while(b)
{
String incomng=inp.readLine();
d.setRec(incomng); //incoming data is transferrd
System.out.println(incomng);
outp.println(sendn);
if(incomng==null)
{
b=false;
}
}
}
catch(IOException ee)
{
}
}
}
The code first gets the data from JTextAreaachatby method set()..dn data from dis method is transferred into static block of main or console ..Main problem is that I am unable to get data from non static field of JTextAreain main method().
代码首先通过 set()..dn 方法从JTextAreaachat获取数据从 dis 方法传输到 main 或控制台的静态块..主要问题是我无法从JTextAreain main 方法的非静态字段中获取数据( )。
回答by Asd
Component.getText()This function returns a Stringwhatever is in that area after you have pressed the button or done some action.
Component.getText()String在您按下按钮或执行某些操作后,此函数会返回该区域中的任何内容。
JTextArea textArea = new JTextArea();
String s = textArea.getText();
回答by ryvantage
There are quite a few things to mention with your code, but to simply answer your question: You can get the data from the non-static field of textarea in main by using the Clientobject you created (d).
您的代码有很多东西要提及,但简单地回答您的问题:您可以使用Client您创建的对象 ( d)从 main 中 textarea 的非静态字段中获取数据。
So
所以
String achat_text = d.achat.getText();
would be sufficient to grab the text from the textarea in your main method.
就足以从您的主要方法中的 textarea 中获取文本。

