简体   繁体   English

将文本从JTextarea获取到OutputStream或将文本打印到控制台

[英]Get text from JTextarea into OutputStream or print text into console

How to get text from JTextarea into OutputStream or print text from JTextarea into console in Java? 如何将文本从JTextareaOutputStream或将文本从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 JTextArea achat by 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 JTextArea in main method(). 代码首先通过方法set()从JTextArea achat获取数据。.dis方法中的dn数据被传输到main或console的静态块中。主要问题是我无法从main中的JTextArea非静态字段获取数据方法()。

Component.getText() This function returns a String whatever 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();

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 Client object 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中获取文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM