I had created a basic calculator using Java Swing. The question I have is how to print each calculation on the panel. for example, I have added two numbers, my panel should look something like 2 + 4 = 6, and if I perform another operation it should append another operation in the panel.
My idea: I know I can do with the labels and change the value of the labels, but this is not a good idea since as the operations increase I cannot keep on adding the labels, I believe there should be a way but since I am learning Swing, I need some help from experts.
There are two possibilities:
Use a JTextArea and set it to be non-editable. The text area control will allow you to add multiple lines of text so that you can keep appending operations as you like.
textArea.setEditable(false);
Draw the text as graphics on a Canvas. This is a little more involved but you have more control over the way the text look and how it is formatted.
g.drawString(“a + b = c”, x, y);