I'm trying to accept user input from a textfield. For some reason, the String can't be accepted, gives an error. Here's my code. I placed it on the constructor so I just have to call it from my main method. I placed my initializations before the constructor.
public Compute2(){
Panel p1 = new Panel();
f1.setLayout(new FlowLayout());
CheckboxGroup g = new CheckboxGroup(); // creates radio buttons
Checkbox C = new Checkbox("Circle", g, false);
Checkbox R = new Checkbox("Rectangle", g, false);
p1.add(C);
p1.add(R);
f1.add(p1); // adds panel 1 to main frame
Panel p2 = new Panel(new GridLayout(4,1));
p2.add(lengthLabel);
p2.add(lengthField);
String l = "";
l = lengthField.getText();
length = Float.parseFloat(l);
p2.add(widthLabel);
p2.add(widthField);
String w = "";
w = widthField.getText();
width = Float.parseFloat(w);
p2.add(heightLabel);
p2.add(heightField);
String h = "";
h = heightField.getText();
height = Float.parseFloat(h);
p2.add(new Label(" ")); // required to "center" the button
p2.add(compute);
f1.add(p2); // adds panel 2 to main frame
compute.addActionListener(this);
compute.addActionListener(new ActionListener() { // when comp button is clicked, it must perform actions
@Override
public void actionPerformed(ActionEvent e) {
String ae = e.getActionCommand();
switch (ae) {
case "Circle":
areaCircle(length);
volumeCircle(length);
break;
case "Rectangle":
areaRectangle(length, width);
volumeRectangle(length, width, height);
break;
}
}
});
Panel p3 = new Panel();
f1.setLayout(new FlowLayout());
p3.add(volume);
volumeField = new TextField(10);
volumeField.setEditable(false);
p3.add(volumeField);
p3.add(area);
areaField = new TextField(10);
areaField.setEditable(false);
p3.add(areaField);
f1.add(p3); // adds panel 3 to main frame
f1.setSize(350, 250);
f1.setVisible(true);
f1.addWindowListener(new WindowAdapter(){ // closes program when "X" icon is clicked.
@Override
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}