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:
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
}