I'm trying to setup a function that takes input from a user and prints it onto a label and updates it per entry. The updating would occur through removing the old label and then adding a label with the updated value. The text would be center-aligned. While I'm able to get the label to print the current value of "entry", it does so without removing the label with the old value. I was wondering how I would be able to correct this issue?
import acm.graphics.*;
import acm.program.*;
public class testCanvas extends ConsoleProgram {
public void run()
{
GCanvas canvas = new GCanvas();
add(canvas);
String entry ="";
while(true)
{
entry += readLine("Give me a word: ");
if(entry=="") break;
GLabel label = new GLabel(entry);
label.setLocation(200-label.getWidth()/2,
60+label.getHeight());
label.setFont("Times New Roman-24");
// remove old label and immediately update it with
// label with current value for "entry"
canvas.remove(label);
canvas.add(label);
}
}
}