简体   繁体   English

如何将文本打印到文本区域

[英]How to print text to a text area

I have a text area where I want messages to be displayed in my game and I'm wondering how I would go about making a method that would print a text to the text area.我有一个文本区域,我希望在我的游戏中显示消息,我想知道如何制作一种将文本打印到文本区域的方法。 Here is my GUI class:这是我的 GUI 类:

package com.erikbalen.rpg;
import com.erikbalen.core.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Gui extends JFrame implements ActionListener {

/**
 * 
 */
private static final long serialVersionUID = -384241835772507459L;
private JLabel playerInfo;
private JTextField textField;
private final static String newline = "\n";
private JTextArea textArea;
private JScrollPane scrollPane;

public Gui(Player currentPlayer) {
    super("Erik's RPG");
    setLayout(new FlowLayout());        
    playerInfo = new JLabel(
       "<html>Health = " + currentPlayer.getHealth() 
               + " | " + "Mana = " + currentPlayer.getMana() + "</html>");  
    playerInfo.setBorder(BorderFactory.createTitledBorder(
               currentPlayer.getName()));
    textField = new JTextField(20);
    textField.addActionListener(this);
    textArea = new JTextArea(5, 20);
    scrollPane = new JScrollPane(textArea); 
    textArea.setEditable(false);

    add(playerInfo);
    add(textArea);
    add(textField);
    add(scrollPane);        
}

public void actionPerformed(ActionEvent textBox) {
        String text = textField.getText();
        textArea.append(text + newline);
        textArea.setCaretPosition(textArea.getDocument().getLength());
        textField.selectAll();          
}   
}

So basically I want to make a method that's like:所以基本上我想制作一个类似的方法:

public void printTextField(String text) {
    //print text to Gui.textArea
}

You mean other than你的意思是除了

public void printTextField(String text) {
    textArea.setText(text);
}

? ?

Ok, you can create an OutputStream for that.好的,您可以为此创建一个 OutputStream。 The jTextArea will then be printing out in your GUI any System.out.print() or errors...然后 jTextArea 将在您的 GUI 中打印出任何 System.out.print() 或错误...

To do that you add it to where you are creating the gui components.为此,请将其添加到创建 gui 组件的位置。

And you add below that for example:你在下面添加例如:

PrintStream outStream = new PrintStream( new TextAreaOutputStream(jTextArea1));
        jTextArea1.setFont(new Font(Font.MONOSPACED, Font.BOLD, 12));

        System.setOut( outStream );
        System.setErr( outStream );

Then you need an inner class within your code that extends OutputStream, (here i'm still doing it for a jTextArea i initialised called 'jTextArea1')然后,您需要在代码中使用一个内部类来扩展 OutputStream,(这里我仍在为我初始化的名为“jTextArea1”的 jTextArea 执行此操作)

public class TextAreaOutputStream extends OutputStream {
        private javax.swing.JTextArea jTextArea1;

        /**
         * Creates a new instance of TextAreaOutputStream which writes
         * to the specified instance of javax.swing.JTextArea control.
         *
         * @param textArea   A reference to the javax.swing.JTextArea
         *                  control to which the output must be redirected to.
         */
        public TextAreaOutputStream( JTextArea textArea ) {
            this.jTextArea1 = textArea;
        }

        public void write( int b ) throws IOException {
            jTextArea1.append( String.valueOf( ( char )b ) );
            jTextArea1.setCaretPosition(jTextArea1.getDocument().getLength());
        }

        public void write(char[] cbuf, int off, int len) throws IOException {
            jTextArea1.append(new String(cbuf, off, len));
            jTextArea1.setCaretPosition(jTextArea1.getDocument().getLength());
        }
    }

With that, all System.out's are redirected there, and it'll keep hold of all the data you give it, so you don't need that last method.这样,所有 System.out 都被重定向到那里,它会保留您提供的所有数据,因此您不需要最后一个方法。

And jTextArea1 is just jTextArea for you...而 jTextArea1 对你来说只是 jTextArea ......

Text, eh... 文字,嗯...

public class Store {
public static void main(String[] args) {
System.out.println("Hello world!");
  }
}

That syntax works. 该语法有效。

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

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