Если вы выполните поиск в Google: «stdout JTextArea», у вас будет несколько ссылок для решения вашей проблемы.
В последней ссылке buddybob расширяет java.io.OutputStream
для печати стандартного вывода на его JTextArea. Я добавил его решение ниже.
/*
*
* @(#) TextAreaOutputStream.java
*
*/
import java.io.IOException;
import java.io.OutputStream;
import javax.swing.JTextArea;
/**
* An output stream that writes its output to a javax.swing.JTextArea
* control.
*
* @author Ranganath Kini
* @see javax.swing.JTextArea
*/
public class TextAreaOutputStream extends OutputStream {
private JTextArea textControl;
/**
* Creates a new instance of TextAreaOutputStream which writes
* to the specified instance of javax.swing.JTextArea control.
*
* @param control A reference to the javax.swing.JTextArea
* control to which the output must be redirected
* to.
*/
public TextAreaOutputStream( JTextArea control ) {
textControl = control;
}
/**
* Writes the specified byte as a character to the
* javax.swing.JTextArea.
*
* @param b The byte to be written as character to the
* JTextArea.
*/
public void write( int b ) throws IOException {
// append the data as characters to the JTextArea control
textControl.append( String.valueOf( ( char )b ) );
}
}
TextAreaOutputStream
расширяет классjava.io.OutputStream
и переопределяет его перегрузку методаwrite(int)
, этот класс использует ссылку на экземпляр управленияjavax.swing.JTextArea
, а затем добавляет к нему вывод всякий раз, когда вызывается метод write (int b).Чтобы использовать класс
blockquote>TextAreaOutputStream
, [yo] u должен использовать:Использование
// Create an instance of javax.swing.JTextArea control JTextArea txtConsole = new JTextArea(); // Now create a new TextAreaOutputStream to write to our JTextArea control and wrap a // PrintStream around it to support the println/printf methods. PrintStream out = new PrintStream( new TextAreaOutputStream( txtConsole ) ); // redirect standard output stream to the TextAreaOutputStream System.setOut( out ); // redirect standard error stream to the TextAreaOutputStream System.setErr( out ); // now test the mechanism System.out.println( "Hello World" );
У вас были некоторые неправильные выравнивания, поэтому я исправил их для вас, в общем, вы действительно проделали большую часть работы действительно хорошим способом.
Для будущих макетов я хочу порекомендовать использовать constraintLayout , это действительно приятно, просто в использовании и реагирует на экран разных размеров.
Вот новый макет (я также проверил его на galaxy j7) :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/questiontitle"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30sp"
android:text="newquestion"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/writequestion"
android:layout_centerHorizontal="true"
android:layout_below="@+id/questiontitle"
android:text="writequestion"
android:textSize="20sp"/>
<EditText
android:id="@+id/question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/writequestion"
android:layout_marginTop="30dp"
android:hint="questionhint" />
<TextView
android:layout_width="1dp"
android:layout_height="1dp"
android:layout_centerHorizontal="true"
android:layout_below="@+id/question"
android:layout_marginTop="60dp"
android:id="@+id/centerview"/>
<Button
android:id="@+id/cancelbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/question"
android:layout_marginTop="40dp"
android:layout_marginEnd="30dp"
android:layout_toEndOf="@+id/centerview"
android:text="cancel" />
<Button
android:id="@+id/okbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/question"
android:text="ok"
android:layout_toStartOf="@+id/centerview"
android:layout_marginTop="40dp"/>
</RelativeLayout>