GridLayout - Layout of Applet Help
Hi everyone I am in a bit of a time crunch and unsure what i am doing wrong with my applet/html page which calls it. Its a fairly simple applet with a few textboxes, checkboxes and one calculation. But when i load it, the format is all over the page, its not organized at all, i am lost. I have been at this for about a week. Can anyone please help me, code is below.
Thanks in advance
Rob
Code:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.io.*;
public class FinalApplet extends Applet implements ActionListener, ItemListener {
//declare variables
double value;
// Create components for applet
Label firstNameLabel = new Label("First Name");
TextField firstNameField = new TextField(15);
Label lastNameLabel = new Label("Last Name");
TextField lastNameField = new TextField(15);
Label addressLabel = new Label("Address");
TextField addressField = new TextField(15);
Label cityLabel = new Label("City");
TextField cityField = new TextField(10);
Label stateLabel = new Label("State");
TextField stateField = new TextField(2);
Label whenLabel = new Label("How soon to buy new car?");
Choice whenChoice = new Choice();
Label makeLabel = new Label("Make");
Choice makeChoice = new Choice();
Label modelLabel = new Label("Model");
Choice modelChoice = new Choice();
Label colorLabel = new Label("Color");
Choice colorChoice = new Choice();
//contruct components
CheckboxGroup optionsGroup = new CheckboxGroup();
Checkbox leatherBox = new Checkbox ("Leather", false, optionsGroup);
Checkbox navigationBox = new Checkbox ("Navigation", false, optionsGroup);
Checkbox seatsBox = new Checkbox ("Heated Seats", false, optionsGroup);
Checkbox sunroofBox = new Checkbox ("SunRoof", false, optionsGroup);
Label priceLabel = new Label("Anticipated Price");
TextField priceField = new TextField(5);
Label rateLabel = new Label("Rate");
TextField rateField = new TextField(5);
Label numbermonthsLabel = new Label("Number of months");
TextField numbermonthsField = new TextField(5);
Label commentsLabel = new Label("Comments");
TextField commentsField = new TextField(50);
Label outputLabel = new Label("Click the Calc button");
Button calcButton = new Button("Calculate");
Button submitButton = new Button("Submit");
Button clearButton = new Button("Clear");
public void init() {
setBackground(Color.BLUE);
setForeground(Color.BLACK);
setLayout(new GridLayout(4,0));
add(firstNameLabel);
add(firstNameField);
add(lastNameLabel);
add(lastNameField);
add(addressLabel);
add(addressField);
add(cityLabel);
add(cityField);
add(stateLabel);
add(stateField);
leatherBox.addItemListener(this);
add(leatherBox);
navigationBox.addItemListener(this);
add(navigationBox);
seatsBox.addItemListener(this);
add(seatsBox);
sunroofBox.addItemListener(this);
add(sunroofBox);
add(priceLabel);
add(priceField);
add(rateLabel);
add(rateField);
add(numbermonthsLabel);
add(numbermonthsField);
add(commentsLabel);
add(commentsField);
add(new Label(""));
add(outputLabel);
add(submitButton);
add(clearButton);
add(calcButton);
// Set Focus on First Name Field
firstNameField.requestFocus();
// Populate values
whenChoice.add("1 Months");
whenChoice.add("3 Months");
whenChoice.add("6 Months");
whenChoice.add("More than 1 Year");
makeChoice.add("Audi");
makeChoice.add("Honda");
makeChoice.add("Mazda");
makeChoice.add("Nissan");
makeChoice.add("Scion");
modelChoice.add("2 Door");
modelChoice.add("4 Door");
modelChoice.add("SUV");
modelChoice.add("Crossover");
colorChoice.add("Blue");
colorChoice.add("Red");
colorChoice.add("Silver");
colorChoice.add("White");
calcButton.addActionListener(this);
submitButton.addActionListener(this);
clearButton.addActionListener(this);
}
//check to see which button is pressed
public void actionPerformed(ActionEvent e)
{
String arg = e.getActionCommand();
if (arg.equalsIgnoreCase("Submit"))
{
}
else if (arg.equalsIgnoreCase("Clear"))
{
clearFields();
firstNameField.requestFocus();
}
else if (arg.equalsIgnoreCase("Calculate"))
{
double price = Double.parseDouble(priceField.getText());
double rate = Double.parseDouble(rateField.getText());
double months = Double.parseDouble(numbermonthsField.getText());
value = (price * rate) / months;
outputLabel.setText("Monthly Payment is" + Math.round(value* 100)/100D);
}
}
//Clear selections
private void clearFields()
{
firstNameField.setText("");
lastNameField.setText("");
addressField.setText("");
cityField.setText("");
stateField.setText("");
commentsField.setText("");
}
public void itemStateChanged(ItemEvent choice)
{
}
}
Code:
<HTML>
<APPLET CODE = "FinalApplet.class" WIDTH = "400" HEIGHT = "550">
</APPLET>
</HTML>