I resolved all my problems but one. I mounted the button, the labels and the text fields onto the frame. The program compiles fine the layouts are exactly what I want it to be. But the problem is that if I move my mouse cursor over the JFrame the labels and the text fields disappears except the button. Any Idea why it is happening?
Here is the updated code
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class SimpleCalculation implements ActionListener{
//Data Labels
//Fields for data entry
final JFormattedTextField currentField;
final JFormattedTextField systemVoltageField;
final JFormattedTextField kVAField;
//Labels to identify the fields
final JLabel currentLabel;
final JLabel systemVoltageLabel;
final JLabel kVALabel;
//The button
JButton button;
// begin constructor here
public SimpleCalculation(){
JFrame frame;
frame = new JFrame("Sun Stone");
// create the button and set layout
button = new JButton("Calculate");
button.setBounds(new Rectangle(20, 90, 40, 30));
// Create current the label and set layout
currentLabel = new JLabel("Current");
currentLabel.setLocation(150,180);
currentLabel.setSize(50,60);
// Create the current text fields and set layout
currentField = new JFormattedTextField();
currentField.setLocation(200, 200);
currentField.setSize(100,25);
currentField.setForeground(Color.BLACK);
// Create System Voltage the label and set layout
systemVoltageLabel = new JLabel("System Voltage");
systemVoltageLabel.setLocation(350,180);
systemVoltageLabel.setSize(90,60);
//Create the System Voltage text fields and set layout
systemVoltageField = new JFormattedTextField();
systemVoltageField.setLocation(445, 200);
systemVoltageField.setSize(100,25);
systemVoltageField.setForeground(Color.BLACK);
// Create kVA the label and set layout
kVALabel = new JLabel("kVA");
kVALabel.setLocation(590,180);
kVALabel.setSize(90,60);
// Create the kVA text fields and set layout
kVAField = new JFormattedTextField();
kVAField.setEditable(false);
kVAField.setLocation(620, 200);
kVAField.setSize(100,25);
kVAField.setForeground(Color.red);
button.addActionListener(this);
currentField.addActionListener(this);
systemVoltageField.addActionListener(this);
//Put the panels in this panel, labels on left,
//text fields on right and the button on the bottom.
frame.add(currentLabel);
frame.add(systemVoltageLabel);
frame.add(kVALabel);
frame.add(currentField);
frame.add(systemVoltageField);
frame.add(kVAField);
frame.add(button);
// Other frame properties
frame.setSize(1400,1400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
double Current=Double.parseDouble(currentField.getText());
double systemVoltage=Double.parseDouble(systemVoltageField.getText());
double kVA;
kVA = (Current*(systemVoltage*Math.sqrt(3)))/1000;
}
public static void main(String[] args) {
SimpleCalculation sc;
sc = new SimpleCalculation();
}
}