Skip to content
Advertisement

Java GUI Calculator

My JTextArea does not display and no errors or problems shows up. It’s a Java GUI Calculator.

Main class code:

JavaScript

Calculator code:

JavaScript

Any suggestions on improvements to the code?

Advertisement

Answer

You’re adding your JTextArea, text, to your screen JPanel,

JavaScript

but you’re adding screen to nothing, and so it makes complete sense that the JTextArea won’t show. You should add the JTextArea to a container that somehow is either directly or indirectly added to your JFrame, else it’s not going to show. There’s no magic here — the GUI will only show what you add to it.

Note that since your JTextArea is written to be a single row, why not just use a JTextField instead? But regardless, the container hierarchy of this component must reach the top level window.

As an aside, read up on and learn about arrays and ArrayLists as you can use this and for loops to shorten your program considerably, making it much easier to read, understand and debug.

Please have a look at the code in this example to see what I mean. It will produce this GUI:
enter image description here

Advertisement