2017-04-09 14 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); 
} 
} 

애플릿을 실행하려고하면 컴파일 오류가 발생하지 않지만 오류는 발생하지 않습니다.
Double.parseDouble 때문에 애플릿이 초기화되지 않음

시작 :
를 초기화하지 애플릿이
길이를 두 배로 때문에
// 문자열이 나타납니다 = 사용해 Double.parseDouble가 (가져 오기 [0]);
width = Double.parseDouble (Get [1]);

아무도 해결책이 없습니까? 그렇게 나는 "getWidth는"당신은 방법을 만들어야합니다 버튼을 눌러

답변

0

당신은 "getLength"및 문자열을 구문 분석하려고하는 JTextFields에 내 번호를 입력 후에 가격을 얻을 수 있도록 노력
getLength 및 getWidth를 호출합니다.

+0

그래서 새로운 방법을 시도했습니다. 하지만 지금은 코드에서 오류가 발생합니다. – Jeef

+0

public double getLength() { [0] = 상자 [0] 가져 오기 .getText(); length = Double.parseDouble (Get [0]); 반품 길이; } public double getWidth() { Get [1] = Box [1] .getText(); width = Double.parseDouble (Get [1]); 반송 너비; } – Jeef

+0

카펫의 getWidth()는 java.awt.Component의 getWidth()를 재정의 할 수 없습니다. 반환 유형 double은 int와 호환되지 않습니다. – Jeef