2011-09-11 38 views
1

我有一個文本區域,我希望在我的遊戲中顯示消息,我想知道如何去製作將文本​​打印到文本區域的方法。這裏是我的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();   
} 
} 

所以基本上我想要的是像一個方法:

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

你確定要添加textarea的***和***滾動面板兩者兼而有之?相反,只需添加scrollPane(其中包含textArea)呢? –

回答

4

你的意思是不是

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

其他?

+0

我會問同樣的事情! – Mohayemin

+0

或者還有一個textAreandText(String text)方法,用於將text和「\ n」附加到textArea。 1 + –

+1

另外,當必須確保在事件分派線程或EDT上調用此方法時,棘手的部分就來了。 –

0

好的,你可以爲它創建一個OutputStream。 然後,jTextArea將在您的GUI中打印出任何System.out.print()或錯誤...

要做到這一點,請將其添加到創建gui組件的位置。

你需要添加下列例如:

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

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

然後,你需要你的代碼擴展的OutputStream, (在這裏,我還是這樣做了一個JTextArea內的內部類初始化我稱爲「jTextArea1」 )

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()); 
     } 
    } 

就這樣,所有的System.out的被重定向那裏,它會繼續保持你給它的數據的,所以你不需要那麼最後一個方法。

而且jTextArea1只是JTextArea中你...