2012-08-15 58 views
0

這個簡單的程序應該能夠獲得標記百分比和批發成本,並計算出零售價格,我把一個行動監聽器放到了CALCULATE按鈕上,但是當我按下calculate按鈕時出現這個錯誤:在這個簡單的GUI程序中的錯誤

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: 
For input string: "Enter the markup precentage" 
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source) 
at java.lang.Double.parseDouble(Unknown Source) 
at mm$CalcListerner.actionPerformed(mm.java:58) 
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) 
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) 
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) 
at javax.swing.DefaultButtonModel.setPressed(Unknown Source) 

有人可以幫我解決這個問題嗎?

有人可以解釋這些錯誤信息,因爲我真的沒有得到任何的?

我的代碼是:

import javax.swing.*; 
import java.awt.event.*; 

public class mm extends JFrame { 

    private JTextField WholesaleCost; 
    private JTextField markupPresentage; 
    private JLabel WCost; 
    private JLabel MPrecentage; 
    private JButton button; 
    private JPanel pannel; 
    private final int Width = 250; 
    private final int height = 320; 

    public mm() { 
     setTitle("Retail Price Calculator"); 
     setSize(Width, height); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setVisible(true); 
     buildPanel(); 
     add(pannel); 
    } 

    private void buildPanel() { 
     WholesaleCost = new JTextField(10); 
     markupPresentage = new JTextField(10); 

     WCost = new JLabel("enter the Whole Sale cost"); 
     MPrecentage = new JLabel("Enter the markup precentage"); 

     button = new JButton("Calculate"); 
     button.addActionListener(new CalcListerner()); 

     pannel = new JPanel(); 
     pannel.add(WholesaleCost); 
     pannel.add(markupPresentage); 
     pannel.add(WCost); 
     pannel.add(MPrecentage); 
     pannel.add(button); 
    } 

    private class CalcListerner implements ActionListener { 

     public void actionPerformed(ActionEvent e) { 
      String WSaleinput; 
      String MPres; 
      WSaleinput = WholesaleCost.getText(); 
      MPres = MPrecentage.getText(); 

      double Value = Double.parseDouble(WSaleinput) * (Double.parseDouble(MPres)/100); 

      JOptionPane.showMessageDialog(null, "Retail Price is " + Value); 
     } 
    } 

    public static void main(String[] args) { 
     mm x = new mm(); 
    } 
} 
+0

是出現什麼錯誤? – Vulcan 2012-08-15 02:48:10

+0

我仍然沒有看到它。將它直接添加到您的問題中,因爲您可能沒有足夠的信譽來發布鏈接。 – Vulcan 2012-08-15 02:48:51

+0

@Vulcan異常在線程 「AWT-EventQueue的-0」 java.lang.NumberFormatException:對於輸入字符串: 「輸入標記PRECENTAGE」 \t在sun.misc.FloatingDecimal.readJavaFormatString(未知來源) \t在java.lang中。 Double.parseDouble(來源不明) \t在毫米$ CalcListerner.actionPerformed(mm.java:58) \t在javax.swing.AbstractButton.fireActionPerformed(來源不明) \t在javax.swing.AbstractButton中的$ Handler.actionPerformed(未知源) \t at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) \t at javax.swing.DefaultButtonModel.setPressed(Unknown So urce) \t ...........更多 – ChawBawwa 2012-08-15 02:50:24

回答

3

您正在嘗試引用錯誤的組件。

WSaleinput=WholesaleCost.getText(); 
MPres =MPrecentage.getText(); 

實際上應該是

WSaleinput = WholesaleCost.getText(); 
MPres = markupPresentage.getText(); 
+0

非常感謝你給我解釋錯誤信息 – ChawBawwa 2012-08-15 03:03:09

+1

基本上你說「請java可以轉換」輸入markup precentage「加倍」。 Java說:「我無法識別你以數字或數字格式傳遞給我的內容」 - 因此,「NumberFormatException」。它只是說,價值不是Java可以轉換的可識別的數字格式。 – MadProgrammer 2012-08-15 03:05:55

3

您正在試圖解析MPrecentage.getText()爲雙,但是這是你的標籤的價值,而不是所輸入的值。

您應該設置MPresmarkupPercentage.getText();

錯誤,NumberFormatException的,是一個如果JVM不能得到一個數字出來,你給它的字符串時引發。在這種情況下,您嘗試從文本「輸入標記百分比」解析數字

+0

打我吧;) – MadProgrammer 2012-08-15 02:56:38

+0

只有幾秒鐘:) – digitaljoel 2012-08-15 02:57:51

+0

非常感謝你能給我解釋一下錯誤信息 – ChawBawwa 2012-08-15 03:04:12