2017-04-09 2 views
-1
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); 
} 
} 

Il n'y a pas d'erreur de compilation, mais lorsque j'essaie d'exécuter mon applet.
L'applet ne s'initialise pas à cause de Double.parseDouble

Début: applet non initialisé
Cela apparaît en raison de la
// Chaîne à Double
= longueur Double.parseDouble (Get [0]);
width = Double.parseDouble (Get [1]);

Quelqu'un at-il une solution?
Essayer de le faire, je peux donc appuyer sur un bouton et obtenir le prix après mettre mes numéros dans les JTextFields

Répondre

0

Vous essayez d'analyser la chaîne « getLength » et « getWidth » Vous devez créer une méthode appelé getLength et getWidth.

+0

J'ai donc essayé de faire une nouvelle méthode. Mais je reçois une erreur maintenant dans le code. – Jeef

+0

public double getLength() { Récupère [0] = Case [0] .getText(); length = Double.parseDouble (Get [0]); longueur de retour; } public double getWidth() { Récupère [1] = Boîte [1] .getText(); width = Double.parseDouble (Get [1]); largeur de retour; } – Jeef

+0

getWidth() dans Carpet ne peut pas remplacer getWidth() dans java.awt.Component le type de retour double n'est pas compatible avec int – Jeef