Applet not initializing because of Double.parseDouble

时间:2017-04-09 23:50:24

标签: java arrays for-loop jbutton jtextfield

    import java.awt.*;
    import javax.swing.*;
    import java.text.*;
    import java.awt.event.*;
    public class Carpet extends JApplet implements ActionListener
    {
//Carpet
double length,width;

//double length,width;
double area;

//Money
double total;

double constant = 1.99;

DecimalFormat myFormat = new DecimalFormat("#0.00");
String numberInStringFormat;

//Essentials

JButton button;

JTextField [ ] Box = {
    new JTextField(),
    new JTextField()
};

JLabel [ ] Label = {
  new JLabel("Length"),
  new JLabel("Width"),
};

//Strings
String [ ] Get = {
    "getLength",
    "getWidth"
};

public void init()
{
   setLayout(null);

   //Button
   button = new JButton("Math!");
   button.setBounds(110,110,100,30);
   button.addActionListener(this);

   //Add
   add(button);
   myTexts();
   myLabels();
}

public void myTexts()
{
    //JText Sets
    for (int x=0;x<Box.length;x++)
    {
        Box[x].setBounds(50+(x*120),70,100,30);
        add(Box[x]);
    }

    //JText String
    for(int x=0;x<Get.length;x++)
    {
        Get[x]=Box[x].getText();
    }

    //String to Double
    length= Double.parseDouble(Get[0]);
    width= Double.parseDouble(Get[1]);


    //ActionListener
    for (int x=0;x<Box.length;x++)
    {
        Box[x].addActionListener(this);
    }
}

public void myLabels()
{
    //JLabels
    for (int x=0;x<Label.length;x++)
    {
        Label[x].setBounds(75+(x*120),45,100,30);
        add(Label[x]);
    }
}

public double getArea(double length, double width)
{
    area = length*width;
    return area;
}

public double getPrice(double area)
{
    total = area*constant;
    return total;
}

public void actionPerformed(ActionEvent ae)
{

}

public void paint(Graphics g)
{
    super.paint(g);

    g.drawString("Price : "+total,130,160);
}
}

There's no compiling error, but when i try to run my applet.

Start: applet not initialized
This appears because of the
//String to Double
length= Double.parseDouble(Get[0]);
width= Double.parseDouble(Get[1]);

Anybody got a solution?
Trying to make it so I can press a button and get the price out after putting my numbers in the JTextFields

1 个答案:

答案 0 :(得分:0)

You are trying to parse the string "getLength" and "getWidth" You will need to create a method called getLength and getWidth.