Hello! I'm writing a very simple program to get used to using GUIs, and I'm having a bit of trouble. We're supposed to be writing a program that just creates a window and after the user puts in a number, it calculates the square root and the square of the number and displays them. I've modeled my code after the one we had in the lecture:
package javax.swing;
import java.awt.*;
import java.awt.event.*;
//I don't understand the difference between import.java.blah.* and package java.blah. Can someone
// explain that?
public class Calculations extends JFrame {
JLabel labelSquare;
JLabel labelRoot;
JTextField textfieldInput;
JButton calculateButton;
public void calculate() {
int squareValue;
squareValue = (int.parseInt(textfieldInput.getText()))^2;
double rootValue;
rootValue = (int.parseInt(textfieldInput.getText()))^(1/2);
labelSquare.setText(int.toString(squareValue));
labelRoot.setText(Double.toString(rootValue));
}
public Calculations() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(3.2));
textfieldInput = new JTextField();
getContentPane().add(textfieldInput);
JLabel inputLabel = new JLabel("input number");
getContentPane().add(inputLabel);
JLabel squareLabel = new JLabel();
getContentPane().add(squareLabel);
JLabel rootLabel = new JLabel();
getContentPane().add(rootLabel);
calculateButton = new JButton("Calculate");
getContentPane().add(calculateButton);
calculateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
calculate();
}
});
pack();
setVisible(true);
}
}
Right now, I'm getting this error message:
Calculations.java:30: class expected
squareValue = (int.parseInt(textfieldInput.getText()))^2;
^
Calculations.java:30: ')' expected
squareValue = (int.parseInt(textfieldInput.getText()))^2;
^
Calculations.java:32: class expected
rootValue = (int.parseInt(textfieldInput.getText()))^(1/2);
^
Calculations.java:32: ')' expected
rootValue = (int.parseInt(textfieldInput.getText()))^(1/2);
^
Calculations.java:34: class expected
labelSquare.setText(int.toString(squareValue));
^
Calculations.java:34: ')' expected
labelSquare.setText(int.toString(squareValue));
Can someone help me out? I'm not sure why those aren't working - like I said, I modeled this after the lecture, so maybe those lines aren't correct, I'm not sure.