List of errors: Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method setDefaultCloseOperation(int) is undefined for the type Calculator
The method setVisible(boolean) is undefined for the type Calculator
The method setSize(int, int) is undefined for the type Calculator
The method setTitle(String) is undefined for the type Calculator
The method getContentPane() is undefined for the type Calculator
I have gone back and corrected your code tags. That link tells you how they work. Doesn't it look better but it shows formatting problems. You have too many empty lines. Only put empty lines between successive methods/constructors or logical subdivisions of the code. Also be consistent with indentation. Get your IDE/editor to set up automatic indentation and automatically change tab characters to (?4) spaces.
Don't use GridBagConstraints directly. Find a copy of Core Java by Cay Horstmann and find out about his GBC class. There is a brief introduction to GBC here. GBC can save hundreds of lines of code in a large GUI.
Piet Souris wrote:. . . is it possible that you somewhere have another class Calculator . . .? . . .
How can you have two classes with the same name in the same package? More likely there is another class called JFrame, minus the setDefaultCloseOperation() method.
I am afraid I can see all sorts of problems with your code. The first is that I don't think you should subclass most GUI components. A calculator ISN'T‑A frame, but a calculator USES‑A frame. So I worry whenever I see class XYZ extends JFrame. In faact, you should write the calculator with no GUI at all, but methods like add(...) or getResult(), and then only add the GUI when you get the calculator working.
Line 8: Why did you use that serialVersionUID? Don't simply believe Eclipse's warnings without understanding them.
Lines 9‑12: Declare each variable on a line by itself. Give all fields private access. Actually, I think those variables don't need to be fields. I think most of them can be changed to become local variables in the constructor. Be careful with names of variables; sqrt appears several times in your code, with at least three different meanings, and followed in line 170 by squrt.
See code below. Note that the constructor includes code to make the GUI visible, so the GUI will appear if you execute new CalculatorGUI() anywhere.Don't write “^2” Write “\u00b2”. This is \u00b2: ² On a Linux box, try AltGr‑2 instead.
Don't make your frame implement action listener. Add different action listeners to the buttons. The method you have with multiple ifs is a maintenance problem. Unfortunately it is difficult if you use one button to set the operation, enter the numbers, and then use the “=” button to do the calculation. You will end up with code like this:-If you are feeling ambitious, there is a much more object‑oriented solution. Look in the Java® Language Specification, where you will find a ready‑made calculator application , which you can alter to suit your requirements.
posted 3 weeks ago
Campbell wrote:How can you have two classes with the same name in the same package? More likely there is another class called JFrame, minus the setDefaultCloseOperation() method.
Unlikely. But wanna make a bet on it?
posted 3 weeks ago
Piet Souris wrote:. . . Unlikely. But wanna make a bet on it?
This is BJ where the unlikeliest things in the programming universe happen